Fits a MaxDiff model using the specified approach. This function serves as a unified interface to different MaxDiff modeling methods.
Usage
md_model(
data,
type = c("mlogit", "bayesm"),
cohorts = NULL,
anchor_data = NULL,
...
)Arguments
- data
A data frame in the tidy format produced by
md_parse_data().- type
Character string specifying the model type to fit:
"mlogit": Aggregate model using themlogitpackage."bayesm": Individual-level hierarchical model using thebayesmpackage.
- cohorts
A
cohort,cohort_set, orNULL(for full population). When specified, models will be fit only on the respondents in the cohort(s). Create cohorts usingcreate_cohort()orcreate_cohort_set().- anchor_data
A data frame containing anchor data with columns: id, item, answer. Used for anchored MaxDiff to transform relative preferences to absolute values. When provided, the anchor data is converted to tasks and appended to the main data.
- ...
Additional parameters passed to the underlying model function:
For
type = "mlogit": Parameters passed tomlogit::mlogit().For
type = "bayesm": Parameters passed tobayesm::rhierMnlRwMixture(), such asmcmc = list(R = 5000, keep = 5)to control MCMC iterations. TheRparameter sets the total number of iterations, whilekeepspecifies the thinning interval (every k-th iteration is retained).
Value
One of:
With no cohorts: An object of class "md_model"
With a single cohort: An object of class "md_model" for that cohort
With a cohort set: A nested data frame containing models for each cohort
Details
Two distinct modeling approaches are available:
Aggregate model (mlogit):
Treats all respondents as a homogeneous group.
Estimates a single set of utilities for the entire sample.
Fast to compute, but doesn't capture individual differences.
Individual model (bayesm):
Uses hierarchical Bayesian methods to estimate individual-level utilities.
Each respondent gets their own set of utilities.
More computationally intensive but captures heterogeneity.
For bayesm models, you can control MCMC settings with the
mcmcparameter, e.g.,mcmc = list(R = 5000, keep = 1000).
Anchored MaxDiff:
When
anchor_datais provided, the model performs anchored MaxDiff.Anchored MaxDiff transforms relative preferences into absolute values.
Items scoring above the anchor are considered important/desirable.
Items scoring below the anchor are considered unimportant/undesirable.
Examples
# Fit a mlogit model on full population
mod_mlogit <- md_model(example_md_parsed_data, type = "mlogit")
# Fit a bayesm model on full population
mod_bayesm <- md_model(
example_md_parsed_data,
type = "bayesm",
mcmc = list(R = 100, keep = 1) # 100 iterations, keep each one
)
#> Z not specified
#> Table of Y values pooled over all units
#> ypooled
#> 1 2 3 4 5
#> 1504 1276 1170 1137 1103
#>
#> Starting MCMC Inference for Hierarchical Logit:
#> Normal Mixture with 1 components for first stage prior
#> 5 alternatives; 25 variables in X
#> for 213 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 213 units ...
#> completed unit # 50
#> completed unit # 100
#> completed unit # 150
#> completed unit # 200
#> MCMC Iteration (est time to end - min)
#> 100 (0.0)
#> Total Time Elapsed: 0.02
# Fit a model for male respondents
male_cohort <- example_md_data |>
haven::as_factor() |>
dplyr::filter(s_gender == "Male") |>
g6tr.cohorts::create_cohort(id_col = "response_id")
#> ℹ No weights provided. Using equal weights of 1.
mod_mlogit_male <- md_model(example_md_parsed_data, type = "mlogit", cohorts = male_cohort)
# Fit models for gender segments
cohort_set <- example_md_data |>
haven::as_factor() |>
g6tr.cohorts::create_cohort_set(
id_col = "response_id",
grp_cols = "s_gender"
)
#> ℹ No weights provided. Using equal weights of 1.
mod_mlogit_gender <- md_model(example_md_parsed_data, type = "mlogit", cohorts = cohort_set)
# Fit a model with anchor data
mod_anchored <- md_model(
example_md_parsed_data,
type = "mlogit",
anchor_data = example_md_anchor_answers
)