Learn R Programming

hicp (version 1.1.0)

tree: COICOP tree

Description

Following the tree structure of COICOP, the function tree() derives from a given set of COICOP codes those at the lowest possible level. This can be particularly useful for aggregating from the lowest to the highest level in a single step.

Usage

tree(id, by=NULL, w=NULL, flag=FALSE, settings=list())

Value

Either a list (for flag=FALSE) or a logical vector of the same length as id (for flag=TRUE).

Author

Sebastian Weinand

Arguments

id

character vector of COICOP codes.

by

vector specifying the variable to be used for merging the derived COICOP codes, e.g., a vector of dates to obtain the same composition of COICOP codes over time. If by=NULL, no merging is performed.

w

numeric weight of id. If supplied, it is checked that the weights of children add up to the weight of their parent (allowing for tolerance settings$w.tol). If w=NULL, no checking of weight aggregation is performed.

flag

logical specifying the function output. For FALSE, a list with the COICOP codes at each level. For TRUE, a logical vector of the same length as id indicating which COICOP codes in id define the lowest level.

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").

  • max.lvl : integer specifying the maximum (or deepest) COICOP level allowed. If NULL (the default), the maximum level found in id is used.

  • w.tol : numeric tolerance for checking of weights. Only relevant if w is not NULL. The default is 1/100.

Details

The derivation of COICOP codes at the lowest level follows a top-down-approach. Starting from the top level of the COICOP tree (usually the all-items code), it is checked if

  1. the code has children in id,

  2. the children's weights correctly add up to the weight of the parent (if w provided),

  3. all children can be found in all the groups in by (if by provided).

Only if all three conditions are met, the children are stored and further processed following the same three steps. Otherwise, the parent is kept and the processing stops in the respective node. This process is followed until the lowest level of all codes is reached.

If by is provided, the function tree() first subsets all codes in id to the intersecting levels. This ensures that the derivation of the COICOP codes does not directly stops if, for example, the all-items code is missing in one of the groups in by. For example, assume the codes(00,01,02,011,012,021) for by=1 and (01,011,012,021) for by=2. In this case, the code 00 would be dropped internally first because its level is not available for by=2. The other codes would be processed since their levels intersect across by. However, since (01,02) do not fulfill the third check, the derivation would stop and no merged tree would be available though codes (011,012,021) seem to be a solution.

See Also

child, coicop, parent

Examples

Run this code
# example codes:
ids <- c("CP01","CP011","CP012","CP0111","CP0112")

# derive lowest level of COICOP tree from top to bottom:
tree(ids) # (CP0111,CP0112,CP012) at lowest level

# or just flag lowest level:
tree(ids, flag=TRUE) 

# still same codes because weights add up:
tree(id=ids, w=c(0.2,0.08,0.12,0.05,0.03)) 

# now (CP011,CP012) because weights do not correctly add up at lower levels:
tree(id=ids, w=c(0.2,0.08,0.12,0.05,0.01)) 

# again (CP011,CP012) because maximum COICOP level limited to 3 digits:
tree(id=c(ids,"01121"),
     w=c(0.2,0.08,0.12,0.02,0.06,0.06),
     settings=list(max.lvl=3)) 

# merge (or fix) COICOP tree over groups:
tree(id=c("TOTAL","CP01","CP02","CP011","CP012", 
          "TOTAL","CP01","CP02","CP011"), 
     by=c(1,1,1,1,1, 2,2,2,2),
     w=c(1,0.3,0.7,0.12,0.18, 1,0.32,0.68,0.15))
# for by=1, the lowest level would be (CP011,CP012,CP02).
# however, CP012 is missing for by=2. therefore, the merged 
# COICOP tree consists of (CP01,CP02) at the lowest level.

Run the code above in your browser using DataLab