This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

#install.packages("quantmod")
#install.packages("forecast")

Load data

The CPI data is available at the St. Louis Federal Reserve. We can load the data directly into R.

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
cpi<-getSymbols(Symbols = "CPILFESL",src="FRED",auto.assign=FALSE)
cpi$dCPI<-Delt(cpi[,1],k=12,type="log")
cpi$LdCPI<-Lag(cpi$dCPI,k=12)

Summary statistics

print(summary(cpi))
##      Index               CPILFESL           dCPI              LdCPI         
##  Min.   :1957-01-01   Min.   : 28.50   Min.   :0.006009   Min.   :0.006009  
##  1st Qu.:1974-01-08   1st Qu.: 46.98   1st Qu.:0.019868   1st Qu.:0.019784  
##  Median :1991-01-16   Median :139.85   Median :0.027466   Median :0.026882  
##  Mean   :1991-01-15   Mean   :139.79   Mean   :0.035813   Mean   :0.035857  
##  3rd Qu.:2008-01-24   3rd Qu.:213.90   3rd Qu.:0.045260   3rd Qu.:0.045911  
##  Max.   :2025-02-01   Max.   :325.48   Max.   :0.127553   Max.   :0.127553  
##                                        NA's   :12         NA's   :24
plot(cpi$dCPI,main="Proportional change in CPI")

Modeling inflation

How well does the model forecast for the November, 2018 to October, 2019 time period?

library(forecast)
inflation_model19<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2018-11-01")])
print(inflation_model19)
## Series: cpi$dCPI[index(cpi) < as.Date("2018-11-01")] 
## ARIMA(1,1,3) 
## 
## Coefficients:
##          ar1      ma1     ma2      ma3
##       0.8339  -0.6510  0.1144  -0.1199
## s.e.  0.0614   0.0721  0.0462   0.0457
## 
## sigma^2 = 5.146e-06:  log likelihood = 3406.09
## AIC=-6802.19   AICc=-6802.1   BIC=-6779.23
window19<-seq(as.Date("2018-11-01"),length=12,by="1 months")
cpi$f19_dCPI_LB<-NA
cpi$f19_dCPI_UB<-NA
cpi$f19_dCPI_MN<-NA
cpi$f19_dCPI_LB[window19]<-as.numeric(forecast(inflation_model19,h=12)[["lower"]][,1])
cpi$f19_dCPI_UB[window19]<-as.numeric(forecast(inflation_model19,h=12)[["upper"]][,1])
cpi$f19_dCPI_MN[window19]<-as.numeric(forecast(inflation_model19,h=12)[["mean"]])

plot(cpi[window19,c(2,4,5,6)],
     main="Inflation: Nov 2018 - Oct 2019",
     ylim=range(na.omit(c(as.numeric(cpi$dCPI[window19]),
                          as.numeric(cpi$f19_dCPI_LB[window19]),
                          as.numeric(cpi$f19_dCPI_UB[window19])))))

How well does the model forecast from November, 2019 to October, 2020 time period?

inflation_model20<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2019-11-01")])
print(inflation_model20)
## Series: cpi$dCPI[index(cpi) < as.Date("2019-11-01")] 
## ARIMA(1,1,3) 
## 
## Coefficients:
##          ar1      ma1     ma2      ma3
##       0.8337  -0.6507  0.1147  -0.1205
## s.e.  0.0612   0.0718  0.0458   0.0453
## 
## sigma^2 = 5.072e-06:  log likelihood = 3467.5
## AIC=-6924.99   AICc=-6924.91   BIC=-6901.95
window20<-seq(as.Date("2019-11-01"),length=12,by="1 months")
cpi$f20_dCPI_LB<-NA
cpi$f20_dCPI_UB<-NA
cpi$f20_dCPI_MN<-NA

cpi$f20_dCPI_LB[window20]<-as.numeric(forecast(inflation_model20,h=12)[["lower"]][,1])
cpi$f20_dCPI_UB[window20]<-as.numeric(forecast(inflation_model20,h=12)[["upper"]][,1])
cpi$f20_dCPI_MN[window20]<-as.numeric(forecast(inflation_model20,h=12)[["mean"]])


plot(cpi[window20,c(2,7,8,9)],
     main="Inflation: Nov 2019 - Oct 2020",
     ylim=range(na.omit(c(as.numeric(cpi$dCPI[window20]),
                          as.numeric(cpi$f20_dCPI_LB[window20]),
                          as.numeric(cpi$f20_dCPI_UB[window20])))))

How well does the model forecast from November, 2020 to October, 2021 time period?

inflation_model21<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2020-11-01")])
print(inflation_model21)
## Series: cpi$dCPI[index(cpi) < as.Date("2020-11-01")] 
## ARIMA(1,1,3) with drift 
## 
## Coefficients:
##          ar1      ma1     ma2      ma3  drift
##       0.8340  -0.6435  0.1056  -0.1240  0e+00
## s.e.  0.0626   0.0728  0.0456   0.0453  2e-04
## 
## sigma^2 = 5.095e-06:  log likelihood = 3522.46
## AIC=-7032.92   AICc=-7032.81   BIC=-7005.18
window21<-seq(as.Date("2020-11-01"),length=12,by="1 months")
cpi$f21_dCPI_LB<-NA
cpi$f21_dCPI_UB<-NA
cpi$f21_dCPI_MN<-NA

cpi$f21_dCPI_LB[window21]<-as.numeric(forecast(inflation_model21,h=12)[["lower"]][,1])
cpi$f21_dCPI_UB[window21]<-as.numeric(forecast(inflation_model21,h=12)[["upper"]][,1])
cpi$f21_dCPI_MN[window21]<-as.numeric(forecast(inflation_model21,h=12)[["mean"]])


plot(cpi[window21,c(2,10,11,12)],
     main="Inflation: Nov 2020 - Oct 2021",
     ylim=range(na.omit(c(as.numeric(cpi$dCPI[window21]),
                          as.numeric(cpi$f21_dCPI_LB[window21]),
                          as.numeric(cpi$f21_dCPI_UB[window21])))))

How well does the model forecast from November, 2021 to October, 2022 time period?

inflation_model22<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2021-11-01")])
print(inflation_model22)
## Series: cpi$dCPI[index(cpi) < as.Date("2021-11-01")] 
## ARIMA(2,1,3) 
## 
## Coefficients:
##          ar1     ar2      ma1     ma2      ma3
##       0.8114  0.0261  -0.5895  0.0654  -0.1460
## s.e.  0.1819  0.1367   0.1783  0.1052   0.0458
## 
## sigma^2 = 5.366e-06:  log likelihood = 3558.75
## AIC=-7105.5   AICc=-7105.39   BIC=-7077.66
window22<-seq(as.Date("2021-11-01"),length=12,by="1 months")
cpi$f22_dCPI_LB<-NA
cpi$f22_dCPI_UB<-NA
cpi$f22_dCPI_MN<-NA

cpi$f22_dCPI_LB[window22]<-as.numeric(forecast(inflation_model22,h=12)[["lower"]][,1])
cpi$f22_dCPI_UB[window22]<-as.numeric(forecast(inflation_model22,h=12)[["upper"]][,1])
cpi$f22_dCPI_MN[window22]<-as.numeric(forecast(inflation_model22,h=12)[["mean"]])


plot(cpi[window22,c(2,13,14,15)],
     main="Inflation: Nov 2021 - Oct 2022",
     ylim=range(na.omit(c(as.numeric(cpi$dCPI[window22]),
                          as.numeric(cpi$f22_dCPI_LB[window22]),
                          as.numeric(cpi$f22_dCPI_UB[window22])))))

How well does the model forecast from November, 2022 to October, 2023 time period?

inflation_model23<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2022-11-01")])
print(inflation_model23)
## Series: cpi$dCPI[index(cpi) < as.Date("2022-11-01")] 
## ARIMA(0,1,3) 
## 
## Coefficients:
##          ma1     ma2     ma3
##       0.2444  0.2737  0.0668
## s.e.  0.0365  0.0379  0.0335
## 
## sigma^2 = 5.419e-06:  log likelihood = 3609.67
## AIC=-7211.34   AICc=-7211.29   BIC=-7192.72
window23<-seq(as.Date("2022-11-01"),length=12,by="1 months")
cpi$f23_dCPI_LB<-NA
cpi$f23_dCPI_UB<-NA
cpi$f23_dCPI_MN<-NA

cpi$f23_dCPI_LB[window23]<-as.numeric(forecast(inflation_model23,h=12)[["lower"]][,1])
cpi$f23_dCPI_UB[window23]<-as.numeric(forecast(inflation_model23,h=12)[["upper"]][,1])
cpi$f23_dCPI_MN[window23]<-as.numeric(forecast(inflation_model23,h=12)[["mean"]])


plot(cpi[window23,c(2,16,17,18)],
     main="Inflation: Nov 2022 - Oct 2023",
     ylim=range(na.omit(c(as.numeric(cpi$dCPI[window23]),
                          as.numeric(cpi$f23_dCPI_LB[window23]),
                          as.numeric(cpi$f23_dCPI_UB[window23])))))

How well does the model forecast from November, 2023 to October, 2024 time period?

inflation_model24<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2023-11-01")])
print(inflation_model24)
## Series: cpi$dCPI[index(cpi) < as.Date("2023-11-01")] 
## ARIMA(0,1,3) with drift 
## 
## Coefficients:
##          ma1     ma2     ma3  drift
##       0.2508  0.2769  0.0685  0e+00
## s.e.  0.0361  0.0375  0.0333  1e-04
## 
## sigma^2 = 5.388e-06:  log likelihood = 3668.21
## AIC=-7326.43   AICc=-7326.35   BIC=-7303.07
window24<-seq(as.Date("2023-11-01"),length=12,by="1 months")
cpi$f24_dCPI_LB<-NA
cpi$f24_dCPI_UB<-NA
cpi$f24_dCPI_MN<-NA

cpi$f24_dCPI_LB[window24]<-as.numeric(forecast(inflation_model24,h=12)[["lower"]][,1])
cpi$f24_dCPI_UB[window24]<-as.numeric(forecast(inflation_model24,h=12)[["upper"]][,1])
cpi$f24_dCPI_MN[window24]<-as.numeric(forecast(inflation_model24,h=12)[["mean"]])


plot(cpi[window24,c(2,19,20,21)],
     main="Inflation: Nov 2023 - Oct 2024",
     ylim=range(na.omit(c(as.numeric(cpi$dCPI[window24]),
                          as.numeric(cpi$f24_dCPI_LB[window24]),
                          as.numeric(cpi$f24_dCPI_UB[window24])))))

Forecasting for the next year

inflation_model25<-auto.arima(y=cpi$dCPI[index(cpi)<as.Date("2024-11-01")])
print(inflation_model25)
## Series: cpi$dCPI[index(cpi) < as.Date("2024-11-01")] 
## ARIMA(0,1,3) 
## 
## Coefficients:
##          ma1     ma2     ma3
##       0.2513  0.2763  0.0682
## s.e.  0.0358  0.0371  0.0330
## 
## sigma^2 = 5.313e-06:  log likelihood = 3729.05
## AIC=-7450.1   AICc=-7450.05   BIC=-7431.36
window25<-seq(as.Date("2024-11-01"),length=12,by="1 months")

f25_dCPI_LB<-as.numeric(forecast(inflation_model25,h=12)[["lower"]][,1])
f25_dCPI_UB<-as.numeric(forecast(inflation_model25,h=12)[["upper"]][,1])
f25_dCPI_MN<-as.numeric(forecast(inflation_model25,h=12)[["mean"]])

cpiF<-xts(x=data.frame(f25_dCPI_LB,f25_dCPI_UB,f25_dCPI_MN),order.by=window25)

plot(cpiF[window25,],
     main="Inflation: Nov 2024 - Oct 2025",
     ylim=range(na.omit(c(as.numeric(cpiF$f25_dCPI_LB[window25]),
                          as.numeric(cpiF$f25_dCPI_UB[window25])))))