Inflation adjusted prices package?
There is a much simpler solution for acquiring the annual CPI (e.g., CPIAUCSL) that does not require use of the quantmod
package, which seems to always have compatibility issues for one reason or another, at least in my experience.
require(lubridate) || install.packages("lubridate")
require(dplyr) || install.packages("dplyr")
monthly_cpi <-
read.table("http://research.stlouisfed.org/fred2/data/CPIAUCSL.txt",
skip = 53, header = TRUE)
monthly_cpi$cpi_year <- year(monthly_cpi$DATE)
yearly_cpi <- monthly_cpi %.% group_by(cpi_year) %.% summarize(cpi = mean(VALUE))
Then, to create your adjustment factor relative to say, last year's prices:
yearly_cpi$adj_factor <- yearly_cpi$cpi/yearly_cpi$cpi[yearly_cpi$cpi_year == 2013]
You have to find out how many lines to skip
, but then again, that causes you to actually look at the lines that are skipped by viewing the actual data source, which happens to have useful preamble information.
BUT WAIT! THERE'S MORE!
Thanks to @GSee (who gave the checked answer) for noting that there is a .csv
version for which you need not skip any rows! Using this version, the code is:
require(lubridate) || install.packages("lubridate")
require(dplyr) || install.packages("dplyr")
monthly_cpi <-
read.csv("http://research.stlouisfed.org/fred2/data/CPIAUCSL.csv", header = TRUE)
monthly_cpi$cpi_year <- year(monthly_cpi$DATE)
yearly_cpi <- monthly_cpi %.% group_by(cpi_year) %.% summarize(cpi = mean(VALUE))
yearly_cpi$adj_factor <- yearly_cpi$cpi/yearly_cpi$cpi[yearly_cpi$cpi_year == 2013]
Use priceR
, like so:
library(priceR)
set.seed(123)
prices <- rnorm(10, mean=10, sd=3)
years <- round(rnorm(10, mean=2006, sd=5))
df <- data.frame(years, nominal_prices)
df$in_2008_dollars <- adjust_for_inflation(prices, years, "US", to_date = 2008)
df
## years nominal_prices in_2008_dollars
## 1 2012 8.31857 7.66782
## 2 2008 9.30947 9.30947
## 3 2008 14.67612 14.67612
## 4 2007 10.21153 10.60356
## 5 2003 10.38786 12.15782
## 6 2015 15.14519 13.26473
## 7 2008 11.38275 11.38275
## 8 1996 6.20482 8.51713
## 9 2010 7.93944 7.67319
## 10 2004 8.66301 9.87471
Notes
- Example from here
- This uses CPI data from World Bank
adjust_for_inflation()
works for all countries. Useshow_countries()
for more info.
You can get CPI data from FRED using the
FRED
method of the getSymbols
function in the
quantmod package
getSymbols("CPIAUCSL", src='FRED') #Consumer Price Index for All Urban Consumers: All Items
#[1] "CPIAUCSL"
tail(CPIAUCSL)
# CPIAUCSL
#2012-03-01 229.098
#2012-04-01 229.177
#2012-05-01 228.527
#2012-06-01 228.618
#2012-07-01 228.723
#2012-08-01 230.102
# make an `xts` object of prices
set.seed(1)
p <- xts(rnorm(63, mean=10, sd=3), seq(from=as.Date('1950-12-01'), by='years', length.out=63))
colnames(p) <- "price"
The CPI inflation calculator at the BLS
... uses the average Consumer Price Index for a given calendar year... For the current year, the latest monthly index value is used.
(For this answer, I'm going to ignore the second part of the above quote...)
So, calculate an annual average
avg.cpi <- apply.yearly(CPIAUCSL, mean)
Then divide all index levels by the base price to create a conversion factor
cf <- avg.cpi/as.numeric(avg.cpi['2008']) #using 2008 as the base year
dat <- merge(p, cf, all=FALSE)
dat$adj <- dat[, 1] * dat[, 2]
tail(dat)
# price CPIAUCSL adj
#2006-12-01 8.898336 0.9363693 8.332128
#2007-12-01 6.867596 0.9632483 6.615200
#2008-12-01 11.709159 1.0000000 11.709159
#2009-12-01 9.594836 0.9967933 9.564069
#2010-12-01 17.204853 1.0131453 17.431015
#2011-12-01 9.882280 1.0449769 10.326754