Skip to contents

Calculate MaxDiff statistics for segmentation models. Supports both pre-fitted models and fitting new models on the fly.

Usage

rack_put_md_stats(rack, model, name = "md_stats")

shelf_make_md_stats(shelf, model)

Arguments

rack

A segmentation rack created with, for instance, seg_rack_nmf()

model

Either a fitted individual-level ("bayesm") md_model object or a list of parameters for fitting models. The list should contain at least a type element ("bayesm" or "mlogit") and a data element with MaxDiff data. Additional parameters are passed to g6tr.md::md_model().

name

Name of the new list-column to create, defaults to "md_stats".

shelf

A model shelf as returned by rack_extract_shelf().

Value

For rack_put_md_stats(): The input rack with an extra list-column containing MaxDiff statistics for every shelf.

For shelf_make_md_stats(): A dataframe of MaxDiff statistics with one row per segment cohort.

Details

When using a pre-fitted model individual-level, segment-level statistics are computed by subsetting the appropriate respondents. When passing a parameter list, the function will either:

  • For "bayesm" type: Fit one individual-level model and use it to compute statistics for each segment.

  • For "mlogit" type: Fit separate aggregate models for each segment.

Rendering MaxDiff Statistics

Currently there's no built-in functionality to render MaxDiff statistics directly within g6tr.segmentation. However, you can easily extract the statistics and use the rendering functions from g6tr.md package.

MaxDiff statistics in the rack need to be prepared before they can be rendered using g6tr.md's md_render_stats_summary() function. This typically involves combining all segment stats sets from across the rack into a single tibble.

For more detailed information about MaxDiff statistics and rendering options, see the "MaxDiff Statistics & Spreadsheet Summaries" vignette in the g6tr.md package.

Examples

# 1. Reuse a pre-fitted individual-level model
md_mod_bayesm <- g6tr.md::md_model(
  example_md_parsed_data_for_seg,
  type = "bayesm",
  mcmc = list(R = 100, keep = 1)
)
#> Z not specified
#> Table of Y values pooled over all units
#> ypooled
#>   1   2   3   4   5 
#> 694 602 578 544 532 
#>  
#> Starting MCMC Inference for Hierarchical Logit:
#>    Normal Mixture with 1 components for first stage prior
#>    5  alternatives;  25  variables in X
#>    for  100  cross-sectional units
#>  
#> Prior Parms: 
#> nu = 28
#> V 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]   28    0    0    0    0    0    0    0    0     0     0     0     0
#>  [2,]    0   28    0    0    0    0    0    0    0     0     0     0     0
#>  [3,]    0    0   28    0    0    0    0    0    0     0     0     0     0
#>  [4,]    0    0    0   28    0    0    0    0    0     0     0     0     0
#>  [5,]    0    0    0    0   28    0    0    0    0     0     0     0     0
#>  [6,]    0    0    0    0    0   28    0    0    0     0     0     0     0
#>  [7,]    0    0    0    0    0    0   28    0    0     0     0     0     0
#>  [8,]    0    0    0    0    0    0    0   28    0     0     0     0     0
#>  [9,]    0    0    0    0    0    0    0    0   28     0     0     0     0
#> [10,]    0    0    0    0    0    0    0    0    0    28     0     0     0
#> [11,]    0    0    0    0    0    0    0    0    0     0    28     0     0
#> [12,]    0    0    0    0    0    0    0    0    0     0     0    28     0
#> [13,]    0    0    0    0    0    0    0    0    0     0     0     0    28
#> [14,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [15,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [16,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [17,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [18,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [19,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [20,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [21,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [22,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [23,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [24,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [25,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#>  [1,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [2,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [3,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [4,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [5,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [6,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [7,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [8,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [9,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [10,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [11,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [12,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [13,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [14,]    28     0     0     0     0     0     0     0     0     0     0     0
#> [15,]     0    28     0     0     0     0     0     0     0     0     0     0
#> [16,]     0     0    28     0     0     0     0     0     0     0     0     0
#> [17,]     0     0     0    28     0     0     0     0     0     0     0     0
#> [18,]     0     0     0     0    28     0     0     0     0     0     0     0
#> [19,]     0     0     0     0     0    28     0     0     0     0     0     0
#> [20,]     0     0     0     0     0     0    28     0     0     0     0     0
#> [21,]     0     0     0     0     0     0     0    28     0     0     0     0
#> [22,]     0     0     0     0     0     0     0     0    28     0     0     0
#> [23,]     0     0     0     0     0     0     0     0     0    28     0     0
#> [24,]     0     0     0     0     0     0     0     0     0     0    28     0
#> [25,]     0     0     0     0     0     0     0     0     0     0     0    28
#> mubar 
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#> [1,]     0     0     0     0     0     0     0     0     0     0     0
#> Amu 
#>      [,1]
#> [1,] 0.01
#> a 
#> [1] 5
#>  
#> MCMC Parms: 
#> s= 0.476  w=  0.1  R=  100  keep=  1  nprint=  100
#> 
#> initializing Metropolis candidate densities for  100  units ...
#>   completed unit # 50
#>   completed unit # 100
#>  MCMC Iteration (est time to end - min) 
#>  100 (0.0)
#>  Total Time Elapsed: 0.00 
rack1 <- rack_put_md_stats(
  example_seg_rack,
  model = md_mod_bayesm
)
#> ℹ Using the first cohort in the cohort set as the reference for comparisons.
#> If you need a different reference, please reorder the cohort set accordingly.
#> • label: "Overall population"
#> • level: population
#> ✖ Cohorts with < 20 respondents:
#> # A tibble: 1 × 4
#>   column  level     n weighted_n
#>   <chr>   <chr> <int>      <dbl>
#> 1 segment 4        19       18.0
#> ℹ Consider whether these small cohorts are appropriate for your analysis. You may want to drop them using `filter()`.
#> ℹ Using the first cohort in the cohort set as the reference for comparisons.
#> If you need a different reference, please reorder the cohort set accordingly.
#> • label: "Overall population"
#> • level: population
#> ✖ Cohorts with < 20 respondents:
#> # A tibble: 3 × 4
#>   column  level     n weighted_n
#>   <chr>   <chr> <int>      <dbl>
#> 1 segment 1        17       18.2
#> 2 segment 3        15       16.2
#> 3 segment 5        11       10.2
#> ℹ Consider whether these small cohorts are appropriate for your analysis. You may want to drop them using `filter()`.
#> ℹ Using the first cohort in the cohort set as the reference for comparisons.
#> If you need a different reference, please reorder the cohort set accordingly.
#> • label: "Overall population"
#> • level: population
# stats for the first segmentation solution
rack1$md_stats[[1]]
#> Cohort Set
#> # A tibble: 3 × 6
#>   label level cohort                n weighted_n stats            
#>   <chr> <fct> <list>            <int>      <dbl> <list>           
#> 1 nmf_3 1     <cohort [22 × 2]>    22       23.7 <tibble [26 × 4]>
#> 2 nmf_3 2     <cohort [50 × 2]>    50       49.6 <tibble [26 × 4]>
#> 3 nmf_3 3     <cohort [28 × 2]>    28       29.2 <tibble [26 × 4]>

# 2. Fit a new individual-level model on the fly which will be reused across all segments
rack2 <- rack_put_md_stats(
  example_seg_rack,
  model = list(
    type = "bayesm",
    data = example_md_parsed_data_for_seg,
    mcmc = list(R = 100, keep = 1)
  )
)
#> ℹ Fitting one individual-level bayesm model to be reused across all segments.
#> Z not specified
#> Table of Y values pooled over all units
#> ypooled
#>   1   2   3   4   5 
#> 694 602 578 544 532 
#>  
#> Starting MCMC Inference for Hierarchical Logit:
#>    Normal Mixture with 1 components for first stage prior
#>    5  alternatives;  25  variables in X
#>    for  100  cross-sectional units
#>  
#> Prior Parms: 
#> nu = 28
#> V 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]   28    0    0    0    0    0    0    0    0     0     0     0     0
#>  [2,]    0   28    0    0    0    0    0    0    0     0     0     0     0
#>  [3,]    0    0   28    0    0    0    0    0    0     0     0     0     0
#>  [4,]    0    0    0   28    0    0    0    0    0     0     0     0     0
#>  [5,]    0    0    0    0   28    0    0    0    0     0     0     0     0
#>  [6,]    0    0    0    0    0   28    0    0    0     0     0     0     0
#>  [7,]    0    0    0    0    0    0   28    0    0     0     0     0     0
#>  [8,]    0    0    0    0    0    0    0   28    0     0     0     0     0
#>  [9,]    0    0    0    0    0    0    0    0   28     0     0     0     0
#> [10,]    0    0    0    0    0    0    0    0    0    28     0     0     0
#> [11,]    0    0    0    0    0    0    0    0    0     0    28     0     0
#> [12,]    0    0    0    0    0    0    0    0    0     0     0    28     0
#> [13,]    0    0    0    0    0    0    0    0    0     0     0     0    28
#> [14,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [15,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [16,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [17,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [18,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [19,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [20,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [21,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [22,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [23,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [24,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#> [25,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#>  [1,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [2,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [3,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [4,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [5,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [6,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [7,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [8,]     0     0     0     0     0     0     0     0     0     0     0     0
#>  [9,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [10,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [11,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [12,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [13,]     0     0     0     0     0     0     0     0     0     0     0     0
#> [14,]    28     0     0     0     0     0     0     0     0     0     0     0
#> [15,]     0    28     0     0     0     0     0     0     0     0     0     0
#> [16,]     0     0    28     0     0     0     0     0     0     0     0     0
#> [17,]     0     0     0    28     0     0     0     0     0     0     0     0
#> [18,]     0     0     0     0    28     0     0     0     0     0     0     0
#> [19,]     0     0     0     0     0    28     0     0     0     0     0     0
#> [20,]     0     0     0     0     0     0    28     0     0     0     0     0
#> [21,]     0     0     0     0     0     0     0    28     0     0     0     0
#> [22,]     0     0     0     0     0     0     0     0    28     0     0     0
#> [23,]     0     0     0     0     0     0     0     0     0    28     0     0
#> [24,]     0     0     0     0     0     0     0     0     0     0    28     0
#> [25,]     0     0     0     0     0     0     0     0     0     0     0    28
#> mubar 
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#> [1,]     0     0     0     0     0     0     0     0     0     0     0
#> Amu 
#>      [,1]
#> [1,] 0.01
#> a 
#> [1] 5
#>  
#> MCMC Parms: 
#> s= 0.476  w=  0.1  R=  100  keep=  1  nprint=  100
#> 
#> initializing Metropolis candidate densities for  100  units ...
#>   completed unit # 50
#>   completed unit # 100
#>  MCMC Iteration (est time to end - min) 
#>  100 (0.0)
#>  Total Time Elapsed: 0.00 
#> ℹ Using the first cohort in the cohort set as the reference for comparisons.
#> If you need a different reference, please reorder the cohort set accordingly.
#> • label: "Overall population"
#> • level: population
#> ✖ Cohorts with < 20 respondents:
#> # A tibble: 1 × 4
#>   column  level     n weighted_n
#>   <chr>   <chr> <int>      <dbl>
#> 1 segment 4        19       18.0
#> ℹ Consider whether these small cohorts are appropriate for your analysis. You may want to drop them using `filter()`.
#> ℹ Using the first cohort in the cohort set as the reference for comparisons.
#> If you need a different reference, please reorder the cohort set accordingly.
#> • label: "Overall population"
#> • level: population
#> ✖ Cohorts with < 20 respondents:
#> # A tibble: 3 × 4
#>   column  level     n weighted_n
#>   <chr>   <chr> <int>      <dbl>
#> 1 segment 1        17       18.2
#> 2 segment 3        15       16.2
#> 3 segment 5        11       10.2
#> ℹ Consider whether these small cohorts are appropriate for your analysis. You may want to drop them using `filter()`.
#> ℹ Using the first cohort in the cohort set as the reference for comparisons.
#> If you need a different reference, please reorder the cohort set accordingly.
#> • label: "Overall population"
#> • level: population

# 3. Fit separate aggregate models for each segment
rack3 <- rack_put_md_stats(
  example_seg_rack,
  model = list(
    type = "mlogit",
    data = example_md_parsed_data_for_seg
  )
)
#> ℹ Fitting separate aggregate models for each segment.
#> ✖ Cohorts with < 20 respondents:
#> # A tibble: 1 × 4
#>   column  level     n weighted_n
#>   <chr>   <chr> <int>      <dbl>
#> 1 segment 4        19       18.0
#> ℹ Consider whether these small cohorts are appropriate for your analysis. You may want to drop them using `filter()`.
#> ℹ Fitting separate aggregate models for each segment.
#> ✖ Cohorts with < 20 respondents:
#> # A tibble: 3 × 4
#>   column  level     n weighted_n
#>   <chr>   <chr> <int>      <dbl>
#> 1 segment 1        17       18.2
#> 2 segment 3        15       16.2
#> 3 segment 5        11       10.2
#> ℹ Consider whether these small cohorts are appropriate for your analysis. You may want to drop them using `filter()`.
#> ℹ Fitting separate aggregate models for each segment.

# 4. Compute MaxDiff stats for a single shelf
md_stats <- shelf_make_md_stats(
  example_seg_shelf,
  model = list(
    type = "mlogit",
    data = example_md_parsed_data_for_seg
  )
)
#> ℹ Fitting separate aggregate models for each segment.

# Rendering MaxDiff statistics
if (FALSE) { # \dontrun{
# Extract stats from all solutions and combine into one tibble
combined_stats_set <- dplyr::bind_rows(rack1$md_stats)

# This won't render md stats
wb <- rack_render_everything(rack1, file = NULL)
# But we can add it using the g6tr.md function
wb <- g6tr.md::md_render_stats_summary(
  combined_stats_set, 
  stat_name = "score_mean100",
  sheet_name = "MD Score Mean 100",
  wb = wb
)

# Add another sheetcolored by difference from population
wb <- g6tr.md::md_render_stats_summary(
  combined_stats_set,
  stat_name = "prob",
  number_format = "0.0%",
  color_by_col = "diff_vs_pop_superiority",
  sheet_name = "MD Probability",
  wb = wb
)

# Save the workbook
openxlsx::saveWorkbook(wb, "maxdiff_segment_stats.xlsx", overwrite = TRUE)
} # }