#Load necessary libraries
library(tidyverse)
library(realtalk)
library(epiextractr)
library(here)
Inflation adjusting with Realtalk
Note: Users will need to install realtalk and epiextractr for this example. Refer to EPI packages for R for installation instructions.
Loading CPI Indices using the realtalk library
The following chunk of code loads the R libraries necessary for this exercise. You may need to install them to run this code.
The RealTalk package includes several datasets of common US price indices. You may view those by executing the available_price_indexes() command.
#list available cpi series
::available_price_indexes realtalk
# A tibble: 20 × 6
index_name frequency seasonal min_date max_date package_data_name
<chr> <chr> <chr> <chr> <chr> <chr>
1 C-CPI-U annual <NA> 2000 2023 c_cpi_u_annual
2 C-CPI-U monthly NSA Dec 1999 Aug 2024 c_cpi_u_monthly_nsa
3 C-CPI-U quarterly NSA 2000q1 2024q2 c_cpi_u_quarterly_nsa
4 C-CPI-U, extended annual <NA> 1937 2023 c_cpi_u_extended_annu…
5 C-CPI-U, extended monthly NSA Jan 1937 Aug 2024 c_cpi_u_extended_mont…
6 C-CPI-U, extended monthly SA Jan 1947 Aug 2024 c_cpi_u_extended_mont…
7 C-CPI-U, extended quarterly NSA 1937q1 2024q2 c_cpi_u_extended_quar…
8 C-CPI-U, extended quarterly SA 1947q1 2024q2 c_cpi_u_extended_quar…
9 CPI-U annual <NA> 1937 2023 cpi_u_annual
10 CPI-U monthly NSA Jan 1937 Aug 2024 cpi_u_monthly_nsa
11 CPI-U monthly SA Jan 1947 Aug 2024 cpi_u_monthly_sa
12 CPI-U quarterly NSA 1937q1 2024q2 cpi_u_quarterly_nsa
13 CPI-U quarterly SA 1947q1 2024q2 cpi_u_quarterly_sa
14 CPI-U-RS annual <NA> 1978 2023 cpi_u_rs_annual
15 CPI-U-RS monthly NSA Dec 1977 Dec 2023 cpi_u_rs_monthly_nsa
16 CPI-U-X1 annual <NA> 1967 1982 cpi_u_x1_annual
17 CPI-U-X1 monthly NSA Jan 1967 Dec 1982 cpi_u_x1_monthly_nsa
18 PCE annual <NA> 1929 2023 pce_annual
19 PCE monthly SA Jan 1959 Jul 2024 pce_monthly_sa
20 PCE quarterly SA 1947q1 2024q2 pce_quarterly_sa
EPI uses the CPI-U-RS series to inflation adjust wages, so we’ll select that series and assign it to a dataframe.
#this creates a dataframe with the annual CPI-U-RS index from 1937-2022
<- realtalk::cpi_u_rs_annual
cpi_data
#Set base year to 2022
<- cpi_data$cpi_u_rs[cpi_data$year==2022] cpi2022
A refresher on inflation adjustment
Before jumping into the full code. Let’s refresh on how to calculate inflation using the Consumer Price Index.
Inflation in a given year is calculated by dividing the price of a market basket in a particular year by the price of the same basket in the base year, like so:
\[ \frac{\text{Given year}}{\text{Base year}} * 100 \]
For example, let’s calculate how much the CPI-U-RS index has increased from 1978 to 2022.
\[ \frac{\text{CPI}_{2022}}{\text{CPI}_{1978}} = \frac{431.5}{104} \approx 4.149038 \text{ or } 414.9\% \]
And voila - we see that inflation has caused the basket of goods to increase 414.9% since 1978.
Applying inflation adjustment to CPS ORG wage data
This section uses epiextractr to load Current Population Survey data. Refer to the Using EPI’s CPS Extracts to learn how to use this library.
Below, I load CPS ORG data and define my sample:
<- load_org(2012:2022, year, month, orgwgt, wage, age, lfstat) %>%
org #define sample universe
filter(age>=16, lfstat %in% c(1,2))
Next, calculate median wages in the CPS. and merge the annual CPI index to the dataframe using left_join()
#Calculate median wages in the CPS ORG
<- org %>%
wage_data #use MetricsWeighted package to calculate a weighted median
#Note: I am pooling 12 months of CPS data,
#so I adjust orgwgt—the survey weight— variable, dividing it by 12.
summarize(nominal_median_wage =
::weighted_median(wage, w=orgwgt/12, na.rm=TRUE),
MetricsWeighted.by=year) %>%
#Merge annual CPI data to dataframe by year.
left_join(cpi_data, by='year')
wage_data
# A tibble: 11 × 3
year nominal_median_wage cpi_u_rs
<dbl> <dbl> <dbl>
1 2012 16 337.
2 2013 16.4 342
3 2014 16.8 348.
4 2015 17 348.
5 2016 17.5 353.
6 2017 18 360.
7 2018 18.8 369.
8 2019 19.2 376.
9 2020 21 381.
10 2021 21.4 399.
11 2022 22.9 432.
Finally, calculate the inflation rate relative to 2022, and the inflation adjusted median wage using the CPI index as follows:
<- wage_data %>%
adjusted_data
#This mutate command calculates inflation in a given year, relative to 2022
#and multiplies nominal median wages by the inflation quotient
mutate(infl_rel_to_2022 = signif(x = ((cpi2022/cpi_u_rs)-1), digits=2),
real_wage_2022 = nominal_median_wage*(cpi2022/cpi_u_rs)) %>%
relocate(infl_rel_to_2022, nominal_median_wage, .after=cpi_u_rs)
adjusted_data
# A tibble: 11 × 5
year cpi_u_rs infl_rel_to_2022 nominal_median_wage real_wage_2022
<dbl> <dbl> <dbl> <dbl> <dbl>
1 2012 337. 0.28 16 20.5
2 2013 342 0.26 16.4 20.7
3 2014 348. 0.24 16.8 20.8
4 2015 348. 0.24 17 21.1
5 2016 353. 0.22 17.5 21.4
6 2017 360. 0.2 18 21.6
7 2018 369. 0.17 18.8 21.9
8 2019 376. 0.15 19.2 22.1
9 2020 381. 0.13 21 23.8
10 2021 399. 0.081 21.4 23.1
11 2022 432. 0 22.9 22.9