View source: R/hmnlogit_utils.R
| run_hmnlogit | R Documentation |
Runs the adaptive RW-Metropolis-within-Gibbs sampler for the hierarchical (random-coefficients, panel or cross-sectional) multinomial logit with a BLP-style alternative-level random effect:
U_{ijt} = x_{ijt}'\gamma_i + \delta_j + \epsilon_{ijt}, \qquad
U_{iot} = \epsilon_{iot},
with i.i.d. Gumbel shocks (including on the implicit outside option, whose
systematic utility is 0), \beta_i \sim N(b, W) over the structural
covariates (\gamma_{ik} = \beta_{ik} or \exp(\beta_{ik}) per
rc_dist), and \delta_j = z_j'\theta + \xi_j,
\xi_j \sim N(0, \sigma_d^2). Partial pooling shrinks each
\delta_j toward its characteristics-based mean z_j'\theta;
the outside option anchors the level of \delta (mean utility
relative to the outside good), so no base alternative or sum-to-zero
constraint is needed.
run_hmnlogit(
data = NULL,
id_col = NULL,
alt_col = NULL,
choice_col = NULL,
covariate_cols = NULL,
person_col = NULL,
alt_covariate_cols = NULL,
outside_opt_label = NULL,
cf_residual_col = NULL,
input_data = NULL,
include_outside_option = TRUE,
rc_dist = NULL,
prior = list(),
mcmc = list(),
chains = 1,
keep_beta_i = c("means", "draws", "none"),
keep_data = TRUE
)
data |
Data frame (convenience pathway). Supply either |
id_col |
Name of the column identifying choice situations (tasks). Task ids only need to be unique within a respondent. |
alt_col |
Name of the column identifying alternatives. |
choice_col |
Name of the column indicating the chosen alternative (1 = chosen, 0 = not chosen). |
covariate_cols |
Vector of names of structural covariate columns (the random-coefficient dimensions). |
person_col |
Name of the respondent column grouping choice
situations. |
alt_covariate_cols |
Names of alternative-level covariate columns
(constant within each alternative) forming the |
outside_opt_label |
Label of physical outside-option rows, removed
when |
cf_residual_col |
Name of a first-stage residual column (control
function for an endogenous covariate), appended to |
input_data |
A |
include_outside_option |
Logical; if |
rc_dist |
Integer vector, one entry per column of |
prior |
Named list overriding prior defaults: |
mcmc |
Named list overriding MCMC defaults: |
chains |
Number of independent chains (seeds offset by 1, run
sequentially). Chain 1 provides the reported draws; all chains feed the
rank-normalized split-R-hat table and the retained |
keep_beta_i |
|
keep_data |
Logical; keep the prepared data on the fit (default
|
Initialization. \beta_i start at the pooled MNL maximum
likelihood estimate over the structural covariates (log-normal
coordinates transformed to the chain scale with a warn-and-clamp at
0.05); \delta starts at shrunk log choice-share contrasts against
the outside option; \theta at the OLS regression of the initial
\delta on Z.
Priors. b \sim N(b\_bar, A^{-1}), W \sim IW(\nu, V),
\theta \sim N(\theta\_bar, A_\theta^{-1}), and
\sigma_d \sim half-Cauchy(0, s_d) via the Makalic-Schmidt
scale mixture (set sd_prior$half_cauchy = FALSE for a plain
IG(c_0, d_0) on \sigma_d^2).
Endogeneity. If a price-like covariate is endogenous (correlated
with \xi_j), supply a first-stage residual via cf_residual_col
(Petrin & Train 2010); posterior uncertainty does NOT propagate
first-stage estimation error.
For beta_i draws at very large scale beyond the memory guard's
threshold, a future disk-streaming path (writing each kept slice to
disk instead of retaining it in memory) is on the roadmap but not built
in this phase; users needing per-respondent draws at that scale should
reduce R, reduce chains, or use keep_beta_i = "means".
A choicer_hmnl object (classed c("choicer_hmnl", "choicer_hb")) with posterior summaries (coefficients, se,
vcov for b; theta_summary; sigma_d2_summary; W_mean;
delta and xi quality-ladder tables; beta_i), the raw thinned
draws (chain 1), acceptance diagnostics in accept, the
rank-normalized split-R-hat table in rhat, all chains' retained
draws in chains, and sampler metadata.
prepare_hmnl_data(), simulate_hmnl_data(),
recovery_table(), rhat(), ess(), mcse(), traceplot()
sim <- simulate_hmnl_data(N = 100, T = 3, J = 4, seed = 42)
fit <- suppressWarnings(run_hmnlogit(sim$data, "task", "alt", "choice", c("x1", "x2"),
person_col = "pid", alt_covariate_cols = "z1",
mcmc = list(R = 500, burn = 200)))
summary(fit)
coef(fit, component = "delta")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.