Skip to contents

Loops run_sube_pipeline()-style processing over a pre-imported sube_suts table grouped by country, year, or country-year. Each group produces a sube_pipeline_result; per-group results are preserved alongside merged tidy $summary, $tidy, and $diagnostics tables suitable for downstream analysis.

Usage

batch_sube(
  sut_data,
  cpa_map,
  ind_map,
  inputs,
  countries = NULL,
  years = NULL,
  by = c("country_year", "country", "year"),
  estimate = FALSE,
  ...
)

Arguments

sut_data

A sube_suts object (from import_suts() or read_figaro()).

cpa_map, ind_map, inputs

Correspondence tables and industry inputs; see build_matrices() and compute_sube().

countries

Optional character vector of REP codes; defaults to all countries in sut_data.

years

Optional integer vector of years; defaults to all years in sut_data.

by

Grouping key; one of "country_year" (default, per D-8.8), "country", or "year".

estimate

Forwarded per-group to the compute stage; see run_sube_pipeline() (D-8.4).

...

Forwarded per-group to build_matrices() and compute_sube().

Value

An object of class c("sube_batch_result", "list") with elements $results (named list of sube_pipeline_result, one per group), $summary (rbindlist of per-group $results$summary), $tidy (rbindlist of per-group $results$tidy), $diagnostics (rbindlist of per-group $diagnostics with an added group_key column), and $call (provenance metadata including by, n_groups, n_errors).

Details

Per D-8.7, each group's processing is wrapped in tryCatch; a failing group appends a diagnostics row with stage = "pipeline", status = "error" and the loop continues. A single summary warning() is emitted at the end if any group errored or produced non-"ok" diagnostics (per D-8.10).

Examples

sut <- sube_example_data("sut_data")
# Duplicate the sample to a second year so batch_sube has 2 groups:
sut2 <- data.table::copy(sut); sut2[, YEAR := 2021L]
#>        REP    PAR    CPA    VAR VALUE  YEAR   TYPE
#>     <char> <char> <char> <char> <int> <int> <char>
#>  1:    AAA    AAA     P1     I1    10  2021    SUP
#>  2:    AAA    AAA     P1     I2     2  2021    SUP
#>  3:    AAA    AAA     P2     I1     1  2021    SUP
#>  4:    AAA    AAA     P2     I2     8  2021    SUP
#>  5:    AAA    AAA     P1     I1     3  2021    USE
#>  6:    AAA    AAA     P1     I2     1  2021    USE
#>  7:    AAA    AAA     P1 FU_bas     6  2021    USE
#>  8:    AAA    AAA     P2     I1     2  2021    USE
#>  9:    AAA    AAA     P2     I2     2  2021    USE
#> 10:    AAA    AAA     P2 FU_bas     5  2021    USE
sut_multi <- rbind(sut, sut2)
class(sut_multi) <- c("sube_suts", class(sut_multi))

inp <- sube_example_data("inputs")
inp2 <- data.table::copy(inp); inp2[, YEAR := 2021L]
#>     YEAR    REP INDUSTRY    GO    VA   EMP   CO2
#>    <int> <char>   <char> <int> <int> <int> <int>
#> 1:  2021    AAA      I01    12     4     3     2
#> 2:  2021    AAA      I02     9     3     2     1
inp_multi <- rbind(inp, inp2)

result <- batch_sube(
  sut_data = sut_multi,
  cpa_map  = sube_example_data("cpa_map"),
  ind_map  = sube_example_data("ind_map"),
  inputs   = inp_multi
)
#> Warning: Batch completed with 0 error(s) across 2 group(s); issues: 2 inputs_misaligned. See result$diagnostics for details.
result$summary
#>     YEAR COUNTRY CPAagg       GO        VA       EMP       CO2    FD      GOe
#>    <int>  <char> <char>    <num>     <num>     <num>     <num> <num>    <num>
#> 1:  2020     AAA    P01 1.625000 0.5416667 0.3993056 0.2569444     6 3.192982
#> 2:  2020     AAA    P02 1.428571 0.4761905 0.3214286 0.1666667     5 2.339181
#> 3:  2021     AAA    P01 1.625000 0.5416667 0.3993056 0.2569444     6 3.192982
#> 4:  2021     AAA    P02 1.428571 0.4761905 0.3214286 0.1666667     5 2.339181
#>         VAe     EMPe     CO2e
#>       <num>    <num>    <num>
#> 1: 3.192982 3.324157 3.639344
#> 2: 2.339181 2.229869 1.967213
#> 3: 3.192982 3.324157 3.639344
#> 4: 2.339181 2.229869 1.967213
result$diagnostics
#>    country  year   stage            status
#>     <char> <int>  <char>            <char>
#> 1:     AAA  2020   build inputs_misaligned
#> 2:     AAA  2020 compute                ok
#> 3:     AAA  2021   build inputs_misaligned
#> 4:     AAA  2021 compute                ok
#>                                                                                            message
#>                                                                                             <char>
#> 1: Country-year AAA_2020 present in both SUT and inputs but absent from build_matrices model_data.
#> 2:                                                                                              ok
#> 3: Country-year AAA_2021 present in both SUT and inputs but absent from build_matrices model_data.
#> 4:                                                                                              ok
#>    n_rows group_key
#>     <int>    <char>
#> 1:     NA  AAA_2020
#> 2:     NA  AAA_2020
#> 3:     NA  AAA_2021
#> 4:     NA  AAA_2021