coicop.tree {hicp} | R Documentation |
Derive and fix COICOP tree
Description
Function tree()
derives the COICOP tree at the lowest possible level. In HICP data, this can be done separately for each reporting month and country. Consequently, the COICOP tree can differ across space and time. If needed, specifying argument by
in tree()
allows to merge the COICOP trees at the lowest possible level, e.g. to obtain a unique composition of COICOP codes over time.
Usage
tree(id, by=NULL, w=NULL, max.lvl=NULL, settings=list())
Arguments
id |
character vector of COICOP ids |
by |
vector specifying the variable to be used for merging the tree, e.g. vector of dates for merging over time or a vector of countries for merging across space. Can be |
w |
numeric weight of |
max.lvl |
integer specifying the maximum depth or deepest COICOP level allowed. If |
settings |
a list of control settings to be used. The following settings are supported:
|
Value
A logical vector of the same length as id
.
Author(s)
Sebastian Weinand
See Also
Examples
### EXAMPLE 1
# flag lowest possible level to be used as COICOP tree:
tree(id=c("01","011","012"), w=NULL) # true
tree(id=c("01","011","012"), w=c(0.2,0.08,0.12)) # true, weights add up
tree(id=c("01","011","012"), w=c(0.2,0.08,0.10)) # false, weights do not add up
# set maximum (or deepest) coicop level to 3:
tree(id=c("01","011","012","0111","0112","01121"),
w=c(0.2,0.08,0.12,0.02,0.06,0.06),
max.lvl=3)
# maximum level=3, but weights do not add up:
tree(id=c("01","011","012","0111","0112","01121"),
w=c(0.2,0.08,0.07,0.02,0.06,0.06),
max.lvl=3)
# coicop bundles:
tree(id=c("08","081","082_083"), w=c(0.25,0.05,0.2))
tree(id=c("08","081","082_083"), w=c(0.25,0.05,0.2), settings=list(unbundle=FALSE))
# merge (or fix) coicop tree over time:
tree(id=c("08","081","082","08"), by=c(1,1,1,2))
### EXAMPLE 2
# set cores for testing on CRAN:
library(restatapi)
options(restatapi_cores=1)
library(data.table)
# load hicp item weights:
coicops <- hicp.dataimport(
id="prc_hicp_inw",
filter=list(geo=c("EA","DE","FR")),
date.range=c("2005", NA))
coicops <- coicops[grepl("^CP", coicop),]
coicops[, "coicop":=gsub("^CP", "", coicop)]
# derive seperate trees for each time period and country:
coicops[, "t1" := tree(id=coicop, w=values, settings=list(w.tol=0.1)), by=c("geo","time")]
coicops[t1==TRUE,
list("n"=uniqueN(coicop), # varying coicops over time and space
"w"=sum(values, na.rm=TRUE)), # weight sums should equal 1000
by=c("geo","time")]
# derive merged trees over time, but not across countries:
coicops[, "t2" := tree(id=coicop, by=time, w=values, settings=list(w.tol=0.1)), by="geo"]
coicops[t2==TRUE,
list("n"=uniqueN(coicop), # same selection over time in a country
"w"=sum(values, na.rm=TRUE)), # weight sums should equal 1000
by=c("geo","time")]
# derive merged trees over countries and time:
coicops[, "t3" := tree(id=coicop, by=paste(geo,time), w=values, settings=list(w.tol=0.1))]
coicops[t3==TRUE,
list("n"=uniqueN(coicop), # same selection over time and across countries
"w"=sum(values, na.rm=TRUE)), # weight sums should equal 1000
by=c("geo","time")]