Overview
lx_predict_for_cohorts() computes posterior summaries
for subgroup means. The key input (besides the model object) is
cohorts, which controls which subgroups are summarized.
For each cohort, the function constructs posterior draws of cohort means for:
-
publicprobability, -
privateprobability, -
delta = private - public.
It then summarizes each draw distribution (mean, lower, upper quantiles) and returns those summaries together with the underlying draws.
This vignette focuses on that cohorts logic. The same
logic also applies to other functions that accept a cohorts
argument:
cohorts not specified: full sample
When cohorts is NULL (default), predictions
are computed for the full sample.
lx_predict_for_cohorts(model)
#> ℹ No weights provided. Using equal weights of 1.
#> # A tibble: 4 × 5
#> component mean lower upper draws
#> <chr> <dbl> <dbl> <dbl> <list>
#> 1 public 0.158 0.115 0.209 <dbl [200]>
#> 2 private 0.253 0.120 0.411 <dbl [200]>
#> 3 delta 0.0957 -0.0446 0.271 <dbl [200]>
#> 4 majority 0.226 NA NA <dbl [0]>Single cohort
Use a cohort object when you want one specific
subgroup.
Because the fitted model stores respondent-level draws, predictions can be computed for any subgroup definition, not only those based on model predictors. Whether non-predictor subgroup summaries are substantively appropriate depends on the application, but technically they are valid to compute and at least we can say that they are not inherently wrong. For example, if a non-modeled variable can be predicted with high certainty from the modeled predictors, cohort summaries for it should still be meaningful. In the examples in this vignette we keep it simple and summarize by gender, which is included in the model.
cohort_female <- example_srv_lx |>
dplyr::filter(d_gender == "female") |>
create_cohort(id_col = response_id, weight_col = weight)
lx_predict_for_cohorts(model, cohorts = cohort_female)
#> # A tibble: 4 × 5
#> component mean lower upper draws
#> <chr> <dbl> <dbl> <dbl> <list>
#> 1 public 0.181 0.120 0.255 <dbl [200]>
#> 2 private 0.240 0.123 0.372 <dbl [200]>
#> 3 delta 0.0589 -0.0827 0.216 <dbl [200]>
#> 4 majority 0.230 NA NA <dbl [0]>
cohort_set
Use a cohort_set when you want many subgroups in one
call.
cohort_set <- example_srv_lx |>
create_cohort_set(
id_col = response_id,
grp_cols = c(d_gender),
weight_col = weight
)
set_pred <- lx_predict_for_cohorts(model, cohorts = cohort_set)
set_pred
#> Cohort Set
#> # A tibble: 3 × 7
#> column label level cohort n weighted_n predictions
#> <chr> <chr> <chr> <list> <int> <dbl> <list>
#> 1 population Overall population population <cohort> 500 500 <tibble>
#> 2 d_gender Gender male <cohort> 244 238. <tibble>
#> 3 d_gender Gender female <cohort> 256 262. <tibble>
set_pred$predictions[[1]]
#> # A tibble: 4 × 5
#> component mean lower upper draws
#> <chr> <dbl> <dbl> <dbl> <list>
#> 1 public 0.168 0.121 0.221 <dbl [200]>
#> 2 private 0.266 0.133 0.427 <dbl [200]>
#> 3 delta 0.0979 -0.0419 0.275 <dbl [200]>
#> 4 majority 0.231 NA NA <dbl [0]>Character interface for predictor-based cohorts
You can also pass character values to auto-generate cohorts from model predictors.
"predictors": all predictors + population
pred_all <- lx_predict_for_cohorts(model, cohorts = "predictors")
unique(pred_all$column)
#> [1] "population" "d_gender" "d_age" "d_hhincome"Explicit predictor subset
To request only a subset of predictors, pass those predictor names
explicitly. If you also want the overall population cohort in the same
output, include "population" in that character vector.
pred_subset <- lx_predict_for_cohorts(model, cohorts = c("d_gender", "d_age"))
unique(pred_subset$column)
#> [1] "d_gender" "d_age"Weight note for predictor-based cohorts
lx_fit() does not use weights in model estimation.
Weights matter here at the summary stage when cohorts are built from
predictors. If weight_col was set in
lx_parse_data(), those weights are used for predictor-based
cohorts; otherwise equal weights of 1 are used.
Same cohorts logic in other functions
The same cohorts options shown above can be used with
PPC and plotting functions.
# Predictor-based PPC summary
lx_ppc(model, cohorts = "predictors")
#> Cohort Set
#> # A tibble: 11 × 7
#> column label level cohort n weighted_n ppc
#> <chr> <chr> <chr> <list> <int> <dbl> <list>
#> 1 population Overall population population <cohort> 500 500 <tibble>
#> 2 d_gender Gender male <cohort> 244 238. <tibble>
#> 3 d_gender Gender female <cohort> 256 262. <tibble>
#> 4 d_age Age group 18-30 <cohort> 104 147. <tibble>
#> 5 d_age Age group 31-45 <cohort> 159 152. <tibble>
#> 6 d_age Age group 46-64 <cohort> 144 123. <tibble>
#> 7 d_age Age group 65+ <cohort> 93 77.9 <tibble>
#> 8 d_hhincome Household income <$25k <cohort> 110 151. <tibble>
#> 9 d_hhincome Household income $25k-$50k <cohort> 151 125. <tibble>
#> 10 d_hhincome Household income $50k-$100k <cohort> 148 147. <tibble>
#> 11 d_hhincome Household income 100k+ <cohort> 91 77.4 <tibble>
# Predictor-based strip plot
lx_strip_plot(model, cohorts = c("population", "d_gender"))
And for design-effect summaries on parsed data:
# Uses the same cohorts logic on lx_data
lx_design_effect(model$data, cohorts = c("population", "d_gender"), n.draws = 2000)