The Consumer Price Index (CPI) measures the monthly change in prices paid by U.S. consumers. The CPI is one of the most popular measures of inflation and deflation. For more details on CPI see https://www.investopedia.com/terms/c/consumerpriceindex.asp.
Inflation vs deflation
Inflation occurs when the prices of goods and services rise, while deflation occurs when those prices decrease. The balance between these two economic conditions, opposite sides of the same coin, is delicate and an economy can quickly swing from one condition to the other.
Both can be potentially bad for the economy, depending on the underlying reasons and the rate of price changes. See https://www.investopedia.com/ask/answers/111414/what-difference-between-inflation-and-deflation.asp.
IS the US on inflation?
By definition, Inflation occurs when the prices of goods and services rise. This study aims determine an answer by looking at the historical records of CPI in the US and through machine learning, build a prediction model for the next couple of months and years of possible inflationary cycle.
We will use the data from github file https://github.com/inciladc/rfiles/blob/main/datasets/CPI.csv to build our analysis and predictive model.
Data Retrieval
#Download from github repo
cpi_raw <- read.csv("https://raw.githubusercontent.com/inciladc/rfiles/main/datasets/CPI.csv")
Inspect the Data
str(cpi_raw)
## 'data.frame': 1303 obs. of 2 variables:
## $ Yearmon: chr "01-01-1913" "01-02-1913" "01-03-1913" "01-04-1913" ...
## $ CPI : num 9.8 9.8 9.8 9.8 9.7 9.8 9.9 9.9 10 10 ...
The Data is consist of two variables, Yearmon and CPI. We need to clean the data, specifically the Yearmon, isolating the month and year. This will be beneficial in our visualization and analysis in this report.
# The oldest month/year and CPI number
head(cpi_raw)
## Yearmon CPI
## 1 01-01-1913 9.8
## 2 01-02-1913 9.8
## 3 01-03-1913 9.8
## 4 01-04-1913 9.8
## 5 01-05-1913 9.7
## 6 01-06-1913 9.8
# The latest month/year and CPI number
tail(cpi_raw)
## Yearmon CPI
## 1298 01-02-2021 263.014
## 1299 01-03-2021 264.877
## 1300 01-04-2021 267.054
## 1301 01-05-2021 269.195
## 1302 01-06-2021 271.696
## 1303 01-07-2021 273.003
The data has 1303 rows which means the number of months from January 1913 to July 2021 of CPI value.
Cleaning the month/year variable
#checking the file again to clean the month/year
str(cpi_raw)
## 'data.frame': 1303 obs. of 2 variables:
## $ Yearmon: chr "01-01-1913" "01-02-1913" "01-03-1913" "01-04-1913" ...
## $ CPI : num 9.8 9.8 9.8 9.8 9.7 9.8 9.9 9.9 10 10 ...
Looking at “Yearmon”, we see that it has “chr” or character class. We need to change it as a date before we continue our Analysis.
#We will us lubridate package to clean the date
cpi_clean <- cpi_raw
cpi_clean$Yearmon <- dmy(cpi_clean$Yearmon)
str(cpi_clean)
## 'data.frame': 1303 obs. of 2 variables:
## $ Yearmon: Date, format: "1913-01-01" "1913-02-01" ...
## $ CPI : num 9.8 9.8 9.8 9.8 9.7 9.8 9.9 9.9 10 10 ...
tail(cpi_clean)
## Yearmon CPI
## 1298 2021-02-01 263.014
## 1299 2021-03-01 264.877
## 1300 2021-04-01 267.054
## 1301 2021-05-01 269.195
## 1302 2021-06-01 271.696
## 1303 2021-07-01 273.003
We have successfully update the date into Date format as Year-month-day. It is time to analyse the data.
CPI Date Coverage
cpi <- cpi_clean
nrow(cpi)
## [1] 1303
# Date from
cpi[1,]
## Yearmon CPI
## 1 1913-01-01 9.8
# Date to
cpi[nrow(cpi),]
## Yearmon CPI
## 1303 2021-07-01 273.003
Date range is from January 1913 to July 2021 or 1,303 months of CPI data.
Year/month with minimum and maximum CPI
#Minimum CPI year/mon
cpi[which.min(cpi$CPI),]
## Yearmon CPI
## 5 1913-05-01 9.7
#Maximum CPI year/mon
cpi[which.max(cpi$CPI),]
## Yearmon CPI
## 1303 2021-07-01 273.003
The year/mon with minimum CPI is May 1913 and maximum is July 2021.
Add “year only” column
cpi1 <- cpi %>% mutate(Year=year(Yearmon))
#inspect
head(cpi1)
## Yearmon CPI Year
## 1 1913-01-01 9.8 1913
## 2 1913-02-01 9.8 1913
## 3 1913-03-01 9.8 1913
## 4 1913-04-01 9.8 1913
## 5 1913-05-01 9.7 1913
## 6 1913-06-01 9.8 1913
We have successfully created a new column “Year” to be use for further annual analysis
Year to Year Observation
#Annual Average CPI
avg_cpi <- cpi1 %>% group_by(Year) %>% summarise(avg_cpi=mean(CPI))
#plot Observation
avg_cpi %>% ggplot(aes(Year,avg_cpi)) + geom_line() + ggtitle("Average Annual CPI from 1913-2021") + geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
avg_cpi %>% filter(Year>"1960" & Year<"1985") %>% ggplot(aes(Year,avg_cpi))+ geom_line() + ggtitle("A closer look at the years 1960-1985")
A closer look at the years between 1960-1985 tells us that a change in the Consumer Price Index exponentially signifies entering into Inflationary cycle. Key take always include:
*The 1970s saw some of the highest rates of inflation in the United States in recent history.
*In turn, interest rates rose to nearly 20%.
*Fed policy, the abandonment of the gold window, Keynesian economic policy, and market psychology all contributed to the high inflation.
*Rapid inflation occurs when the prices of goods and services suddenly rise, eroding the purchasing power of savings.
see https://www.investopedia.com/articles/economics/09/1970s-great-inflation.asp
Are we entering into another great inflation today?
avg_cpi %>% filter(Year>"2010") %>% ggplot(aes(Year,avg_cpi))+ geom_line() + ggtitle("Are we entering into another great inflation?")
Over the years since the great Inflation of the 70’s we have observed a steady yet increasing value of CPI. The steady movement of CPI kept inflation at bay to perhaps an “acceptable” level.
However as we end the year 2022, there are many outside factors that persist that results to increased prices in basic needs and commodities.
The government and financial institutions can only do so much to hold the prices off so we can still live within “acceptable” levels.
cpi_diff <- avg_cpi %>% summarise(difference=diff(as.matrix(avg_cpi)))
cpi_diff %>% ggplot(aes(c(1913:2020),difference)) + geom_line() + geom_hline(yintercept = 0,color="red") +ggtitle("CPI difference (Year_upper-Year_lower)") + geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
This chart will tell you that over the years, we have been playing CPI tug of war since the 70’s. Huge difference from year to year just to keep the CPI at bay to “acceptable” levels hence keeping a “great inflation” to happen again.
Using Prophet for Prediction
#store date in object ds
ds <- avg_cpi$Year
ds <- format(ds, format="%m/%d/%Y")
ds <- ymd(ds, truncated = 2L)
#store avg_cpi in object y
y <- avg_cpi$avg_cpi
#store both as data frame
df_pred <-data.frame(ds,y)
m <- prophet(df_pred)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
#set data frame and predict for the next 10 years
future <- make_future_dataframe(m,periods = 5, freq = "year")
forecast <- predict(m,future)
dyplot.prophet(m,forecast)
## Warning: `select_()` was deprecated in dplyr 0.7.0.
## Please use `select()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
summary(forecast)
## ds trend additive_terms
## Min. :1913-01-01 00:00:00 Min. : 11.30 Min. :-0.5562
## 1st Qu.:1941-04-02 06:00:00 1st Qu.: 17.32 1st Qu.:-0.5512
## Median :1969-07-02 12:00:00 Median : 37.65 Median :-0.5362
## Mean :1969-07-02 06:06:19 Mean : 92.43 Mean :-0.5187
## 3rd Qu.:1997-10-01 18:00:00 3rd Qu.:164.42 3rd Qu.:-0.5082
## Max. :2026-01-01 00:00:00 Max. :288.05 Max. :-0.4723
## additive_terms_lower additive_terms_upper yearly yearly_lower
## Min. :-0.5562 Min. :-0.5562 Min. :-0.5562 Min. :-0.5562
## 1st Qu.:-0.5512 1st Qu.:-0.5512 1st Qu.:-0.5512 1st Qu.:-0.5512
## Median :-0.5362 Median :-0.5362 Median :-0.5362 Median :-0.5362
## Mean :-0.5187 Mean :-0.5187 Mean :-0.5187 Mean :-0.5187
## 3rd Qu.:-0.5082 3rd Qu.:-0.5082 3rd Qu.:-0.5082 3rd Qu.:-0.5082
## Max. :-0.4723 Max. :-0.4723 Max. :-0.4723 Max. :-0.4723
## yearly_upper multiplicative_terms multiplicative_terms_lower
## Min. :-0.5562 Min. :0 Min. :0
## 1st Qu.:-0.5512 1st Qu.:0 1st Qu.:0
## Median :-0.5362 Median :0 Median :0
## Mean :-0.5187 Mean :0 Mean :0
## 3rd Qu.:-0.5082 3rd Qu.:0 3rd Qu.:0
## Max. :-0.4723 Max. :0 Max. :0
## multiplicative_terms_upper yhat_lower yhat_upper trend_lower
## Min. :0 Min. : 8.269 Min. : 13.21 Min. : 11.30
## 1st Qu.:0 1st Qu.: 14.294 1st Qu.: 19.34 1st Qu.: 17.32
## Median :0 Median : 34.574 Median : 39.76 Median : 37.65
## Mean :0 Mean : 89.372 Mean : 94.46 Mean : 92.41
## 3rd Qu.:0 3rd Qu.:161.096 3rd Qu.:166.40 3rd Qu.:164.42
## Max. :0 Max. :284.726 Max. :290.29 Max. :286.94
## trend_upper yhat
## Min. : 11.30 Min. : 10.74
## 1st Qu.: 17.32 1st Qu.: 16.81
## Median : 37.65 Median : 37.10
## Mean : 92.45 Mean : 91.92
## 3rd Qu.:164.42 3rd Qu.:163.87
## Max. :289.13 Max. :287.52
CPI online report by trading economics. See for comparison: https://tradingeconomics.com/united-states/consumer-price-index-cpi
In summary,
*CPI is a good baseline to determine inflationary impact in the economy as observed historically
*In the 70’s we saw an exponential increase of CPI annual average of +4 to +5 points that resulted to the “great inflation”
After the 70’s, there is a steady increase of CPI. And because of the proper balance by the government and financial institutions, kept the inflationary cycle at “acceptable” levels.
But still, the increase in CPI means gradual approach into inflationary borderline levels and it can be felt by the public through increased prices in goods, services and commodities.
*In 2021, we have predicted a CPI average of 265 where it was 267 in actual.
*In 2022, we are predicting a CPI annual average of 270 to maintain the consumer sentiment at bay. However as of August 2022 reports, we are at 290 CPI points.
*A CPI points of 290 in August of 2022 is indicative of a potential exponential increase of CPI points over the next following months.
Unless, yet again, the government and financial institution do something to level the increase.
Brace for Impact
We are nearing the end of the year 2022. As the war of Ukraine and Russia forwards on, it is taking great impacts in food and commodities. This leads to higher prices which is the actual definition of Inflation. Can the general public sustain this over time? Can the financial market keep up? We can only brace for impact for what’s to come.