library(forecast)
library(ggplot2)
library(readxl)

Data

# Consumer price index CPI-U, "U.S. Bureau of Labor Statistics"
cpi.wide<-read_xlsx("C:\\Users\\Jaire\\OneDrive\\Desktop\\Exploratory Research\\Data\\cpi.xlsx")
head(cpi.wide)
## # A tibble: 6 × 112
##      ID month `1914` `1915` `1916` `1917` `1918` `1919` `1920` `1921` `1922`
##   <dbl> <chr>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
## 1     1 Jan      2        1    3     12.5   19.7   17.9   17     -1.6  -11.1
## 2     2 Feb      1        1    4     15.4   17.5   14.9   20.4   -5.6   -8.2
## 3     3 Mar      1        0    6.1   14.3   16.7   17.1   20.1   -7.1   -8.7
## 4     4 Apr      0        2    6     18.9   12.7   17.6   21.6  -10.8   -7.7
## 5     5 May      2.1      2    5.9   19.6   13.3   16.6   21.9  -14.1   -5.6
## 6     6 Jun      1        2    6.9   20.4   13.1   15     23.7  -15.8   -5.1
## # ℹ 101 more variables: `1923` <dbl>, `1924` <dbl>, `1925` <dbl>, `1926` <dbl>,
## #   `1927` <dbl>, `1928` <dbl>, `1929` <dbl>, `1930` <dbl>, `1931` <dbl>,
## #   `1932` <dbl>, `1933` <dbl>, `1934` <dbl>, `1935` <dbl>, `1936` <dbl>,
## #   `1937` <dbl>, `1938` <dbl>, `1939` <dbl>, `1940` <dbl>, `1941` <dbl>,
## #   `1942` <dbl>, `1943` <dbl>, `1944` <dbl>, `1945` <dbl>, `1946` <dbl>,
## #   `1947` <dbl>, `1948` <dbl>, `1949` <dbl>, `1950` <dbl>, `1951` <dbl>,
## #   `1952` <dbl>, `1953` <dbl>, `1954` <dbl>, `1955` <dbl>, `1956` <dbl>, …
nrow(cpi.wide)
## [1] 12
ncol(cpi.wide)
## [1] 112
cpi.wide$ID <- factor(cpi.wide$ID)
is.factor(cpi.wide$ID)
## [1] TRUE
library(tidyr)

cpi.long <- gather(cpi.wide, year, cpi, "1914":"2023", factor_key=TRUE)
head(cpi.long)
## # A tibble: 6 × 4
##   ID    month year    cpi
##   <fct> <chr> <fct> <dbl>
## 1 1     Jan   1914    2  
## 2 2     Feb   1914    1  
## 3 3     Mar   1914    1  
## 4 4     Apr   1914    0  
## 5 5     May   1914    2.1
## 6 6     Jun   1914    1
nrow(cpi.long)
## [1] 1320
ncol(cpi.long)
## [1] 4

Create time-series object

cpi.ts <- ts(cpi.long$cpi, frequency=12, start=c(1914,1))
summary(cpi.ts)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -15.800   1.200   2.700   3.284   4.700  23.700

Autocorrelation

plot(cpi.ts)

acf(cpi.ts, main = "ACF of CPI")

pacf(cpi.ts, main = "PACF of CPI")

library(TTR)
cpi.tscomponents <- decompose(cpi.ts)
cpi.s_adj<-cpi.ts-cpi.tscomponents$seasonal
plot(cpi.tscomponents)

plot(cpi.s_adj)

ARIMA forecasts

cpi.ts_arima <- auto.arima(cpi.ts)
cpi.s_adj_arima <- auto.arima(cpi.s_adj) # seasonally adjusted
summary(cpi.ts_arima)
## Series: cpi.ts 
## ARIMA(2,0,2)(2,0,2)[12] with non-zero mean 
## 
## Coefficients:
##          ar1      ar2      ma1      ma2   sar1     sar2     sma1    sma2
##       1.9466  -0.9472  -0.6717  -0.0790  0.826  -0.0101  -1.7518  0.7763
## s.e.  0.0152   0.0151   0.0313   0.0295  0.133   0.0356   0.1312  0.1144
##         mean
##       3.2462
## s.e.  0.8798
## 
## sigma^2 = 0.3245:  log likelihood = -1137.24
## AIC=2294.47   AICc=2294.64   BIC=2346.32
## 
## Training set error measures:
##                        ME      RMSE       MAE MPE MAPE      MASE        ACF1
## Training set 0.0006712528 0.5677221 0.3605604 NaN  Inf 0.1305503 0.002171613
summary(cpi.s_adj_arima)# seasonally adjusted
## Series: cpi.s_adj 
## ARIMA(2,0,2)(2,0,1)[12] with non-zero mean 
## 
## Coefficients:
##          ar1      ar2      ma1      ma2     sar1     sar2    sma1    mean
##       1.9399  -0.9405  -0.6783  -0.0607  -0.0411  -0.0295  -0.881  3.2337
## s.e.  0.0159   0.0159   0.0320   0.0295   0.0324   0.0319   0.016  0.7119
## 
## sigma^2 = 0.3241:  log likelihood = -1136.71
## AIC=2291.43   AICc=2291.56   BIC=2338.09
## 
## Training set error measures:
##                       ME     RMSE       MAE      MPE     MAPE      MASE
## Training set 0.001547545 0.567609 0.3612519 69.42792 174.8825 0.1308007
##                     ACF1
## Training set 0.001265064
plot(cpi.ts_arima) 

plot(cpi.s_adj_arima)# seasonally adjusted  

cpi.ts_arima_fcst <- forecast(cpi.ts_arima, 12)
cpi.s_adj_arima_fcst <- forecast(cpi.s_adj_arima, 12) # seasonally adjusted  
# Forecast, 12 months after DEC 2023
plot(cpi.ts_arima_fcst) 

cpi.ts_arima_fcst       
##          Point Forecast      Lo 80    Hi 80       Lo 95     Hi 95
## Jan 2024       3.019010  2.2889522 3.749069  1.90248269  4.135538
## Feb 2024       3.076544  1.8936149 4.259472  1.26741032  4.885677
## Mar 2024       3.233247  1.6430745 4.823420  0.80128801  5.665206
## Apr 2024       3.062032  1.0776575 5.046407  0.02719308  6.096871
## May 2024       3.263662  0.8890922 5.638231 -0.36792888  6.895252
## Jun 2024       3.282139  0.5182757 6.046003 -0.94482535  7.509104
## Jul 2024       3.299347  0.1461031 6.452591 -1.52312345  8.121818
## Aug 2024       2.969042 -0.5737732 6.511857 -2.44922620  8.387310
## Sep 2024       2.934101 -0.9981902 6.866392 -3.07981921  8.948021
## Oct 2024       3.208882 -1.1123341 7.530098 -3.39984774  9.817612
## Nov 2024       3.226618 -1.4824549 7.935692 -3.97528778 10.428525
## Dec 2024       3.132181 -1.9631547 8.227517 -4.66046251 10.924825
plot(cpi.s_adj_arima_fcst)  

cpi.s_adj_arima_fcst        
##          Point Forecast      Lo 80    Hi 80      Lo 95     Hi 95
## Jan 2024       3.171871  2.4422368 3.901506  2.0559916  4.287751
## Feb 2024       3.366625  2.1920336 4.541217  1.5702423  5.163008
## Mar 2024       3.557494  1.9786159 5.136372  1.1428082  5.972180
## Apr 2024       3.584470  1.6127720 5.556169  0.5690179  6.599923
## May 2024       3.749349  1.3883930 6.110305  0.1385783  7.360120
## Jun 2024       3.834020  1.0848391 6.583201 -0.3704895  8.038530
## Jul 2024       3.759835  0.6228194 6.896850 -1.0378162  8.557486
## Aug 2024       3.339349 -0.1849924 6.863691 -2.0506662  8.729364
## Sep 2024       3.305049 -0.6056799 7.215777 -2.6758944  9.285991
## Oct 2024       3.585503 -0.7101230 7.881129 -2.9840900 10.155096
## Nov 2024       3.592264 -1.0861937 8.270722 -3.5628195 10.747347
## Dec 2024       3.517797 -1.5408675 8.576462 -4.2187628 11.254357
plot(cpi.ts_arima_fcst, xlim = c(2020,2025))

cpi.ts_arima_fcst
##          Point Forecast      Lo 80    Hi 80       Lo 95     Hi 95
## Jan 2024       3.019010  2.2889522 3.749069  1.90248269  4.135538
## Feb 2024       3.076544  1.8936149 4.259472  1.26741032  4.885677
## Mar 2024       3.233247  1.6430745 4.823420  0.80128801  5.665206
## Apr 2024       3.062032  1.0776575 5.046407  0.02719308  6.096871
## May 2024       3.263662  0.8890922 5.638231 -0.36792888  6.895252
## Jun 2024       3.282139  0.5182757 6.046003 -0.94482535  7.509104
## Jul 2024       3.299347  0.1461031 6.452591 -1.52312345  8.121818
## Aug 2024       2.969042 -0.5737732 6.511857 -2.44922620  8.387310
## Sep 2024       2.934101 -0.9981902 6.866392 -3.07981921  8.948021
## Oct 2024       3.208882 -1.1123341 7.530098 -3.39984774  9.817612
## Nov 2024       3.226618 -1.4824549 7.935692 -3.97528778 10.428525
## Dec 2024       3.132181 -1.9631547 8.227517 -4.66046251 10.924825
plot(cpi.s_adj_arima_fcst, xlim = c(2020,2025))

cpi.s_adj_arima_fcst
##          Point Forecast      Lo 80    Hi 80      Lo 95     Hi 95
## Jan 2024       3.171871  2.4422368 3.901506  2.0559916  4.287751
## Feb 2024       3.366625  2.1920336 4.541217  1.5702423  5.163008
## Mar 2024       3.557494  1.9786159 5.136372  1.1428082  5.972180
## Apr 2024       3.584470  1.6127720 5.556169  0.5690179  6.599923
## May 2024       3.749349  1.3883930 6.110305  0.1385783  7.360120
## Jun 2024       3.834020  1.0848391 6.583201 -0.3704895  8.038530
## Jul 2024       3.759835  0.6228194 6.896850 -1.0378162  8.557486
## Aug 2024       3.339349 -0.1849924 6.863691 -2.0506662  8.729364
## Sep 2024       3.305049 -0.6056799 7.215777 -2.6758944  9.285991
## Oct 2024       3.585503 -0.7101230 7.881129 -2.9840900 10.155096
## Nov 2024       3.592264 -1.0861937 8.270722 -3.5628195 10.747347
## Dec 2024       3.517797 -1.5408675 8.576462 -4.2187628 11.254357