This function acts as an overlay for functions that produce tabular statistics
through an interface utilizing the ModelMatrix()
function and its formula
parameter.
Each table (individual statistic) is defined by a formula. The output is a single data.frame
that contains the results for all tables.
Usage
tables_by_formulas(
data,
table_fun,
...,
table_formulas,
substitute_vars = NULL,
auto_collapse = TRUE,
collapse_vars = NULL,
total = "Total",
hierarchical_extend0 = TRUE
)
Arguments
- data
The input data to be processed by
table_fun
.- table_fun
The table-producing function to be used.
- ...
Additional arguments passed to
table_fun
.- table_formulas
A named list of formulas, where each entry defines a specific table.
- substitute_vars
Allows formulas in
table_formulas
to be written in a simplified way. Ifsubstitute_vars
is specified, the final formulas are generated usingsubstitute_formula_vars()
withsubstitute_vars
as input.- auto_collapse
Logical. If
TRUE
, variables are collapsed usingtotal_collapse()
with thevariables
parameter according tosubstitute_vars
.- collapse_vars
When specified,
total_collapse()
is called withcollapse_vars
as thevariables
parameter, after any call triggered by theauto_collapse
parameter.- total
A string used to name totals. Passed to both
table_fun
andtotal_collapse()
.- hierarchical_extend0
Controls automatic hierarchy generation for
Extend0()
. See "Details" for more information.
Details
To ensure full control over the generated output variables, table_fun
is called with avoid_hierarchical
or avoidHierarchical
set to TRUE
. Desired variables in the output are achieved using
substitute_vars
, auto_collapse
, and collapse_vars
.
If table_fun
automatically uses Extend0()
, the parameter hierarchical_extend0
specifies the hierarchical
parameter in Extend0()
via Extend0fromModelMatrixInput()
.
When hierarchical_extend0
is TRUE
, hierarchies are generated automatically.
By default, it is set to TRUE
, preventing excessive data extension and aligning with
the default behavior of Formula2ModelMatrix()
, where avoidHierarchical = FALSE
.
Note: The use of total_collapse
internally allows handling of variable names not present in the data.
This ensures flexibility when modifying the table_formulas
parameter.
Examples
tables_by_formulas(SSBtoolsData("magnitude1"),
table_fun = model_aggregate,
table_formulas = list(table_1 = ~region * sector2,
table_2 = ~region1:sector4 - 1,
table_3 = ~region + sector4 - 1),
substitute_vars = list(region = c("geo", "eu"), region1 = "eu"),
collapse_vars = list(sector = c("sector2", "sector4")),
sum_vars = "value",
total = "T")
#> [pre_aggregate 20*6->10*5] [ModelMatrix] [crossprod] [cbind]
#> region sector value table_1 table_2 table_3
#> 1 T T 462.3 TRUE FALSE FALSE
#> 2 Iceland T 37.1 TRUE FALSE TRUE
#> 3 Portugal T 162.5 TRUE FALSE TRUE
#> 4 Spain T 262.7 TRUE FALSE TRUE
#> 5 EU T 425.2 TRUE FALSE TRUE
#> 6 nonEU T 37.1 TRUE FALSE TRUE
#> 7 T private 429.5 TRUE FALSE FALSE
#> 8 T public 32.8 TRUE FALSE FALSE
#> 9 T Agriculture 240.2 FALSE FALSE TRUE
#> 10 T Entertainment 131.5 FALSE FALSE TRUE
#> 11 T Governmental 32.8 FALSE FALSE TRUE
#> 12 T Industry 57.8 FALSE FALSE TRUE
#> 13 Iceland private 37.1 TRUE FALSE FALSE
#> 14 Portugal private 138.9 TRUE FALSE FALSE
#> 15 Portugal public 23.6 TRUE FALSE FALSE
#> 16 Spain private 253.5 TRUE FALSE FALSE
#> 17 Spain public 9.2 TRUE FALSE FALSE
#> 18 EU private 392.4 TRUE FALSE FALSE
#> 19 EU public 32.8 TRUE FALSE FALSE
#> 20 nonEU private 37.1 TRUE FALSE FALSE
#> 21 EU Agriculture 240.2 FALSE TRUE FALSE
#> 22 EU Entertainment 114.7 FALSE TRUE FALSE
#> 23 EU Governmental 32.8 FALSE TRUE FALSE
#> 24 EU Industry 37.5 FALSE TRUE FALSE
#> 25 nonEU Entertainment 16.8 FALSE TRUE FALSE
#> 26 nonEU Industry 20.3 FALSE TRUE FALSE