library(forecast)
library(stats)
library(readxl)
inflation<-read.csv("C:\\Users\\Jaire\\OneDrive\\Desktop\\Exploratory Research\\Data\\long_inflation.csv")
# U.S. Consumer Price Index for All Urban Consumers (CPI-U) reshaped into long format - 792 months from JAN 1958 to DEC 2023
# extracted from https://data.bls.gov/
summary(inflation)
## CPI_U
## Min. : 0.60
## 1st Qu.: 2.00
## Median : 2.70
## Mean : 3.68
## 3rd Qu.: 4.70
## Max. :13.60
inflationseries <- ts(inflation, frequency=12, start=c(1958,1))
summary(inflationseries)
## CPI_U
## Min. : 0.60
## 1st Qu.: 2.00
## Median : 2.70
## Mean : 3.68
## 3rd Qu.: 4.70
## Max. :13.60
plot(inflationseries)
library(TTR)
inflationseries_SMA3 <- SMA(inflationseries,n=100)
plot.ts(inflationseries_SMA3)
inflationseriesforecasts <- HoltWinters(inflationseries, gamma=FALSE)
inflationseriesforecasts
## Holt-Winters exponential smoothing with trend and without seasonal component.
##
## Call:
## HoltWinters(x = inflationseries, gamma = FALSE)
##
## Smoothing parameters:
## alpha: 1
## beta : 0.242808
## gamma: FALSE
##
## Coefficients:
## [,1]
## a 3.9000000
## b -0.1338818
inflationseriesforecasts$SSE
## [1] 49.68064
plot(inflationseriesforecasts)
plot(forecast(inflationseriesforecasts, h=5))
HWpredicted_inflation<-predict(inflationseriesforecasts, n.ahead = 5, prediction.interval = TRUE,
level = 0.95)
HWpredicted_inflation
## fit upr lwr
## Jan 2024 3.766118 4.257933 3.274304
## Feb 2024 3.632236 4.416765 2.847708
## Mar 2024 3.498355 4.570424 2.426286
## Apr 2024 3.364473 4.732662 1.996284
## May 2024 3.230591 4.907444 1.553739
plot(HWpredicted_inflation)
plot(forecast(HWpredicted_inflation, h=50))
library(ggplot2)
library(eFRED)
## Loading required package: jsonlite
## Loading required package: httr
# Retrieve data from FRED
set_fred_key(fred_api)
# FPCPITOTLZGUSA- Inflation, consumer prices for the United States
fredcpi <- fred(y = "FPCPITOTLZGUSA", all=FALSE)
# Check for missing values
summary(is.na(fredcpi))
## date y
## Mode :logical Mode :logical
## FALSE:63 FALSE:63
head(fredcpi)
## date y
## 1 1960-01-01 1.457976
## 2 1961-01-01 1.070724
## 3 1962-01-01 1.198773
## 4 1963-01-01 1.239669
## 5 1964-01-01 1.278912
## 6 1965-01-01 1.585169
# time series object
fredcpi_ts <- ts(fredcpi[,"y"], start = c(1960, 1), frequency = 1)
(fredcpi_ts)
## Time Series:
## Start = 1960
## End = 2022
## Frequency = 1
## [1] 1.4579760 1.0707241 1.1987733 1.2396694 1.2789116 1.5851693
## [7] 3.0150754 2.7727856 4.2717962 5.4623862 5.8382553 4.2927667
## [13] 3.2722782 6.1777601 11.0548048 9.1431469 5.7448126 6.5016840
## [19] 7.6309638 11.2544711 13.5492020 10.3347153 6.1314270 3.2124352
## [25] 4.3005355 3.5456442 1.8980477 3.6645632 4.0777411 4.8270030
## [31] 5.3979564 4.2349640 3.0288197 2.9516570 2.6074416 2.8054197
## [37] 2.9312042 2.3376899 1.5522791 2.1880272 3.3768573 2.8261711
## [43] 1.5860316 2.2700950 2.6772367 3.3927468 3.2259441 2.8526725
## [49] 3.8391003 -0.3555463 1.6400434 3.1568416 2.0693373 1.4648327
## [55] 1.6222230 0.1186271 1.2615832 2.1301100 2.4425833 1.8122101
## [61] 1.2335844 4.6978589 8.0027998
# time series, ACF, & PACF
plot(fredcpi_ts)
acf(fredcpi_ts, main = "ACF of Inflation")
pacf(fredcpi_ts, main = "PACF of Inflation")
High autocorrelation within the first 10 lags of the ACF & PACF.
# comparison of time series objects| BLS - inflation 792 months and FRED 63 years
BLS_arima <- auto.arima(inflationseries)
FRED_arima <- auto.arima(fredcpi_ts)
summary(BLS_arima)
## Series: inflationseries
## ARIMA(3,1,5)(2,0,1)[12]
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2 ma3 ma4 ma5 sar1
## 1.4597 -0.1036 -0.3730 -1.1692 -0.0687 0.172 0.0305 0.0981 0.0078
## s.e. 0.3870 0.7354 0.3658 0.3836 0.6247 0.247 0.0565 0.0520 0.0620
## sar2 sma1
## 0.0365 -0.7363
## s.e. 0.0518 0.0494
##
## sigma^2 = 0.03905: log likelihood = 161.39
## AIC=-298.77 AICc=-298.37 BIC=-242.69
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0004879021 0.1961131 0.1423083 -0.2128334 5.133976 0.1505908
## ACF1
## Training set -0.0005730011
summary(FRED_arima)
## Series: fredcpi_ts
## ARIMA(0,1,2)
##
## Coefficients:
## ma1 ma2
## 0.1503 -0.3833
## s.e. 0.1352 0.1322
##
## sigma^2 = 2.544: log likelihood = -116.09
## AIC=238.18 AICc=238.6 BIC=244.57
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.09692927 1.55653 1.113811 -3.27011 72.95666 0.8715536 0.07255437
BLS_fcst <- forecast(BLS_arima, 108)
FRED_fcst <- forecast(FRED_arima, 10)
plot(FRED_fcst) # 10 years after 2022-01-01
FRED_fcst # to 2032 CE
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2023 7.205768 5.1617339 9.249802 4.0796876 10.33185
## 2024 6.137171 3.0215788 9.252763 1.3722840 10.90206
## 2025 6.137171 2.6493347 9.625007 0.8029856 11.47136
## 2026 6.137171 2.3131561 9.961186 0.2888448 11.98550
## 2027 6.137171 2.0042328 10.270109 -0.1836125 12.45795
## 2028 6.137171 1.7168468 10.557495 -0.6231315 12.89747
## 2029 6.137171 1.4470373 10.827305 -1.0357695 13.31011
## 2030 6.137171 1.1919266 11.082415 -1.4259276 13.70027
## 2031 6.137171 0.9493458 11.324996 -1.7969229 14.07126
## 2032 6.137171 0.7176122 11.556730 -2.1513290 14.42567
plot(BLS_fcst) # 108 months after 2023-12-31
BLS_fcst # to DEC 2032
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 2024 3.722160 3.4689053262 3.975415 3.33484026 4.109480
## Feb 2024 3.560952 3.1474762925 3.974428 2.92859527 4.193309
## Mar 2024 3.324401 2.7555984860 3.893204 2.45449257 4.194310
## Apr 2024 3.133580 2.4234423564 3.843717 2.04751837 4.219641
## May 2024 3.060525 2.2202038216 3.900846 1.77536471 4.345685
## Jun 2024 3.089253 2.1205395791 4.057967 1.60773342 4.570774
## Jul 2024 2.993333 1.8965862124 4.090080 1.31600342 4.670663
## Aug 2024 3.062009 1.8355197246 4.288498 1.18625551 4.937763
## Sep 2024 2.966587 1.6080508548 4.325124 0.88888509 5.044290
## Oct 2024 2.932305 1.4389713405 4.425639 0.64844826 5.216162
## Nov 2024 2.877174 1.2462611112 4.508087 0.38290802 5.371440
## Dec 2024 2.774160 1.0030073165 4.545313 0.06541563 5.482905
## Jan 2025 2.730693 0.8789173825 4.582469 -0.10135361 5.562740
## Feb 2025 2.696470 0.7730978350 4.619843 -0.24507399 5.638014
## Mar 2025 2.688537 0.6999666314 4.677108 -0.35271917 5.729794
## Apr 2025 2.685376 0.6325019358 4.738250 -0.45422388 5.824976
## May 2025 2.687106 0.5693240208 4.804888 -0.55176208 5.925974
## Jun 2025 2.686523 0.5052121310 4.867835 -0.64950428 6.022551
## Jul 2025 2.705842 0.4626821117 4.949002 -0.72477512 6.136460
## Aug 2025 2.721021 0.4184462158 5.023595 -0.80046305 6.242504
## Sep 2025 2.746689 0.3874891736 5.105890 -0.86139600 6.354775
## Oct 2025 2.780110 0.3673970676 5.192823 -0.90981598 6.470036
## Nov 2025 2.819783 0.3568414549 5.282725 -0.94696105 6.586527
## Dec 2025 2.857424 0.3476299030 5.367218 -0.98097484 6.695823
## Jan 2026 2.893962 0.3392549171 5.448669 -1.01312542 6.801050
## Feb 2026 2.931796 0.3351902116 5.528403 -1.03937007 6.902963
## Mar 2026 2.967112 0.3316036127 5.602621 -1.06355043 6.997775
## Apr 2026 3.003633 0.3322496371 5.675017 -1.08189539 7.089162
## May 2026 3.043526 0.3391836658 5.747868 -1.09240853 7.179460
## Jun 2026 3.085749 0.3511396365 5.820359 -1.09647529 7.267974
## Jul 2026 3.121874 0.3594952411 5.884254 -1.10281997 7.346569
## Aug 2026 3.161985 0.3741236102 5.949846 -1.10168097 7.425651
## Sep 2026 3.193972 0.3827139226 6.005230 -1.10547613 7.493420
## Oct 2026 3.225857 0.3930880448 6.058625 -1.10648906 7.558202
## Nov 2026 3.254506 0.4019228139 6.107090 -1.10814367 7.617156
## Dec 2026 3.278784 0.4079007653 6.149668 -1.11185323 7.669422
## Jan 2027 3.302574 0.4154493633 6.189699 -1.11290212 7.718050
## Feb 2027 3.324050 0.4220363539 6.226064 -1.11419699 7.762297
## Mar 2027 3.343830 0.4280934049 6.259566 -1.11540405 7.803063
## Apr 2027 3.361209 0.4327021272 6.289716 -1.11755574 7.839974
## May 2027 3.376288 0.4358122133 6.316764 -1.12078165 7.873358
## Jun 2027 3.388895 0.4371599367 6.340630 -1.12539419 7.903184
## Jul 2027 3.399891 0.4375039925 6.362279 -1.13068918 7.930472
## Aug 2027 3.408611 0.4360910872 6.381130 -1.13746571 7.954687
## Sep 2027 3.415634 0.4334211945 6.397848 -1.14526710 7.976536
## Oct 2027 3.421077 0.4295354679 6.412618 -1.15409082 7.996244
## Nov 2027 3.425015 0.4244448890 6.425586 -1.16396109 8.013992
## Dec 2027 3.427298 0.4179383106 6.436659 -1.17512073 8.029718
## Jan 2028 3.428152 0.4101694456 6.446134 -1.18745385 8.043757
## Feb 2028 3.427808 0.4013355111 6.454280 -1.20078214 8.056398
## Mar 2028 3.426289 0.3914163254 6.461162 -1.21514835 8.067727
## Apr 2028 3.423879 0.3806579667 6.467100 -1.23032580 8.078083
## May 2028 3.420803 0.3692532955 6.472352 -1.24613941 8.087745
## Jun 2028 3.417162 0.3572719089 6.477051 -1.26253589 8.096859
## Jul 2028 3.412787 0.3445184974 6.481055 -1.27972451 8.105298
## Aug 2028 3.408164 0.3314562787 6.484871 -1.29725421 8.113581
## Sep 2028 3.402970 0.3177428829 6.488197 -1.31547762 8.121417
## Oct 2028 3.397604 0.3037606650 6.491447 -1.33402099 8.129229
## Nov 2028 3.392047 0.2894781089 6.494616 -1.35292277 8.137017
## Dec 2028 3.386343 0.2749279403 6.497758 -1.37215563 8.144841
## Jan 2029 3.380710 0.2603403858 6.501080 -1.39148364 8.152904
## Feb 2029 3.375151 0.2456992550 6.504603 -1.41093243 8.161234
## Mar 2029 3.369745 0.2310811504 6.508409 -1.43042709 8.169917
## Apr 2029 3.364517 0.2165083752 6.512525 -1.44994666 8.178980
## May 2029 3.359512 0.2020268885 6.516997 -1.46944471 8.188468
## Jun 2029 3.354756 0.1876668804 6.521845 -1.48888893 8.198401
## Jul 2029 3.350304 0.1734874987 6.527120 -1.50821754 8.208825
## Aug 2029 3.346152 0.1594918125 6.532812 -1.52742404 8.219727
## Sep 2029 3.342329 0.1457166621 6.538941 -1.54646758 8.231125
## Oct 2029 3.338847 0.1321825656 6.545512 -1.56532317 8.243018
## Nov 2029 3.335709 0.1189008446 6.552517 -1.58397461 8.255393
## Dec 2029 3.332904 0.1058707850 6.559937 -1.60241748 8.268226
## Jan 2030 3.330433 0.0931024916 6.567763 -1.62063663 8.281502
## Feb 2030 3.328291 0.0806021612 6.575979 -1.63862024 8.295201
## Mar 2030 3.326463 0.0683655798 6.584561 -1.65636709 8.309293
## Apr 2030 3.324943 0.0563950309 6.593490 -1.67386949 8.323755
## May 2030 3.323717 0.0446886256 6.602745 -1.69112406 8.338558
## Jun 2030 3.322768 0.0332378031 6.612298 -1.70813431 8.353670
## Jul 2030 3.322067 0.0220228689 6.622112 -1.72491510 8.369050
## Aug 2030 3.321608 0.0110463558 6.632170 -1.74145933 8.384676
## Sep 2030 3.321356 0.0002806105 6.642431 -1.75779038 8.400502
## Oct 2030 3.321300 -0.0102764636 6.652876 -1.77390641 8.416506
## Nov 2030 3.321416 -0.0206426338 6.663475 -1.78982158 8.432653
## Dec 2030 3.321682 -0.0308336676 6.674198 -1.80554841 8.448913
## Jan 2031 3.322084 -0.0408580424 6.685026 -1.82109210 8.465260
## Feb 2031 3.322600 -0.0507331396 6.695933 -1.83646775 8.481667
## Mar 2031 3.323212 -0.0604726661 6.706896 -1.85168687 8.498110
## Apr 2031 3.323901 -0.0700917574 6.717893 -1.86676278 8.514564
## May 2031 3.324651 -0.0796040816 6.728905 -1.88170762 8.531009
## Jun 2031 3.325445 -0.0890231887 6.739914 -1.89653367 8.547424
## Jul 2031 3.326272 -0.0983608230 6.750904 -1.91125173 8.563795
## Aug 2031 3.327115 -0.1076296929 6.761860 -1.92587381 8.580104
## Sep 2031 3.327965 -0.1168405498 6.772770 -1.94041022 8.596339
## Oct 2031 3.328809 -0.1260035418 6.783622 -1.95487088 8.612489
## Nov 2031 3.329639 -0.1351282566 6.794407 -1.96926537 8.628544
## Dec 2031 3.330446 -0.1442236798 6.805116 -1.98360283 8.644495
## Jan 2032 3.331223 -0.1532973246 6.815744 -1.99789109 8.660338
## Feb 2032 3.331964 -0.1623559164 6.826285 -2.01213733 8.676066
## Mar 2032 3.332665 -0.1714055580 6.836735 -2.02634822 8.691677
## Apr 2032 3.333320 -0.1804511894 6.847091 -2.04052936 8.707170
## May 2032 3.333928 -0.1894969986 6.857354 -2.05468568 8.722542
## Jun 2032 3.334487 -0.1985465737 6.867521 -2.06882159 8.737796
## Jul 2032 3.334995 -0.2076031074 6.877593 -2.08294111 8.752931
## Aug 2032 3.335451 -0.2166682601 6.887571 -2.09704681 8.767950
## Sep 2032 3.335857 -0.2257441105 6.897457 -2.11114164 8.782855
## Oct 2032 3.336212 -0.2348311950 6.907254 -2.12522705 8.797650
## Nov 2032 3.336517 -0.2439300757 6.916965 -2.13930452 8.812339
## Dec 2032 3.336776 -0.2530407580 6.926593 -2.15337492 8.826927