This series contains the daily pageviews for one year for the Hyndsight blog, beginning 30 April 2014. It is from the fpp2 package.

Setup

library(fGarch)
## Loading required package: timeDate
## Loading required package: timeSeries
## Loading required package: fBasics
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(fpp2)
## Loading required package: ggplot2
## Loading required package: fma
## Loading required package: expsmooth
library(tseries)

Data

autoplot(hyndsight, xlab="Weeks", main = "Pageviews")

decomp = stl(hyndsight,s.window='periodic')
autoplot(decomp)

trainData = head(hyndsight, n = 294)
testData = tail(hyndsight, n = 71)

Holt Winters Model

HWmod = HoltWinters(trainData)
HWfc = forecast(HWmod, h = 71)
autoplot(HWfc)

checkresiduals(HWfc)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

Arima

adf.test(trainData)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  trainData
## Dickey-Fuller = -3.1753, Lag order = 6, p-value = 0.0924
## alternative hypothesis: stationary
arimaMod = auto.arima(trainData)
arimaFC = forecast(arimaMod, h=71)
autoplot(arimaFC)

checkresiduals(arimaFC)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(1,0,0)(2,1,1)[7]
## Q* = 19.671, df = 10, p-value = 0.03252
## 
## Model df: 4.   Total lags used: 14

GARCH model

pacf(trainData)

acf(trainData)

garchMod = garchFit(trainData, formula = ~ garch(1,1), cond.dist = 'QMLE')
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(1, 1)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               1 1
##  Max GARCH Order:           1
##  Maximum Order:             1
##  Conditional Dist:          QMLE
##  h.start:                   2
##  llh.start:                 1
##  Length of Series:          294
##  Recursion Init:            mci
##  Series Scale:              401.5943
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                       U         V   params includes
##     mu     -29.95077825  29.95078 2.995078     TRUE
##     omega    0.00000100 100.00000 0.100000     TRUE
##     alpha1   0.00000001   1.00000 0.100000     TRUE
##     gamma1  -0.99999999   1.00000 0.100000    FALSE
##     beta1    0.00000001   1.00000 0.800000     TRUE
##     delta    0.00000000   2.00000 2.000000    FALSE
##     skew     0.10000000  10.00000 1.000000    FALSE
##     shape    1.00000000  10.00000 4.000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1  beta1 
##      1      2      3      5 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     412.25139:  2.99508 0.100000 0.100000 0.800000
##   1:     411.98846:  2.96187 0.104621 0.102008 0.804406
##   2:     411.84934:  2.95540 0.101297 0.0989035 0.801188
##   3:     411.83051:  2.94110 0.103588 0.100145 0.803666
##   4:     411.80214:  2.93757 0.101094 0.0979813 0.801493
##   5:     411.79270:  2.92769 0.100886 0.0987226 0.803850
##   6:     411.78526:  2.93376 0.0981615 0.0990060 0.806176
##   7:     411.78513:  2.93370 0.0980369 0.0988736 0.806173
##   8:     411.78502:  2.93364 0.0980545 0.0988533 0.806353
##   9:     411.78480:  2.93365 0.0978493 0.0986067 0.806529
##  10:     411.78283:  2.93427 0.0966428 0.0970869 0.809468
##  11:     411.78184:  2.93280 0.0938219 0.0975861 0.811467
##  12:     411.78161:  2.93300 0.0939818 0.0976826 0.811656
##  13:     411.78145:  2.93328 0.0938752 0.0974735 0.811762
##  14:     411.78129:  2.93341 0.0936296 0.0974088 0.812245
##  15:     411.78112:  2.93317 0.0928343 0.0973111 0.812987
##  16:     411.78089:  2.93424 0.0933771 0.0964447 0.813150
##  17:     411.78088:  2.93421 0.0934234 0.0964939 0.813236
##  18:     411.78085:  2.93420 0.0933375 0.0964438 0.813283
##  19:     411.78082:  2.93409 0.0932437 0.0964438 0.813479
##  20:     411.78054:  2.93434 0.0917358 0.0957139 0.815615
##  21:     411.78054:  2.93433 0.0915393 0.0958682 0.815680
##  22:     411.78054:  2.93415 0.0915536 0.0958368 0.815680
##  23:     411.78054:  2.93425 0.0915792 0.0958288 0.815667
##  24:     411.78054:  2.93425 0.0915735 0.0958326 0.815670
## 
## Final Estimate of the Negative LLH:
##  LLH:  2174.441    norm LLH:  7.396056 
##           mu        omega       alpha1        beta1 
## 1.178377e+03 1.476878e+04 9.583263e-02 8.156697e-01 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                   mu         omega        alpha1         beta1
## mu     -1.398228e-03  9.706655e-07 -1.562787e-01  1.002592e-01
## omega   9.706655e-07 -1.931015e-07 -2.401451e-02 -2.891337e-02
## alpha1 -1.562787e-01 -2.401451e-02 -3.741445e+03 -3.818930e+03
## beta1   1.002592e-01 -2.891337e-02 -3.818930e+03 -4.506027e+03
## attr(,"time")
## Time difference of 0.01150608 secs
## 
## --- END OF TRACE ---
## 
## 
## Time to Estimate Parameters:
##  Time difference of 0.04597807 secs
## Warning: Using formula(x) is deprecated when x is a character vector of length > 1.
##   Consider formula(paste(x, collapse = " ")) instead.
checkresiduals(garchMod@residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

garchFC = predict(garchMod, n.ahead=71, plot = TRUE)

summary(garchFC)
##   meanForecast    meanError     standardDeviation lowerInterval  
##  Min.   :1178   Min.   :404.4   Min.   :404.4     Min.   :481.0  
##  1st Qu.:1178   1st Qu.:407.7   1st Qu.:407.7     1st Qu.:481.0  
##  Median :1178   Median :408.4   Median :408.4     Median :481.2  
##  Mean   :1178   Mean   :407.9   Mean   :407.9     Mean   :482.1  
##  3rd Qu.:1178   3rd Qu.:408.5   3rd Qu.:408.5     3rd Qu.:482.4  
##  Max.   :1178   Max.   :408.5   Max.   :408.5     Max.   :488.0  
##  upperInterval 
##  Min.   :1972  
##  1st Qu.:1979  
##  Median :1980  
##  Mean   :1979  
##  3rd Qu.:1980  
##  Max.   :1980

Comparison

accuracy(garchFC$meanForecast, testData)
##                ME     RMSE      MAE      MPE     MAPE      ACF1 Theil's U
## Test set 452.1019 611.9964 514.5392 22.18546 28.77365 0.5247578  1.210584
accuracy(HWfc, testData)
##                      ME     RMSE      MAE       MPE     MAPE     MASE      ACF1
## Training set  -3.039342 244.0232 175.3343 -3.422528 15.45622 0.794019 0.2465167
## Test set     253.524421 334.2907 262.6859 14.157454 15.03539 1.189599 0.4400235
##              Theil's U
## Training set        NA
## Test set     0.6745651
accuracy(arimaFC, testData)
##                     ME     RMSE      MAE       MPE     MAPE     MASE
## Training set  20.09961 221.2017 150.8014 -1.247853 12.74527 0.682919
## Test set     251.77245 338.0943 263.0560 13.756835 14.79158 1.191275
##                     ACF1 Theil's U
## Training set -0.03521556        NA
## Test set      0.46563461  0.675819

We can see that the arima and holt winters model outperforn the garch model in this instance. I'm not sure why the garch was returning flat forecasts, but that is likely the reason why the forecasts performed so poorly against the test set. I will note that the holt winters and arima models also were quite biased and inaccurate. We also see in the garch fit that the model still contains lots of autocorrelation.