title: “Time Series Analysis” author: “Cornelius Tanui” date: “January 19, 2020” output: html_document #_____________________________________________________________________________
library(readxl)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
library(forecast)
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
library(ggTimeSeries)
TimeSeriesData <- read_excel("C:/Users/User/OneDrive/CORY/DATA/TimeSeries.xlsx",
col_types = c("date",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric")) %>% as.data.frame()
class(TimeSeriesData)
## [1] "data.frame"
View(TimeSeriesData)
sapply(TimeSeriesData, class)
## $date
## [1] "POSIXct" "POSIXt"
##
## $open
## [1] "numeric"
##
## $high
## [1] "numeric"
##
## $low
## [1] "numeric"
##
## $close
## [1] "numeric"
##
## $volume
## [1] "numeric"
ggplot(TimeSeriesData,
mapping = aes(x = date)) +
geom_line(aes(y = low),
colour = "green",
size = 1.5) +
geom_point(aes(y = high),
colour = "red",
size = 0.5) +
labs(title = "Trend of Low and High Values of Stock",
subtitle = "Red = 'High', Green = 'Low'",
caption = "Data source: AMREC Africa") +
theme(plot.title = element_text(hjust = 0.5, size = 14), # Center title position and size
plot.subtitle = element_text(hjust = 0.5), # Center subtitle
plot.caption = element_text(hjust = 0, face = "italic")) +
xlab("Year") +
ylab("Value") +
theme_light()
#_____________________________________________________________________________ ## Data Cleaning #_____________________________________________________________________________
min(TimeSeriesData$date)
## [1] "1997-07-02 UTC"
max(TimeSeriesData$date)
## [1] "2016-05-31 UTC"
TimeSeriesData <- TimeSeriesData %>% arrange(date)
TimeSeries <- ts(TimeSeriesData$open,
start = c(1997, 7),
end = c(2016, 7),
frequency = 12)
plot(TimeSeries,
xlim = c(1997, 2016),
col = "red",
lwd = 2)
#_____________________________________________________________________________ ## Decomposition #_____________________________________________________________________________
Decomposed <- decompose(TimeSeries)
#Decomposed
plot(Decomposed)
plot(Decomposed$x)
plot(Decomposed$seasonal)
plot(Decomposed$trend)
plot(Decomposed$random)
#mean(Decomposed$random,
# na.rm = TRUE)
HW_Smoothing <- HoltWinters(TimeSeries, # Smooth (remove trend)
beta=FALSE,
gamma=FALSE,
l.start=23.56)
plot(forecast(HW_Smoothing))
HW_Forecasts <- forecast:::forecast.HoltWinters(HW_Smoothing,
h=10) # Forecast 10 years ahead
forecast:::plot.forecast(HW_Forecasts) # Plot the forecasts
Box.test(HW_Forecasts$residuals,
lag = 20,
type = "Ljung-Box") # A p-value less than 0.05 means the errors are white noise
##
## Box-Ljung test
##
## data: HW_Forecasts$residuals
## X-squared = 1.3012, df = 20, p-value = 1
plot.ts(HW_Forecasts$residuals)
#_____________________________________________________________________________