For the same data set used in previous weeks, build GARCH models using either the fGarch or other packages. Compare this model to others you have produced.
For this analysis I continued to work with Tesla.
#Import Data
library(forecast)
## Warning: package 'forecast' was built under R version 3.4.2
## Warning in as.POSIXlt.POSIXct(Sys.time()): unknown timezone 'zone/tz/2018c.
## 1.0/zoneinfo/America/New_York'
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(tseries)
library(readr)
mydata <- read_csv("~/Desktop/TSLA-3.csv")
## Parsed with column specification:
## cols(
## Date = col_date(format = ""),
## Open = col_double(),
## High = col_double(),
## Low = col_double(),
## Close = col_double(),
## `Adj Close` = col_double(),
## Volume = col_integer()
## )
#Create Time Series
myts<-ts(mydata$`Adj Close`, frequency=252, start=c(2013,4))
plot(myts, ylab= "Adjusted Closing Price", main= "Tesla, Inc, (TSLA)")
#Create Training and Test Set
#Training is 80%
#Test set is 20%
train=myts[2:1007]
test=myts[1008:1260]
volume.train <- as.numeric(mydata$Volume[1:1006])
volume.test <- as.numeric(mydata$Volume[1007:1259])
#Select ARIMA(0,1,0)
fit1 <- auto.arima(train)
fit1
## Series: train
## ARIMA(0,1,0)
##
## sigma^2 estimated as 32.6: log likelihood=-3176.89
## AIC=6355.77 AICc=6355.78 BIC=6360.68
fcast1 <- forecast(fit1, h=253)
accuracy(fcast1, test)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.2536318 5.70672 4.047787 0.1448252 2.077068 0.9990167
## Test set 33.6910950 41.12391 35.242875 9.6701336 10.255148 8.6981408
## ACF1
## Training set 0.032286
## Test set NA
plot(fcast1)
#Regression with ARIMA(0,1,0) errors
fit2<-auto.arima(train, xreg=volume.train)
fit2
## Series: train
## Regression with ARIMA(0,1,0) errors
##
## Coefficients:
## xreg
## 0
## s.e. 0
##
## sigma^2 estimated as 32.62: log likelihood=-3176.76
## AIC=6357.52 AICc=6357.53 BIC=6367.35
fcast2 <- forecast(fit2, xreg=volume.test, h=252)
accuracy(fcast2, test)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.2534863 5.706008 4.048442 0.1445037 2.076736 0.9991785
## Test set 33.7186646 41.158317 35.280248 9.6779454 10.266610 8.7073647
## ACF1
## Training set 0.03288603
## Test set NA
plot(fcast2)
#ETS(A,N,N)
fit3=ets(train,model="ZZZ")
fit3
## ETS(A,N,N)
##
## Call:
## ets(y = train, model = "ZZZ")
##
## Smoothing parameters:
## alpha = 0.9999
##
## Initial states:
## l = 43.585
##
## sigma: 5.7067
##
## AIC AICc BIC
## 10465.42 10465.44 10480.16
fcast3 <- forecast(fit3, h=253, drift=TRUE)
accuracy(fcast3, test)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.2536188 5.706739 4.047732 0.1447515 2.076971 0.9990032
## Test set 33.6914690 41.124215 35.243222 9.6702467 10.255251 8.6982266
## ACF1
## Training set 0.03238688
## Test set NA
plot(fcast3)
#GARCH
require(fGarch)
## Loading required package: fGarch
## Warning: package 'fGarch' was built under R version 3.4.2
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 3.4.3
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.4.2
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.4.2
fit4 <- garchFit(data=train, formula=~arma(1,1)+garch(1,1),
cond.dist="QML", trace=FALSE)
coef(fit4)
## mu ar1 ma1 omega alpha1 beta1
## 2.69009869 0.98820386 0.03852065 0.56785836 0.02127373 0.96195834
summary(fit4)
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~arma(1, 1) + garch(1, 1), data = train, cond.dist = "QML",
## trace = FALSE)
##
## Mean and Variance Equation:
## data ~ arma(1, 1) + garch(1, 1)
## <environment: 0x7fbb7498c428>
## [data = train]
##
## Conditional Distribution:
## QMLE
##
## Coefficient(s):
## mu ar1 ma1 omega alpha1 beta1
## 2.690099 0.988204 0.038521 0.567858 0.021274 0.961958
##
## Std. Errors:
## robust
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 2.690099 0.812120 3.312 0.000925 ***
## ar1 0.988204 0.003975 248.606 < 2e-16 ***
## ma1 0.038521 0.033745 1.142 0.253654
## omega 0.567858 0.062684 9.059 < 2e-16 ***
## alpha1 0.021274 0.008589 2.477 0.013253 *
## beta1 0.961958 0.009967 96.512 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## -3165.328 normalized: -3.146449
##
## Description:
## Wed Apr 18 20:19:16 2018 by user:
##
##
## Standardised Residuals Tests:
## Statistic p-Value
## Jarque-Bera Test R Chi^2 373.0011 0
## Shapiro-Wilk Test R W 0.9630301 2.769111e-15
## Ljung-Box Test R Q(10) 4.067378 0.9442562
## Ljung-Box Test R Q(15) 5.438833 0.9877137
## Ljung-Box Test R Q(20) 8.040335 0.9915975
## Ljung-Box Test R^2 Q(10) 6.469537 0.774394
## Ljung-Box Test R^2 Q(15) 9.710445 0.8375476
## Ljung-Box Test R^2 Q(20) 13.70045 0.8453531
## LM Arch Test R TR^2 8.282756 0.762662
##
## Information Criterion Statistics:
## AIC BIC SIC HQIC
## 6.304827 6.334133 6.304756 6.315962
plot(fit4@residuals)
hist(fit4@residuals)
fcast4 <- predict(fit4, n.ahead = 253)
meanForecast <- fcast4$meanForecast
plot(meanForecast)
accuracy(meanForecast, test)
## ME RMSE MAE MPE MAPE
## Test set 82.05142 87.34887 82.05142 24.2879 24.2879
The ARIMA(0,1,0) has the best accuarcy measurements still when used on the test set. It has lower ME, RMSE, MAE, MPE, MAPE, and MASE.