library(data.table)
### EXAMPLE 1
# example data with unchained prices and weights:
dt <- data.table("coicop"=c("CP0111","CP0112","CP012","CP021","CP022"),
"price"=c(102,105,99,109,115)/100,
"weight"=c(0.2,0.15,0.4,0.2,0.05))
# aggregate directly into overall index:
dt[, laspeyres(x=price, w0=weight)]
# same result at top level with gradual aggregation:
(dtagg <- dt[, aggregate.tree(x=price, w0=weight, id=coicop)])
# compute user-defined aggregates by disaggregation:
dtagg[, disaggregate(x=laspeyres, w0=w0, id=id,
agg=list("TOTAL"=c("CP01"), "TOTAL"=c("CP022")),
settings=list(names=c("A","B")))]
# which can be similarly derived by aggregation:
dtagg[, aggregate(x=laspeyres, w0=w0, id=id,
agg=list(c("CP021","CP022"), c("CP011","CP012","CP021")),
settings=list(names=c("A","B")))]
# same aggregates by several index formulas:
dtagg[, aggregate(x=laspeyres, w0=w0, id=id,
agg=list(c("CP021","CP022"), c("CP011","CP012","CP021")),
formula=list("lasp"=laspeyres, "jev"=jevons, "mean"=mean),
settings=list(names=c("A","B")))]
# no aggregation if one index is missing:
dtagg[, aggregate(x=laspeyres, w0=w0, id=id,
agg=list(c("CP01","CP02","CP03")),
settings=list(exact=TRUE))]
# or just use the available ones:
dtagg[, aggregate(x=laspeyres, w0=w0, id=id,
agg=list(c("CP01","CP02","CP03")),
settings=list(exact=FALSE))]
### EXAMPLE 2: Index aggregation using published HICP data
# \donttest{
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 since 2014:
dtp <- hicp::data(id="prc_hicp_minr",
date.range=c("2014-12", NA),
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")
# unchain all indices for aggregation:
dtp[, "dec_ratio" := unchain(x=index, t=time), by="coicop18"]
# import euro area item weights since 2014:
dtw <- hicp::data(id="prc_hicp_iw",
date.range=c("2014",NA),
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)
dtall <- dtall[year <= year(Sys.Date())-1,]
# derive COICOP tree at lowest possible level:
dtall[weight>0 & !is.na(dec_ratio),
"tree":=tree(id=coicop18, w=weight, flag=TRUE, settings=list(w.tol=0.1)),
by="time"]
# except for rounding, we receive total weight of 1000 in each period:
dtall[tree==TRUE, sum(weight), by="time"]
# (1) compute all-items HICP in one step using only lowest-level indices:
hicp.own <- dtall[tree==TRUE,
list("laspey"=laspeyres(x=dec_ratio, w0=weight)),
by="time"]
setorderv(x=hicp.own, cols="time")
hicp.own[, "chain_laspey" := chain(x=laspey, t=time, by=12)]
hicp.own[, "chain_laspey_25" := rebase(x=chain_laspey, t=time, t.ref="2025")]
# (2) compute all-items HICP gradually through all higher-levels:
hicp.own.all <- dtall[weight>0 & !is.na(dec_ratio),
aggregate.tree(x=dec_ratio, w0=weight, id=coicop18),
by="time"]
setorderv(x=hicp.own.all, cols="time")
hicp.own.all[, "chain_laspey" := chain(x=laspeyres, t=time, by=12), by="id"]
hicp.own.all[, "chain_laspey_25" := rebase(x=chain_laspey, t=time, t.ref="2025"), by="id"]
# (3) compare all-items HICP from direct and gradual aggregation:
all(abs(hicp.own.all[id=="TOTAL", chain_laspey_25]-hicp.own$chain_laspey_25)<0.1)
# no differences -> consistent in aggregation
# }Run the code above in your browser using DataLab