Skip to contents

Overview

lx_summary_for_cohort_set() is a temporary solution that recreates the spreadsheet summaries used in earlier projects. The naming and interface will change, so treat this as a stopgap rather than a stable API.

You need two components:

  1. A list of models with metadata labels. The summary uses model statement metadata for display; if statement is missing, it falls back to experiment.
  2. A cohort set created with g6tr.cohorts::create_cohort_set().

The examples below reuse the parsing and multi-model workflow from the modelling vignette and add synthetic statements for each experiment.

Prepare multiple models and statements

Start with parsed list-experiment data:

parsed <- lx_parse_data(
  example_srv_lx,
  n_insens = 4,
  id_col = response_id,
  treatment = m2_list_treatment,
  private = "m2_list_{exp}_private",
  public = "m3_list_{exp}_public",
  majority = "m3_list_{exp}_majority",
  predictors = starts_with("d_"),
  treatment_coding = c(control = "ctrl", treatment = "trt")
)

Because the example data are synthetic, we create placeholder statements and attach them as metadata to each parsed lx_data object:

statement_map <- tibble::tribble(
  ~experiment, ~statement,
  "pets", "Adopt a shelter pet this year.",
  "privacy", "Let the government access encrypted messages.",
  "climate", "Pay higher taxes to fund climate programs."
)

models <- parsed |>
  left_join(statement_map, by = "experiment") |>
  rowwise() |>
  mutate(data = list(lx_set_metadata(data, statement = statement))) |>
  ungroup()

models$model <- vector("list", nrow(models))

Fit models for each experiment (this can take time):

for (i in seq_len(nrow(models))) {
  cli::cli_progress_step("Fitting {.val {models$experiment[[i]]}}")
  models$model[[i]] <- lx_fit(
    models$data[[i]],
    iter = 100,
    chains = 2,
    cores = 2
  )
  cli::cli_progress_done()
}

lx_fit() carries metadata from lx_data to lx_model, so statement labels are ready for spreadsheet summaries.

Build a cohort set

Summaries are produced for each subgroup in a cohort set:

cohort_set <- 
  example_srv_lx |>
  create_cohort_set(
    id_col = response_id,
    grp_cols = starts_with("d_"),
    weight_col = weight
  )

cohort_set
#> Cohort Set
#> # A tibble: 11 × 6
#>    column     label              level      cohort                 n weighted_n
#>    <chr>      <chr>              <chr>      <list>             <int>      <dbl>
#>  1 population Overall population population <cohort [500 × 2]>   500      500  
#>  2 d_gender   Gender             male       <cohort [244 × 2]>   244      238. 
#>  3 d_gender   Gender             female     <cohort [256 × 2]>   256      262. 
#>  4 d_age      Age group          18-30      <cohort [104 × 2]>   104      147. 
#>  5 d_age      Age group          31-45      <cohort [159 × 2]>   159      152. 
#>  6 d_age      Age group          46-64      <cohort [144 × 2]>   144      123. 
#>  7 d_age      Age group          65+        <cohort [93 × 2]>     93       77.9
#>  8 d_hhincome Household income   <$25k      <cohort [110 × 2]>   110      151. 
#>  9 d_hhincome Household income   $25k-$50k  <cohort [151 × 2]>   151      125. 
#> 10 d_hhincome Household income   $50k-$100k <cohort [148 × 2]>   148      147. 
#> 11 d_hhincome Household income   100k+      <cohort [91 × 2]>     91       77.4

Create the workbook summary

Pipe the summary into the internal renderer to get an openxlsx workbook:

summary <- lx_summary_for_cohort_set(models$model, cohort_set)
wb <- lx_summary_for_cohort_set_render(summary)

wb is the workbook object you can save locally or send to cloud storage.

To write it to Google Drive using cloudfs, you would run something like:

library(cloudfs)
cloud_drive_write(
  wb,
  "results/list_exp_stats_minimal_2025-11-19.xlsx",
  fun = openxlsx::saveWorkbook
)

This does not run in the vignette environment because it requires a project folder configured for cloudfs. In real projects, that setup is typically done at the start.