### 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)
# rebase index to new reference period:
rebase(x=p, t=t, t.ref=c("1996","2023")) # 1996 not present so 2023 is used
rebase(x=p, t=t, t.ref=c("1996","first")) # 1996 not present so first period is used
# convert into quarterly index:
convert(x=p, t=t, type="q") # first quarter is not complete so NA
# unchaining and chaining gives initial results:
100*p/p[1]
chain(unchain(p, t, by=12), t, by=12)
# use annual overlap:
100*p/mean(p[1:12])
(res <- chain(unchain(p, t, by=NULL), t, by=NULL))
# note that for backwards compability, each month in the first
# year receives an index value of 100. this allows the same
# computation again:
chain(unchain(res, t, by=NULL), t, by=NULL)
### EXAMPLE 2: Working with published HICP data
# \donttest{
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 for euro area:
dtm <- hicp::data(id="prc_hicp_minr", filters=list(unit="I25", geo="EA"))
dtm[, "time":=as.Date(paste0(time, "-01"))]
setkeyv(x=dtm, cols=c("unit","coicop18","time"))
# unchain, chain, and rebase all euro area indices by COICOP:
dtm[, "dec_ratio" := unchain(x=values, t=time), by="coicop18"]
dtm[, "chained_index" := chain(x=dec_ratio, t=time), by="coicop18"]
dtm[, "index_own" := rebase(x=chained_index, t=time, t.ref="2025"), by="coicop18"]
# convert all monthly indices into annual averages:
dta <- dtm[, as.data.table(
x=convert(x=values, t=time, type="year"),
keep.rownames=TRUE), by="coicop18"]
setnames(x=dta, c("coicop18","time","index"))
plot(index~as.Date(time), data=dta[coicop18=="TOTAL",], type="l")
# }Run the code above in your browser using DataLab