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(knitr)
library(fpp2)
## Warning: package 'fpp2' was built under R version 3.6.2
## 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
## ── Attaching packages ───────────────────────────────────────────────── fpp2 2.4 ──
## ✓ ggplot2 3.3.2 ✓ fma 2.4
## ✓ forecast 8.13 ✓ expsmooth 2.3
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'forecast' was built under R version 3.6.2
##
library(fGarch)
## Loading required package: timeDate
## Loading required package: timeSeries
## Loading required package: fBasics
Using data from last week’s discussion: CPI
cpi = read.csv("/Users/nelsonwhite/Documents/ms applied economics/Predictive Analytics:Forecasting/week 4 discussion/CPIAUCSL.csv")
cpi.ts = ts(cpi[,2], start=c(1947,01,01), frequency = 12)
autoplot(cpi.ts, ylab = "CPI", main = "Consumer Price Index, All Urban Consumers")
cpi.garch = garchFit(data = cpi.ts, cond.dist = "norm")
##
## 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: norm
## h.start: 2
## llh.start: 1
## Length of Series: 886
## Recursion Init: mci
## Series Scale: 78.8558
##
## Parameter Initialization:
## Initial Parameters: $params
## Limits of Transformations: $U, $V
## Which Parameters are Fixed? $includes
## Parameter Matrix:
## U V params includes
## mu -14.17506676 14.17507 1.417507 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: 1149.3479: 1.41751 0.100000 0.100000 0.800000
## 1: 1130.6585: 0.895175 1.00000e-06 0.474073 1.00000e-08
## 2: 912.15492: 0.432317 0.103315 0.811908 1.00000e-08
## 3: 904.27902: 0.548764 0.102958 0.804002 1.00000e-08
## 4: 901.23316: 0.544605 0.100670 0.886448 1.00000e-08
## 5: 748.71164: 0.529297 0.0188568 0.885395 1.00000e-08
## 6: 387.96377: 0.346008 1.00000e-06 0.784538 1.00000e-08
## 7: 387.86600: 0.346008 8.22190e-06 0.784538 1.21022e-08
## 8: 387.61681: 0.346007 6.59906e-06 0.784536 1.80849e-06
## 9: 387.38502: 0.346007 2.32390e-06 0.784536 1.81336e-06
## 10: 387.37480: 0.346005 4.45348e-06 0.784533 3.87735e-06
## 11: 387.32921: 0.346004 3.69204e-06 0.784532 4.98760e-06
## 12: 387.31674: 0.346002 3.05730e-06 0.784528 7.42474e-06
## 13: 387.30773: 0.346001 3.32001e-06 0.784525 9.88809e-06
## 14: 364.34976: 0.342346 6.44180e-06 0.777460 0.00508158
## 15: 354.53873: 0.340522 6.79154e-06 0.773935 0.00761070
## 16: 333.82181: 0.340370 1.00000e-06 0.775946 0.00537277
## 17: 322.92387: 0.340262 1.00000e-06 0.874935 1.00000e-08
## 18: 321.92862: 0.340189 1.00000e-06 0.877291 0.0689109
## 19: 321.91442: 0.340121 1.00000e-06 0.877293 0.0689110
## 20: 321.91252: 0.340133 1.00000e-06 0.877335 0.0688911
## 21: 321.91149: 0.340124 1.00000e-06 0.877382 0.0688935
## 22: 321.20005: 0.340125 1.00000e-06 0.904016 0.0557390
## 23: 318.79886: 0.340138 1.00000e-06 0.988911 1.00000e-08
## 24: 318.77100: 0.340146 1.00000e-06 0.995822 1.00000e-08
## 25: 318.76448: 0.340145 1.00000e-06 1.00000 1.00000e-08
## 26: 318.76447: 0.340146 1.00000e-06 1.00000 1.00000e-08
## 27: 318.76447: 0.340146 1.00000e-06 1.00000 1.00000e-08
##
## Final Estimate of the Negative LLH:
## LLH: 4188.477 norm LLH: 4.7274
## mu omega alpha1 beta1
## 26.822462470 0.006218237 0.999999990 0.000000010
##
## R-optimhess Difference Approximated Hessian Matrix:
## mu omega alpha1 beta1
## mu -1682.102405 83.102436 2.509533 -4.536496e+01
## omega 83.102436 -3.869352 303.312198 2.287642e+01
## alpha1 2.509533 303.312198 -429.351479 -7.133126e+02
## beta1 -45.364965 22.876423 -713.312601 -1.497544e+06
## attr(,"time")
## Time difference of 0.009613991 secs
##
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): NaNs produced
##
## Time to Estimate Parameters:
## Time difference of 0.0475359 secs
cpi.spec = garchSpec(model = cpi.garch, cond.dist = "norm")
## Warning in `[<-`(`*tmp*`, names(model), value = model): implicit list
## embedding of S4 objects is deprecated
coef(cpi.garch)
## mu omega alpha1 beta1
## 26.822462470 0.006218237 0.999999990 0.000000010
plot(residuals(cpi.garch))
cpi.garch.fcast = predict(cpi.garch, n.ahead=12, plot=TRUE)
acf(cpi.garch@residuals)
As we can see, there is high autocorrelation with the CPI data in a Garch model. Considering that Garch performs well with hetereoskedastic data, last week’s Arima model and even the ETS model was a better fit for the time series.