假设我有一个 data.frame,其中一列是价格,另一列是年份:
prices <- rnorm(200, mean=10, sd=3)
years <- round(rnorm(200, mean=2006, sd=5))
df <- data.frame(prices, years)
现在说我想使用消费者价格指数将所有这些价格标准化为 2008 年美元。我可以查看转换值并手动进行计算,但我的直觉告诉我可能有一个包可以自动执行此操作。搜索 r-seek 和 cran 没有产生任何明显的结果。
有没有人知道什么?
最佳答案
您可以从 FRED 获取 CPI 数据使用FRED
getSymbols
的方法功能在
quantmod包裹
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"
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.
(对于这个答案,我将忽略 above quote 的第二部分 ...)
因此,计算年平均值
avg.cpi <- apply.yearly(CPIAUCSL, mean)
然后将所有指数水平除以基价以创建转换因子
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
关于r - 通胀调整价格套餐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590180/