Learn R Programming

SSBtools (version 0.8.0)

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

Description

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

Usage

DummyHierarchy(
  mapsFrom,
  mapsTo,
  sign,
  level,
  mapsInput = NULL,
  inputInOutput = FALSE,
  keepCodes = mapsFrom[integer(0)],
  unionComplement = FALSE,
  reOrder = 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 mapsFrom not in mapsTo (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)

reOrder

When TRUE (FALSE is default) output codes are ordered differently, more similar to a usual model matrix ordering.

Value

A sparse matrix with row and column and names

Details

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

Examples

Run this code
# NOT RUN {
# 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"))
# }

Run the code above in your browser using DataLab