Learn R Programming

hicp (version 1.1.0)

index.aggregation: Index number functions and aggregation

Description

Lower-level price indices can be aggregated into higher-level indices in a single step using the bilateral index formulas below or gradually following the COICOP tree structure with the function aggregate.tree().

The functions aggregate() and disaggregate() can be used for the calculation of user-defined aggregates (e.g., HICP special aggregates). For aggregate(), lower-level indices are aggregated into the respective total. For disaggregate(), they are deducted from the total to receive a subaggregate.

Usage

# bilateral price index formulas:
jevons(x)
carli(x)
harmonic(x)
laspeyres(x, w0)
paasche(x, wt)
fisher(x, w0, wt)
toernqvist(x, w0, wt)
walsh(x, w0, wt)

# aggregation into user-defined aggregates: aggregate(x, w0, wt, id, formula=laspeyres, agg=list(), settings=list())

# disaggregation into user-defined aggregates: disaggregate(x, w0, id, agg=list(), settings=list())

# gradual aggregation following the COICOP tree: aggregate.tree(x, w0, wt, id, formula=laspeyres, settings=list())

Value

The functions jevons(), carli(), harmonic(), laspeyres(), paasche(), fisher(), toernqvist(), and walsh() return a single aggregated value.

The functions aggregate(), disaggregate() and aggregate.tree() return a data.table with the sum of weights w0 and wt (if supplied) and the computed aggregates for each index formula specified by formula.

Author

Sebastian Weinand

Arguments

x

numeric vector of price relatives between two periods, typically obtained from unchain().

w0, wt

numeric vector of weights in the base period w0 (e.g., for the Laspeyres index) or current period wt (e.g., for the Paasche index).

id

character vector of aggregate codes. For aggregate.tree(), only valid COICOP (bundle) codes are processed.

formula

function or named list of functions specifying the index formula(s) used for aggregation. Each function must return a scalar and have the argument x. For weighted index formulas, the arguments w0 and/or wt must be available as well.

agg

list of user-defined aggregates to be calculated. For disaggregate(), the list must have names specifying the aggregate from which indices are deducted. Each list element is a vector of codes that can be found in id. See settings$exact for further specification of this argument.

settings

list of control settings to be used. The following settings are supported:

  • chatty : logical indicating if package-specific warnings and info messages should be printed or not. The default is getOption("hicp.chatty").

  • coicop.version : character specifying the COICOP version to be used. See coicop for the allowed values. The default is getOption("hicp.coicop.version").

  • coicop.prefix : character specifying a prefix for the COICOP codes. The default is getOption("hicp.coicop.prefix").

  • all.items.code : character specifying the code internally used for the all-items index. The default is taken from getOption("hicp.all.items.code").

  • exact : logical indicating if the codes in agg must all be present in id for aggregation or not. If FALSE, aggregation is carried out using the codes present in agg. If TRUE and some codes cannot be found in id, NA is returned. The default is TRUE.

  • names : character of names for the aggregates in agg. If not supplied, the aggregates are numbered.

Details

The bilateral index formulas currently available are intended for the aggregation of (unchained) price relatives x. The Dutot index is therefore not implemented.

References

European Commission, Eurostat, Harmonised Index of Consumer Prices (HICP) - Methodological Manual - 2024 edition, Publications Office of the European Union, 2024, tools:::Rd_expr_doi("10.2785/055028").

See Also

unchain, tree, spec.agg

Examples

Run this code
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