Cmatrix: Cross-sectional (contemporaneous) aggregation matrix

Description Usage Arguments Value See Also Examples

View source: R/Cmatrix.R

Description

\loadmathjax

This function allows the user to easily build the (\mjseqnn_a \times n_b) cross-sectional (contemporaneous) matrix mapping the \mjseqnn_b bottom level series into the \mjseqnn_a higher level ones. (Experimental version)

Usage

1
Cmatrix(formula, data, sep = "_", sparse = TRUE, top_label = "Total")

Arguments

formula

Specification of the hierarchical structure: grouped hierarchies are specified using ~ g1 * g2 and nested hierarchies are specified using ~ parent / child. Mixtures of the two formulations are also possible, like ~ g1 * (grandparent / parent / child).

data

A dataset in which each column contains the values of the variables in the formula and each row identifies a bottom level time series.

sep

Character to separate the names of the aggregated series (default is "_").

sparse

Option to return sparse matrix (default is TRUE).

top_label

Label of the top level variable (default is "Total").

Value

A (na x nb) matrix.

See Also

Other utilities: FoReco2ts(), commat(), ctf_tools(), hts_tools(), oct_bounds(), score_index(), shrink_estim(), srref(), thf_tools(), ut2c()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## Balanced hierarchy
#         T
#    |--------|
#    A        B
#  |---|   |--|--|
# AA   AB  BA BB BC
# Names of the bottom level variables
data_bts <- data.frame(X1 = c("A", "A", "B", "B", "B"),
                       X2 = c("A", "B", "A", "B", "C"),
                       stringsAsFactors = FALSE)
# Cross-sectional aggregation matrix
C <- Cmatrix(~ X1 / X2, data_bts, sep = "")

## Unbalanced hierarchy (1)
#             T
#    |--------|------|
#    A        B      C
#  |---|   |--|--|
# AA   AB  BA BB BC
# Names of the bottom level variables
data_bts <- data.frame(X1 = c("A", "A", "B", "B", "B", "C"),
                       X2 = c("A", "B", "A", "B", "C", NA),
                       stringsAsFactors = FALSE)
# Cross-sectional aggregation matrix
C <- Cmatrix(~ X1 / X2, data_bts, sep = "")

## Unbalanced hierarchy (2)
#                 T
#       |---------|---------|
#       A         B         C
#     |---|     |---|     |---|
#    AA   AB   BA   BB   CA   CB
#  |----|         |----|
# AAA  AAB       BBA  BBB
# Names of the bottom level variables
data_bts <- data.frame(X1 = c("A", "A", "A", "B", "B", "B", "C", "C"),
                       X2 = c("A", "A", "B", "A", "B", "B", "A", "B"),
                       X3 = c("A", "B", NA, NA, "A", "B", NA, NA),
                       stringsAsFactors = FALSE)
# Cross-sectional aggregation matrix
C <- Cmatrix(~ X1 / X2 / X3, data_bts, sep = "")

## Grouped hierarchy
#         C               S
#    |--------|      |--------|
#    A        B      M        F
#  |---|    |---|
# AA   AB  BA   BB
# Names of the bottom level variables
data_bts <- data.frame(X1 = c("A", "A", "B", "B", "A", "A", "B", "B"),
                       X2 = c("A", "B", "A", "B", "A", "B", "A", "B"),
                       Y1 = c("M", "M", "M", "M", "F", "F", "F", "F"),
                       stringsAsFactors = FALSE)
# Cross-sectional aggregation matrix
C <- Cmatrix(~ Y1 * (X1 / X2), data_bts, sep = "")

FoReco documentation built on July 23, 2021, 5:06 p.m.