build_raking_problem {gseries} | R Documentation |
Build the elements of raking problems.
Description
(version française: https://StatCan.github.io/gensol-gseries/fr/reference/build_raking_problem.html)
This function is used internally by tsraking()
to build the elements of the raking problem. It can also
be useful to derive the cross-sectional (marginal) totals of the raking problem manually (outside of the tsraking()
context).
Usage
build_raking_problem(
data_df,
metadata_df,
data_df_name = deparse1(substitute(data_df)),
metadata_df_name = deparse1(substitute(metadata_df)),
alterability_df = NULL,
alterSeries = 1,
alterTotal1 = 0,
alterTotal2 = 0
)
Arguments
data_df |
(mandatory) Data frame (object of class "data.frame") that contains the time series data to be reconciled. It must minimally
contain variables corresponding to the component series and cross-sectional control totals specified in the
metadata data frame (argument |
metadata_df |
(mandatory) Data frame (object of class "data.frame") that describes the cross-sectional aggregation constraints
(additivity rules) for the raking problem. Two character variables must be included in the metadata data frame:
|
data_df_name |
(optional) String containing the value of argument Default value is |
metadata_df_name |
(optional) String containing the value of argument Default value is |
alterability_df |
(optional) Data frame (object of class "data.frame"), or Default value is |
alterSeries |
(optional) Nonnegative real number specifying the default alterability coefficient for the component series values. It
will apply to component series for which alterability coefficients have not already been specified in the
alterability coefficients data frame (argument Default value is |
alterTotal1 |
(optional) Nonnegative real number specifying the default alterability coefficient for the 1st dimension
cross-sectional control totals. It will apply to cross-sectional control totals for which alterability
coefficients have not already been specified in the alterability coefficients data frame (argument
Default value is |
alterTotal2 |
(optional) Nonnegative real number specifying the default alterability coefficient for the 2nd dimension
cross-sectional control totals. It will apply to cross-sectional control totals for which alterability
coefficients have not already been specified in the alterability coefficients data frame (argument
Default value is |
Details
See tsraking()
for a detailed description of time series raking problems.
The returned raking problem elements do not include the implicit component series temporal totals
when applicable (i.e., elements g
and G
only contain the cross-sectional totals info).
When the input data contains multiple periods (temporal total preservation scenario), raking problem
elements x
, c_x
, g
, c_g
and G
are constructed column by column (in "column-major order"),
corresponding to the default behaviour of R for converting objects of class "matrix" into vectors.
Note: argument validation is not performed here; it is (bluntly) assumed that the function is called
by tsraking()
where a thorough validation of the arguments is done.
Value
A list with the elements of the raking problem (excluding the implicit temporal totals):
-
x
: vector of component series initial values -
c_x
: vector of component series alterability coefficients -
comp_cols
: vector of component series (column) names -
g
: vector of cross-sectional total initial values -
c_g
: vector of cross-sectional total alterability coefficients -
tot_cols
: vector of cross-sectional total (column) names -
G
: cross-sectional total aggregation matrix (g = G %*% x
)
See Also
tsraking()
build_balancing_problem()
Examples
# Derive the 5 marginal totals of a 2 x 3 two-dimensional data cube using `tsraking()`
# metadata.
my_metadata <- data.frame(series = c("A1", "A2", "A3",
"B1", "B2", "B3"),
total1 = c(rep("totA", 3),
rep("totB", 3)),
total2 = rep(c("tot1", "tot2", "tot3"), 2))
my_metadata
# 6 periods of data with marginal totals set to `NA` (they MUST exist in the input data
# but can be `NA`).
my_data <- data.frame(A1 = c(12, 10, 12, 9, 15, 7),
B1 = c(20, 21, 15, 17, 19, 18),
A2 = c(14, 9, 8, 9, 11, 10),
B2 = c(20, 29, 20, 24, 21, 17),
A3 = c(13, 15, 17, 14, 16, 12),
B3 = c(24, 20, 30, 23, 21, 19),
tot1 = rep(NA, 6),
tot2 = rep(NA, 6),
tot3 = rep(NA, 6),
totA = rep(NA, 6),
totB = rep(NA, 6))
# Get the raking problem elements.
p <- build_raking_problem(my_data, my_metadata)
str(p)
# Calculate the 5 marginal totals for all 6 periods.
my_data[p$tot_cols] <- p$G %*% p$x
my_data