MaxDiff Statistics & Spreadsheet Summaries
2025-11-02
Source:vignettes/03_statistics.Rmd
03_statistics.RmdIntroduction
This vignette explains how to generate and visualize statistics from MaxDiff models. It focuses on two main functions:
-
md_stats()- Creates statistical summaries for MaxDiff models -
md_render_stats_summary()- Exports statistics for cohort sets to formatted spreadsheets
Before proceeding, you should be familiar with fitting MaxDiff models as covered in the Modeling vignette.
Let’s begin by fitting some example models:
From Model Output to the “Stats Table” – the Big Picture
What the two model types give us
The md_model() function can create two different types of models, each providing a different output:
Individual‑level model (bayesm) provides posterior draws of PWU for each respondent. Essentially, we have a PWU value for each iteration-respondent-item combination.
Aggregate model (mlogit) provides only a vector of population means and a covariance matrix, not inherent posterior draws. We still simulate PWU draws for the population mean (no respondent dimension) using the model-provided parameters, as this is a more practical approach for assessing distributions of all downstream statistics such as probabilities or anchor scores.
Unified processing pipeline
Despite these differences, we process both model types through a unified pipeline:
-
Obtain PWU draws (real or simulated)
- For
bayesm: Use MCMC draws directly from the model - For
mlogit: Simulate draws from a multivariate normal distribution using the estimated means and covariance matrix
- For
-
For each iteration, compute the desired statistic
- For individual-level models: First compute per-respondent, then average across respondents
- For aggregate models: Compute directly from the population draws (no averaging step)
-
Result: Population draws for each statistic
- Each iteration gives one draw of the population value
- These draws capture the uncertainty in our estimates
-
Summarize these draws into metrics
- Mean, credible intervals, superiority probabilities, etc.
Shape of the “stats table”
The md_stats() function returns a tibble with:
- One row per MaxDiff item
- One list-column per statistic (
pwu,prob,score_mean100, etc.) - Inside each list-column: a packed tibble of metrics for that statistic
Let’s see this in action:
# Generate statistics for our models
stats_mlogit <- md_stats(mod_mlogit)
stats_bayesm <- md_stats(mod_bayesm)
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.0602 1.56
#> 3 Affirmative action gives an unfair adv… -0.653 0.0345 0.898
#> 4 All voters should be required to provi… -0.149 0.0569 1.48
#> 5 America’s current political system is … -0.766 0.0308 0.801
#> 6 Convicted felons should be allowed to … -0.709 0.0326 0.847
#> 7 Democracy requires the active particip… -0.187 0.0549 1.43
#> 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.731
#> 10 I believe democracy is the best form o… -0.272 0.0503 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>
# Examine the structure
glimpse(stats_mlogit)
#> Rows: 26
#> Columns: 4
#> $ item <chr> "A woman is equally capable as a man of being a good pre…
#> $ pwu <tibble[,6]> <tbl_df[26 x 6]>
#> $ prob <tibble[,6]> <tbl_df[26 x 6]>
#> $ score_mean100 <tibble[,6]> <tbl_df[26 x 6]>
stats_mlogit$prob
#> # A tibble: 26 × 6
#> mean ci_low ci_high vec diff_vs_avg_superiority diff_vs_avg_vec
#> <dbl> <dbl> <dbl> <list> <dbl> <named list>
#> 1 0.0661 0.0578 0.0752 <dbl [1,000]> 1 <dbl [1,000]>
#> 2 0.0602 0.0524 0.0688 <dbl [1,000]> 1 <dbl [1,000]>
#> 3 0.0345 0.0300 0.0397 <dbl [1,000]> 0.069 <dbl [1,000]>
#> 4 0.0569 0.0498 0.0647 <dbl [1,000]> 1 <dbl [1,000]>
#> 5 0.0308 0.0266 0.0355 <dbl [1,000]> 0 <dbl [1,000]>
#> 6 0.0326 0.0283 0.0376 <dbl [1,000]> 0.008 <dbl [1,000]>
#> 7 0.0549 0.0482 0.0622 <dbl [1,000]> 1 <dbl [1,000]>
#> 8 0.0240 0.0207 0.0276 <dbl [1,000]> 0 <dbl [1,000]>
#> 9 0.0281 0.0241 0.0323 <dbl [1,000]> 0 <dbl [1,000]>
#> 10 0.0503 0.0433 0.0578 <dbl [1,000]> 1 <dbl [1,000]>
#> # ℹ 16 more rows
# stats_bayesm will have the same structureStatistics Glossary
This section describes each statistic that md_stats() can calculate, and how it’s computed in step 2 of our pipeline.
Part‑Worth Utilities (PWU)
Aggregate models directly provide information about population-level PWU distribution. For individual-level models, we simply average across respondents to obtain draws for population-level PWUs, as described earlier.
Probability Score (prob)
The probability score answers the question: “If every item were shown together, what is the chance each one would be picked?”
Computation:
- For each draw, we convert PWUs to probabilities using the standard multinomial logit formula:
Mean Rescaled Score (score_mean100)
This is the probability score rescaled so the average across all items equals 1 (or 100%).
Computation:
- Simply multiply the probability values by the number of items.
- For example, with 10 items total, the average probability would be 0.1, so multiplying by 10 gives an average of 1.0.
Anchor Score (score_anchor_*)
For anchored models, this statistic simulates a task within the typical MaxDiff experiment structure. The asterisk in the name represents the number of items shown in each task (typically a fixed number like 5 across all tasks).
Computation: - For each item, we simulate a task containing: - The item being evaluated - (T-1) anchor items, where T is the task size
For the anchor item itself, the simulated task contains T anchor items (giving it a baseline probability of 1/T)
We calculate the probability of choosing each item in its respective simulated task
Finally, we rescale all scores so the anchor item equals 1 (100%). Since the anchor’s raw probability is 1/T, this means multiplying all probabilities by T.
Metrics Glossary
For each statistic, md_stats() calculates several metrics from the population draws:
Single-number distribution metrics
-
mean- the expected value of the statistic -
ci_low: The lower bound of the 95% credible interval (2.5th percentile) -
ci_high: The upper bound of the 95% credible interval (97.5th percentile)
Superiority Metrics
diff_vs_avg_superiority: The probability that the item’s value is higher than the average item’s value. For each iteration, we compare the item’s value to the average across all items. The proportion of iterations where the item exceeds the average is the superiority value.diff_vs_ref_superiority: (Only calculated for cohort sets, described later) The probability that the item’s value for this cohort is higher than for the reference cohort (usually the population).
Vector Columns
-
vec: A list-column of raw posterior draws for the statistic -
diff_vs_avg_vec: Posterior draws for the difference between the item and the average item -
diff_vs_ref_vec: (Only present in cohort sets, described later) Posterior draws for the difference between the item in this cohort and the reference cohort
These vector columns allow for custom calculations beyond the standard metrics.
Applying md_stats() to Cohorts and Cohort Sets
So far we have inspected the structure returned by md_stats(model) for a single model. The same function lets you calculate statistics for any subset of respondents (cohort) or for an entire set of cohorts (cohort set) without changing the output format.
Subset with a Cohort
If your model is individual‑level you can pass a cohort object to md_stats(). The result has the same structure you have already seen – one row per item, the same statistic list‑columns, the same metrics, only now computed on the subset of respondents defined by the cohort.
female_cohort <-
example_md_data |>
haven::as_factor() |>
dplyr::filter(s_gender == "Female") |>
g6tr.cohorts::create_cohort(id_col = "response_id")
stats_bayesm_female <- md_stats(mod_bayesm, female_cohort)Stats for a Cohort Set
A cohort set is usually created with create_cohort_set(). It contains
- a
cohortcolumn (each element is a cohort object), and - descriptive columns such as
label,level, …
When you call md_stats(model, cohort_set) you get the same tibble back, with one extra column stats. Each element of stats holds a full md_stats() result for that cohort.
cohort_set <-
example_md_data |>
haven::as_factor() |>
g6tr.cohorts::create_cohort_set(id_col = "response_id", grp_cols = "s_gender")
stats_set_bayesm <- md_stats(mod_bayesm, cohort_set)
stats_set_bayesm
#> # 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>The spreadsheet helper
md_render_stats_summary()(next section) expects exactly this kind of stats set as its input.
Creating Spreadsheet Summaries
The md_render_stats_summary() function creates formatted Excel spreadsheets from your statistics.
Key Parameters
Headers
The headers parameter controls how cohorts appear in columns. If you create a cohort set with create_cohort_set(), it will have label and level columns among others. When you apply md_stats() to this cohort set, these columns are preserved, and md_render_stats_summary() is programmed to use them by default for headers.
You can customize this behavior by specifying your own columns, using just one column for a flat structure, or using more than two columns to create a deeper multilevel spanning header structure.
Statistic Name
The stat_name parameter specifies which statistic to display (“pwu”, “prob”, “score_mean100”, or "score_anchor_*"). You can call the function multiple times to add different statistics to different sheets of the same workbook.
Number Display
The number_col parameter (defaulting to “mean”) specifies which metric to display in the cells.
The number_format parameter controls formatting: - “0.0…” - Controls decimal places - “0.0%” - Displays values as percentages
If left unspecified, the function will identify the most appropriate format based on the statistic and number_col metric.
Cell Coloring
The color_by_col parameter specifies which metric determines cell coloring. Similar to number_col, you pass a metric name to this parameter. The cells are colored according to a gradient scheme (defined by colors and breaks parameters), with the cell color determined by where the metric value falls within this gradient.
The default value color_by_col = "mean" means cells are colored by the same value that’s displayed (assuming you’re using the default number_col). However, you can color cells based on any numeric metric.
The two most common alternatives are:
- “diff_vs_avg_superiority” - Colors cells by difference from the average item
- “diff_vs_pop_superiority” - Colors cells by difference from the reference cohort
Additional parameters for customizing the color scheme: - colors - A vector defining the color gradient - breaks - Numerical values corresponding to colors (defaults are determined automatically)
Basic Example
Create a simple summary of mean scores:
wb <- md_render_stats_summary(stats_set_bayesm, stat_name = "score_mean100")Save the workbook:
cloudfs::cloud_drive_write(
wb,
"summaries/maxdiff/bayesm.xlsx",
fun = openxlsx::saveWorkbook
)Adding Multiple Sheets
You can add multiple statistics to the same workbook:
wb <- md_render_stats_summary(
stats_set = stats_set_bayesm,
stat_name = "score_mean100",
wb = wb,
color_by_col = "diff_vs_pop_superiority",
sheet_name = "score_mean100 vs pop"
)This creates a new sheet colored by the difference from the reference cohort (population).