Introduction
This vignette demonstrates how to analyze MaxDiff data using the g6tr.md package. It assumes you already have prepared and parsed your MaxDiff data as described in the companion vignette “Preparing MaxDiff Data”.
For all examples in this vignette, we’ll use the example_md_parsed_data dataset included with the package, which contains properly formatted MaxDiff data ready for analysis.
Fitting Models
The g6tr.md package supports two types of MaxDiff models:
-
Aggregate models (
"mlogit"): Treats all respondents as a homogeneous group, fast but doesn’t capture individual differences -
Individual-level models (
"bayesm"): Uses hierarchical Bayesian methods to estimate individual-level utilities
# Fit an aggregate model
mod_mlogit <- md_model(example_md_parsed_data, type = "mlogit")
mod_mlogit
#> ── MaxDiff Model ───────────────────────────────────────────────────────────
#> $model: <mlogit>
#> $type: "mlogit"
#> $granularity: "aggregate"
#> $cohort: Cohort (N = 213)
#> $pwu:
#> # A tibble: 26 × 3
#> item pwu se
#> <fct> <dbl> <dbl>
#> 1 A woman is equally capable as a man of being a 0 0
#> 2 A woman should have the right to choose whether -0.0889 0.104
#> 3 Affirmative action gives an unfair advantage -0.653 0.103
#> # ℹ 23 more rows
#> # ℹ Use `print(n = ...)` to see more rows
#> ────────────────────────────────────────────────────────────────────────────
# Fit an individual-level model with reduced MCMC iterations for demonstration
# Note that keep is the thinning parameter, so 5 means every 5th iteration is kept
# = 500/5 = 100 iterations.
mod_bayesm <- md_model(example_md_parsed_data, type = "bayesm",
mcmc = list(R = 500, keep = 5))
mod_bayesm
#> ── MaxDiff Model ───────────────────────────────────────────────────────────
#> $model: <list>
#> $type: "bayesm"
#> $granularity: "individual"
#> $cohort: Cohort (N = 213)
#> $pwu:
#> # A tibble: 5,538 × 3
#> id item pwu
#> <chr> <fct> <dbl>
#> 1 R_0BySpORZ9EPy9mp A woman is equally capable as a man of being a 0
#> 2 R_0BySpORZ9EPy9mp A woman should have the right to choose whether -1.68
#> 3 R_0BySpORZ9EPy9mp Affirmative action gives an unfair advantage to -0.862
#> # ℹ 5,535 more rows
#> # ℹ Use `print(n = ...)` to see more rows
#> ────────────────────────────────────────────────────────────────────────────The resulting model objects contain:
- The original model object
- The model type and granularity
- Part-worth utilities (PWUs)
- Information about the respondent cohort used for fitting
Calculating Statistics
Once we have fitted our models, we can calculate standard statistics using the md_stats() function:
# Calculate statistics for the aggregate model
stats_mlogit <- md_stats(mod_mlogit)
stats_mlogit
#> # A tibble: 26 × 4
#> item pwu$mean prob$mean score_mean100$mean
#> <chr> <dbl> <dbl> <dbl>
#> 1 A woman is equally capable as a man of… 0 0.0661 1.72
#> 2 A woman should have the right to choos… -0.0889 0.0606 1.58
#> 3 Affirmative action gives an unfair adv… -0.653 0.0344 0.893
#> 4 All voters should be required to provi… -0.149 0.0569 1.48
#> 5 America’s current political system is … -0.766 0.0305 0.793
#> 6 Convicted felons should be allowed to … -0.709 0.0325 0.846
#> 7 Democracy requires the active particip… -0.187 0.0548 1.42
#> 8 Ensuring global military dominance sho… -1.01 0.0240 0.624
#> 9 Ensuring that the 700,000 Deferred Act… -0.856 0.0281 0.732
#> 10 I believe democracy is the best form o… -0.272 0.0502 1.31
#> # ℹ 16 more rows
#> # ℹ 15 more variables: pwu$ci_low <dbl>, $ci_high <dbl>, $vec <list>,
#> # $diff_vs_avg_superiority <dbl>, $diff_vs_avg_vec <named list>,
#> # prob$ci_low <dbl>, $ci_high <dbl>, $vec <list>,
#> # $diff_vs_avg_superiority <dbl>, $diff_vs_avg_vec <named list>,
#> # score_mean100$ci_low <dbl>, $ci_high <dbl>, $vec <list>,
#> # $diff_vs_avg_superiority <dbl>, $diff_vs_avg_vec <named list>
# Calculate statistics for the individual-level model
stats_bayesm <- md_stats(mod_bayesm)
stats_bayesm
#> # A tibble: 26 × 4
#> item pwu$mean prob$mean score_mean100$mean
#> <chr> <dbl> <dbl> <dbl>
#> 1 A woman is equally capable as a man of… 0 0.0602 1.57
#> 2 A woman should have the right to choos… -0.285 0.0676 1.76
#> 3 Affirmative action gives an unfair adv… -1.11 0.0339 0.881
#> 4 All voters should be required to provi… -0.307 0.0787 2.05
#> 5 America’s current political system is … -1.40 0.0218 0.567
#> 6 Convicted felons should be allowed to … -1.14 0.0244 0.635
#> 7 Democracy requires the active particip… -0.188 0.0386 1.00
#> 8 Ensuring global military dominance sho… -1.72 0.0255 0.664
#> 9 Ensuring that the 700,000 Deferred Act… -1.32 0.0237 0.616
#> 10 I believe democracy is the best form o… -0.353 0.0441 1.15
#> # ℹ 16 more rows
#> # ℹ 15 more variables: pwu$ci_low <dbl>, $ci_high <dbl>, $vec <list>,
#> # $diff_vs_avg_superiority <dbl>, $diff_vs_avg_vec <named list>,
#> # prob$ci_low <dbl>, $ci_high <dbl>, $vec <list>,
#> # $diff_vs_avg_superiority <dbl>, $diff_vs_avg_vec <named list>,
#> # score_mean100$ci_low <dbl>, $ci_high <dbl>, $vec <list>,
#> # $diff_vs_avg_superiority <dbl>, $diff_vs_avg_vec <named list>Full details on the statistics returned by md_stats()—including part‑worth utilities (pwu), choice probabilities (prob), score_mean100, and, when anchors are used, score_anchor_{n_items_per_task}—are covered in the dedicated Statistics vignette.
Cohort Analysis
Often, we want to analyze specific cohorts within our data. The g6tr.md package integrates with the g6tr.cohorts package to enable cohort analysis.
Cohort‑set Analysis
For a comprehensive comparison across cohorts, you first need to create a cohort set:
# Create a cohort set with overall population and gender segments
cohort_set <- example_md_data |>
haven::as_factor() |>
g6tr.cohorts::create_cohort_set(
id_col = "response_id",
grp_cols = "s_gender"
)
cohort_set
#> # A tibble: 3 × 6
#> column label level cohort n weighted_n
#> <chr> <chr> <chr> <list> <int> <dbl>
#> 1 population Overall population popul… <cohort> 220 220
#> 2 s_gender With which gender do you identify? Male <cohort> 81 81
#> 3 s_gender With which gender do you identify? Female <cohort> 139 139When working with individual-level models ("bayesm"), there’s an option to calculate cohort-level statistics from a single model fit on the entire population. This uses the fact that each respondent has their own utilities estimated. It’s more efficient than refitting a Bayesian model for every cohort:
# Compute statistics for each cohort with a single, full-population bayesm model
mod_bayesm_cohort_set <- md_stats(mod_bayesm, cohort_set)
mod_bayesm_cohort_set
#> # A tibble: 3 × 7
#> column label level cohort n weighted_n stats
#> <chr> <chr> <chr> <list> <int> <dbl> <list>
#> 1 population Overall population popu… <cohort> 220 220 <tibble>
#> 2 s_gender With which gender do you … Male <cohort> 81 81 <tibble>
#> 3 s_gender With which gender do you … Fema… <cohort> 139 139 <tibble>On the other hand, aggregate models (e.g., "mlogit") don’t provide respondent-level utilities, so if you want separate results by cohort, you need to fit a separate model for each one. The md_model() function already supports this approach:
# Fit separate models for each cohort (commonly used for aggregate mlogit)
mod_mlogit_cohort_set <- md_model(example_md_parsed_data, type = "mlogit", cohorts = cohort_set)
# Calculate statistics for each model
mod_mlogit_cohort_set <-
mod_mlogit_cohort_set |>
rowwise() |>
mutate(stats = list(md_stats(model))) |>
ungroup()
mod_mlogit_cohort_set
#> # A tibble: 3 × 8
#> column label level cohort n weighted_n model stats
#> <chr> <chr> <chr> <list> <int> <dbl> <list> <list>
#> 1 population Overall popula… popu… <cohort> 220 220 <md_model> <tibble>
#> 2 s_gender With which gen… Male <cohort> 81 81 <md_model> <tibble>
#> 3 s_gender With which gen… Fema… <cohort> 139 139 <md_model> <tibble>Anchored aggregate model
Anchored MaxDiff (sometimes called anchored Best–Worst) lets you draw a utility boundary between important and unimportant items, turning relative scores into an absolute scale.
• Each respondent answers a single anchoring question (e.g., a dual‑response follow‑up or a direct binary rating task) that labels a subset of items as lying above or below the anchor.
• Whatever the survey design, you must collapse the raw answers into a tall tibble with three columns:
1. id – respondent identifier.
2. item – item label (same factor levels as the main MaxDiff data).
3. answer – logical, where TRUE = item is above the anchor, FALSE = below.
Preparing this table is the analyst’s responsibility, the package doesn’t infer it from the raw export. The built‑in demo data illustrate the expected structure:
print(example_md_anchor_answers, n = 5)
#> # A tibble: 2,315 × 3
#> id item answer
#> <chr> <fct> <lgl>
#> 1 R_0BySpORZ9EPy9mp The United States should have stricter gun control l… TRUE
#> 2 R_0BySpORZ9EPy9mp A woman is equally capable as a man of being a good … TRUE
#> 3 R_0BySpORZ9EPy9mp Ensuring global military dominance should be a top p… TRUE
#> 4 R_0BySpORZ9EPy9mp America’s current political system is responsive to … FALSE
#> 5 R_0BySpORZ9EPy9mp I believe democracy is the best form of government. TRUE
#> # ℹ 2,310 more rowsOnce such a table is available, pass it to the anchor_data argument of md_model(). The answers are internally converted to extra MaxDiff tasks and appended to the main data before fitting the aggregate mlogit model. This produces utilities on an absolute (zero‑anchored) scale—useful, but note that anchoring can re‑introduce scale‑use bias, so compare groups with caution.
mod_mlogit_anchored <- md_model(
example_md_parsed_data,
type = "mlogit",
anchor_data = example_md_anchor_answers
)
mod_mlogit_anchoredThe resulting object is the same kind of md_model as a regular mlogit fit, but after calling md_stats() it additionally contains score_anchor_{n_items_per_task} metrics that quantify how far each item lies above (or below) the anchor.