Using g6tr.cohorts with Choice-Based Conjoint Models
Source:vignettes/03_cohorts.Rmd
03_cohorts.RmdIntroduction
In choice-based conjoint analysis, it’s often valuable to analyze
results not just for the overall population but also for specific
subgroups of respondents. Cohort objects for this workflow are created
in the g6tr.cohorts package, and g6tr.conjoint
uses those objects to generate subgroup summaries and predictions. This
vignette shows that handoff: create cohorts with
g6tr.cohorts, then use them with
g6tr.conjoint.
A cohort in this context is a subset of respondents, defined by specific criteria. This allows you to segment your data and analyze the preferences of different groups separately. Cohorts are represented as data frames containing respondent IDs and their associated weights.
Creating Cohorts with g6tr.cohorts
Single Cohort
After loading g6tr.cohorts, you can create a single
cohort by filtering your survey data and then using
create_cohort(). Here’s how to create a cohort for
respondents from Italy:
# Assuming example_cjt_fp_drn_survey is your survey data
italy_cohort <-
example_cjt_fp_drn_survey |>
filter(country == "Italy") |>
create_cohort(
id_col = response_id,
weight_col = weight
)
italy_cohort
#> Cohort (N = 409)
#> # A tibble: 409 × 2
#> id weight
#> <chr> <dbl>
#> 1 R_0001 0.952
#> 2 R_0002 0.881
#> 3 R_0003 1.00
#> 4 R_0004 1.07
#> 5 R_0005 0.944
#> 6 R_0006 0.942
#> 7 R_0007 1.11
#> 8 R_0008 0.978
#> 9 R_0009 0.987
#> 10 R_0010 0.940
#> # ℹ 399 more rowsCohort Set
The create_cohort_set() function from
g6tr.cohorts allows you to create multiple cohorts at once
based on the levels of the provided grouping variables. For example, you
can create cohorts for each level of country and
m3_customer, and include an overall population cohort:
cohort_set <-
example_cjt_fp_drn_survey |>
create_cohort_set(
id_col = response_id,
grp_cols = c(country, m3_customer),
population = TRUE,
weight_col = weight
)
cohort_set
#> Cohort Set
#> # A tibble: 5 × 6
#> column label level cohort n weighted_n
#> <chr> <chr> <chr> <list> <int> <dbl>
#> 1 population Overall population population <cohort> 843 843
#> 2 country NA Italy <cohort> 409 409.
#> 3 country NA Spain <cohort> 434 434.
#> 4 m3_customer NA Customer <cohort> 417 418.
#> 5 m3_customer NA Non-Customer <cohort> 426 425.Each element in the cohort column is a cohort object
containing the respondent IDs and weights for that subgroup.
Example: Level Summaries for Cohorts
Cohorts created in g6tr.cohorts can be used with
functions like cjt_level_summary() to generate aggregated
results for each subgroup.
First, let’s build a conjoint model using the example dataset:
mod <- cjt_model(
nn_choice + allows_none(drn_sub_buy_choice) ~ service + scenario,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)Level Summaries for the Overall Population
By default, functions like cjt_level_summary() generate
summaries for the entire dataset when the cohorts parameter
is NULL.
level_summary_overall <- cjt_level_summary(mod)
#> ℹ No weights provided. Using equal weights of 1.
level_summary_overall
#> # A tibble: 2 × 6
#> term level_summaries importance attr_pval_vs_below attr_pval_vs_median
#> <chr> <list> <dbl> <dbl> <dbl>
#> 1 service <tibble [9 × 5]> 0.750 3.43e-304 3.43e-304
#> 2 scenario <tibble [6 × 5]> 0.250 NA 3.43e-304
#> # ℹ 1 more variable: indiv_importance <named list>
level_summary_overall$level_summaries[[2]]
#> # A tibble: 6 × 5
#> level probs mean level_pval_vs_below level_pval_vs_median
#> <chr> <named list> <dbl> <dbl> <dbl>
#> 1 Scenario 1 <dbl [843]> 0.198 1.17e- 2 8.72e-33
#> 2 Scenario 2 <dbl [843]> 0.191 1.19e- 7 8.52e-21
#> 3 Scenario 3 <dbl [843]> 0.178 1.02e-17 1.02e-17
#> 4 Scenario 4 <dbl [843]> 0.159 1.75e-18 1.02e-17
#> 5 Scenario 5 <dbl [843]> 0.140 9.01e- 2 2.39e-50
#> 6 Scenario 6 <dbl [843]> 0.135 NA 1.14e-22Level Summaries for a Single Cohort
To get level summaries for a specific cohort, pass the cohort to the
cohorts parameter:
level_summary_italy <- cjt_level_summary(mod, cohorts = italy_cohort)
level_summary_italy$level_summaries[[2]]
#> # A tibble: 6 × 5
#> level probs mean level_pval_vs_below level_pval_vs_median
#> <chr> <named list> <dbl> <dbl> <dbl>
#> 1 Scenario 1 <dbl [409]> 0.198 6.37e- 1 1.88e-17
#> 2 Scenario 2 <dbl [409]> 0.196 9.70e- 6 3.26e-13
#> 3 Scenario 3 <dbl [409]> 0.179 1.01e-11 1.01e-11
#> 4 Scenario 4 <dbl [409]> 0.158 2.39e-10 1.01e-11
#> 5 Scenario 5 <dbl [409]> 0.140 2.50e- 2 1.84e-28
#> 6 Scenario 6 <dbl [409]> 0.130 NA 1.10e-15Level Summaries for Multiple Cohorts
By passing a cohort set, you can obtain level summaries for multiple cohorts at once:
level_summary_cohort_set <- cjt_level_summary(mod, cohorts = cohort_set)
level_summary_cohort_set
#> # A tibble: 5 × 4
#> column label level summaries
#> <chr> <chr> <chr> <list>
#> 1 population Overall population population <tibble [2 × 6]>
#> 2 country NA Italy <tibble [2 × 6]>
#> 3 country NA Spain <tibble [2 × 6]>
#> 4 m3_customer NA Customer <tibble [2 × 6]>
#> 5 m3_customer NA Non-Customer <tibble [2 × 6]>Each element in the summaries column contains the level
summaries for the corresponding cohort.
Performance and Optimization
Generating summaries for a cohort set is not just convenient but also optimized for efficiency. By processing shared computational steps across cohorts collectively, we minimize redundant calculations. This approach typically results in faster execution compared to running the function separately for each cohort.