### EXAMPLE 1
# sample monthly price index:
t <- seq.Date(from=as.Date("2022-12-01"), to=as.Date("2025-12-01"), by="1 month")
p <- rnorm(n=length(t), mean=100, sd=5)
# compute change rates:
rates(x=p, t=t, type="month") # one month to the previous month
rates(x=p, t=t, type="year") # month to the same month of previous year
# compute annual average rate of change:
pa <- convert(x=p, t=t, type="y") # now annual frequency
rates(x=pa, t=as.Date(names(pa)), type="year")
# compute 12-month average rate of change:
pmvg <- convert(x=p, t=t, type="12mavg") # still monthly frequency
rates(x=pmvg, t=t, type="year")
# \donttest{
### EXAMPLE 2: Ribe contributions using published HICP data
library(data.table)
library(restatapi)
options(restatapi_cores=1) # set cores for testing on CRAN
options(hicp.chatty=FALSE) # suppress package messages and warnings
# import monthly price indices:
dtp <- hicp::data(id="prc_hicp_minr", filters=list(unit="I25", geo="EA"))
dtp[, "time":=as.Date(paste0(time, "-01"))]
dtp[, "year":=as.integer(format(time, "%Y"))]
setnames(x=dtp, old="values", new="index")
# import item weights:
dtw <- hicp::data(id="prc_hicp_iw", filters=list(geo="EA"))
dtw[, "time":=as.integer(time)]
setnames(x=dtw, old=c("time","values"), new=c("year","weight"))
# merge price indices and item weights:
dtall <- merge(x=dtp, y=dtw, by=c("geo","coicop18","year"), all.x=TRUE)
# add all-items hicp:
dtall <- merge(x=dtall,
y=dtall[coicop18=="TOTAL", list(geo,time,index,weight)],
by=c("geo","time"), all.x=TRUE, suffixes=c("","_all"))
# Ribe contributions by COICOP:
dtall[, "ribe" := contrib(x=index, w=weight, t=time,
x.all=index_all, w.all=weight_all,
type="year", settings=list(method="ribe")), by="coicop18"]
# plot annual change rates over time:
plot(rates(x=index, t=time, type="year")~time,
data=dtall[coicop18=="TOTAL",],
type="l", ylim=c(-2,12))
# add contribution of energy to plot:
lines(ribe~time, data=dtall[coicop18=="NRG"], col="red")
# }Run the code above in your browser using DataLab