Skip to contents

Computes cohort-level posterior draws and summaries for public, private, and delta (= private − public) probabilities stored in an lx_model. If majority responses are available, also reports the weighted mean for majority. For each cohort, the function:

  • subsets the model's probability draws to respondents in the cohort,

  • computes weighted column means across respondents (using cohort weights),

  • returns per-component summaries (public/private/delta): mean and two quantiles.

Usage

lx_predict_for_cohorts(model, cohorts = NULL, lower = 0.025, upper = 0.975)

# S3 method for class '`NULL`'
lx_predict_for_cohorts(model, cohorts = NULL, lower = 0.025, upper = 0.975)

# S3 method for class 'character'
lx_predict_for_cohorts(model, cohorts, lower = 0.025, upper = 0.975)

# S3 method for class 'cohort'
lx_predict_for_cohorts(model, cohorts, lower = 0.025, upper = 0.975)

# S3 method for class 'cohort_set'
lx_predict_for_cohorts(model, cohorts, lower = 0.025, upper = 0.975)

Arguments

model

An object of class lx_model.

cohorts

A cohort, cohort_set, NULL (whole population), or "predictors" to auto-build a cohort set from all predictors in model$data and include "population". You can also pass a character vector of predictor names to build cohorts from a selected subset. Include "population" explicitly in that vector when you also want an overall population cohort (or pass "population" alone for a population-only summary).

lower, upper

Numeric quantile probabilities for the lower/upper bounds. Defaults to 0.025 and 0.975.

Value

  • For a single cohort: a tibble with rows for component = "public"/"private"/"delta", plus "majority" if available. Columns include mean, lower, upper, and a list-column draws containing the vector of posterior draws of the cohort average for each component. The "majority" row has no draws (empty list entry) and lower/upper are NA.

  • For a cohort_set: the input cohort set with an added list-column predictions containing the tibble described above for each cohort.

See also

Examples

# The package ships with a small example model + the survey it came from
model_pets <- example_lx_model

# Whole population (NULL): weights default to equal
all_pred <- lx_predict_for_cohorts(model_pets)
#> ℹ No weights provided. Using equal weights of 1.
all_pred
#> # 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 (e.g., women)
cohort_female <- example_srv_lx |>
  dplyr::filter(d_gender == "female") |>
  g6tr.cohorts::create_cohort(id_col = response_id, weight_col = weight)

female_pred <- lx_predict_for_cohorts(model_pets, cohorts = cohort_female)
female_pred
#> # 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 (e.g., by gender)
cohort_set <- example_srv_lx |>
  g6tr.cohorts::create_cohort_set(
    id_col = response_id,
    grp_cols = c(d_gender),
    weight_col = weight
  )

set_pred <- lx_predict_for_cohorts(model_pets, cohorts = cohort_set)
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]>