balance_hierarchy: Aggregation matrix of a (possibly) unbalanced hierarchy in...

View source: R/fun_export.R

balance_hierarchyR Documentation

Aggregation matrix of a (possibly) unbalanced hierarchy in balanced form

Description

A hierarchy with L upper levels is said to be balanced if each variable at level l has at least one child at level l+1. When this doesn't hold, the hierarchy is unbalanced. This function transforms an aggregation matrix of an unbalanced hierarchy into an aggregation matrix of a balanced one. This function is used to reconcile forecasts with cslcc, which operates exclusively with balanced hierarchies.

Usage

balance_hierarchy(agg_mat, nodes = "auto", sparse = TRUE)

Arguments

agg_mat

A (n_a \times n_b) numeric matrix representing the cross-sectional aggregation matrix. It maps the n_b bottom-level (free) variables into the n_a upper (constrained) variables.

nodes

A (L \times 1) numeric vector indicating the number of variables in each of the upper L levels of the hierarchy. The default value is the string "auto" which calculates the number of variables in each level.

sparse

Option to return sparse matrices (default is TRUE).

Value

A list containing four elements:

bam

The balanced aggregation matrix.

agg_mat

The input matrix.

nodes

A (L \times 1) numeric vector indicating the number of variables in each of the L upper levels of the balanced hierarchy.

id

The identification number of each variable in the balanced hierarchy. It may contains duplicated values.

See Also

Utilities: FoReco2matrix(), aggts(), commat(), csprojmat(), cstools(), ctprojmat(), cttools(), df2aggmat(), lcmat(), recoinfo(), res2matrix(), shrink_estim(), teprojmat(), tetools(), unbalance_hierarchy()

Examples

#    Unbalanced     ->      Balanced
#        T                     T
#    |-------|             |-------|
#    A       |             A       B
#  |---|     |           |---|     |
# AA   AB    B          AA   AB    BA
A <- matrix(c(1, 1, 1,
              1, 1, 0), 2, byrow = TRUE)
obj <- balance_hierarchy(agg_mat = A, nodes = c(1, 1))
obj$bam

FoReco documentation built on Sept. 14, 2024, 9:07 a.m.