DummyHierarchy: Converting hierarchy specifications to a (signed) dummy...

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/HierarchyCompute.R

Description

A matrix for mapping input codes (columns) to output codes (rows) are created.

Usage

1
2
3
DummyHierarchy(mapsFrom, mapsTo, sign, level, mapsInput = NULL,
  inputInOutput = FALSE, keepCodes = mapsFrom[integer(0)],
  unionComplement = FALSE)

Arguments

mapsFrom

Character vector from hierarchy table

mapsTo

Character vector from hierarchy table

sign

Numeric vector of either 1 or -1 from hierarchy table

level

Numeric vector from hierarchy table

mapsInput

All codes in mapsTo not in mapsFrom (created automatically when NULL) and possibly other codes in input data.

inputInOutput

When FALSE all output rows represent codes in mapsTo

keepCodes

To prevent some codes to be removed when inputInOutput = TRUE

unionComplement

When TRUE, sign means union and complement instead of addition or subtraction (see note)

Details

The elements of the matrix specify how columns contribute to rows.

Value

A sparse matrix with row and column and names

Note

With unionComplement = FALSE (default), the sign of each mapping specifies the contribution as addition or subtraction. Thus, values above one and negative values in output can occur. With unionComplement = TRUE, positive is treated as union and negative as complement. Then 0 and 1 are the only possible elements in the output matrix.

Author(s)

Øyvind Langsrud

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# A hierarchy table
h <- SSBtoolsData("FIFA2018ABCD")

DummyHierarchy(h$mapsFrom, h$mapsTo, h$sign, h$level)
DummyHierarchy(h$mapsFrom, h$mapsTo, h$sign, h$level, inputInOutput = TRUE)
DummyHierarchy(h$mapsFrom, h$mapsTo, h$sign, h$level, keepCodes = c("Portugal", "Spain"))

# Extend the hierarchy table to illustrate the effect of unionComplement
h2 <- rbind(data.frame(mapsFrom = c("EU", "Schengen"), mapsTo = "EUandSchengen", 
                       sign = 1, level = 3), h)

DummyHierarchy(h2$mapsFrom, h2$mapsTo, h2$sign, h2$level)
DummyHierarchy(h2$mapsFrom, h2$mapsTo, h2$sign, h2$level, unionComplement = TRUE)

#' # Extend mapsInput - leading to zero columns.
DummyHierarchy(h$mapsFrom, h$mapsTo, h$sign, h$level,
               mapsInput = c(h$mapsFrom[!(h$mapsFrom %in% h$mapsTo)], "Norway", "Finland"))

statisticsnorway/SSBtools documentation built on Nov. 26, 2019, 9:41 p.m.