Libraries

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
## 
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")

Clearly there is a strong upward trend, with small but significant dips after 2008 and 2020.

cpi.train = window(cpi.ts,start=c(1947),frequency=12)
cpi.test = window(cpi.ts,start=c(2020,01),frequency=12)

Most of the test set will be from pandemic data which will hopefully be interesting.

acf(cpi.test)

pacf(cpi.test)

fets <- function(x, h) {
  forecast(ets(x), h = h)
}

farima <- function(x, h) {
  forecast(auto.arima(x), h=h)
}
m1 <- tsCV(cpi.test, fets, h=6)

m2 <- tsCV(cpi.test, farima, h=6)

mean(m1^2, na.rm=TRUE)
## [1] 26.47453
mean(m2^2, na.rm=TRUE)
## [1] 5.247888
cpi.ts %>% ets() %>% forecast() %>% autoplot()

accuracy(forecast(ets(cpi.ts))) 
##                       ME      RMSE       MAE         MPE     MAPE
## Training set 0.001296926 0.3726502 0.2018882 0.002565197 0.188413
##                    MASE      ACF1
## Training set 0.06116294 0.3205315
forecast(ets(cpi.ts))
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Nov 2020       260.7705 259.8389 261.7020 259.3458 262.1951
## Dec 2020       261.2148 259.7226 262.7070 258.9327 263.4970
## Jan 2021       261.6592 259.6115 263.7070 258.5275 264.7910
## Feb 2021       262.1036 259.4806 264.7267 258.0920 266.1152
## Mar 2021       262.5480 259.3230 265.7731 257.6157 267.4803
## Apr 2021       262.9924 259.1364 266.8484 257.0952 268.8896
## May 2021       263.4368 258.9205 267.9531 256.5297 270.3439
## Jun 2021       263.8812 258.6753 269.0870 255.9195 271.8428
## Jul 2021       264.3256 258.4015 270.2496 255.2655 273.3856
## Aug 2021       264.7700 258.0997 271.4403 254.5686 274.9713
## Sep 2021       265.2143 257.7704 272.6583 253.8299 276.5988
## Oct 2021       265.6587 257.4145 273.9029 253.0503 278.2672
## Nov 2021       266.1031 257.0326 275.1737 252.2309 279.9753
## Dec 2021       266.5475 256.6252 276.4698 251.3727 281.7223
## Jan 2022       266.9919 256.1931 277.7907 250.4765 283.5073
## Feb 2022       267.4363 255.7367 279.1359 249.5432 285.3293
## Mar 2022       267.8807 255.2565 280.5049 248.5737 287.1877
## Apr 2022       268.3251 254.7531 281.8971 247.5685 289.0816
## May 2022       268.7695 254.2269 283.3120 246.5285 291.0104
## Jun 2022       269.2139 253.6783 284.7494 245.4543 292.9734
## Jul 2022       269.6582 253.1079 286.2086 244.3466 294.9699
## Aug 2022       270.1026 252.5158 287.6894 243.2059 296.9993
## Sep 2022       270.5470 251.9026 289.1915 242.0328 299.0612
## Oct 2022       270.9914 251.2685 290.7143 240.8278 301.1550
cpi.ts %>% auto.arima() %>% forecast() %>% autoplot()

accuracy(forecast(auto.arima(cpi.ts))) 
##                       ME      RMSE       MAE        MPE     MAPE
## Training set 0.005554254 0.3221771 0.1864853 0.01124957 0.187615
##                    MASE          ACF1
## Training set 0.05649659 -0.0009797405
forecast(auto.arima(cpi.ts))
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Nov 2020       260.5327 260.1179 260.9474 259.8983 261.1670
## Dec 2020       260.8304 260.0570 261.6038 259.6476 262.0132
## Jan 2021       261.2074 260.1549 262.2600 259.5977 262.8171
## Feb 2021       261.5648 260.2923 262.8372 259.6187 263.5108
## Mar 2021       262.1280 260.6659 263.5901 259.8920 264.3641
## Apr 2021       262.8291 261.1905 264.4677 260.3231 265.3351
## May 2021       263.2662 261.4572 265.0752 260.4996 266.0329
## Jun 2021       263.4445 261.4688 265.4202 260.4230 266.4661
## Jul 2021       263.5848 261.4454 265.7243 260.3128 266.8569
## Aug 2021       263.8437 261.5427 266.1447 260.3246 267.3628
## Sep 2021       264.1662 261.7053 266.6271 260.4025 267.9299
## Oct 2021       264.5327 261.9129 267.1525 260.5260 268.5394
## Nov 2021       264.8968 262.1405 267.6531 260.6815 269.1122
## Dec 2021       265.2435 262.3610 268.1260 260.8351 269.6518
## Jan 2022       265.5944 262.5865 268.6022 260.9942 270.1945
## Feb 2022       265.9481 262.8126 269.0835 261.1529 270.7433
## Mar 2022       266.3745 263.1097 269.6394 261.3814 271.3677
## Apr 2022       266.8497 263.4542 270.2452 261.6567 272.0427
## May 2022       267.2284 263.7013 270.7556 261.8342 272.6227
## Jun 2022       267.5231 263.8635 271.1827 261.9262 273.1200
## Jul 2022       267.8118 264.0187 271.6049 262.0108 273.6128
## Aug 2022       268.1324 264.2048 272.0600 262.1257 274.1391
## Sep 2022       268.4752 264.4121 272.5383 262.2613 274.6892
## Oct 2022       268.8374 264.6378 273.0371 262.4146 275.2603