predict.choicer_hb: Posterior choice probabilities and shares for hierarchical...

View source: R/hb_postest.R

predict.choicer_hbR Documentation

Posterior choice probabilities and shares for hierarchical Bayes fits

Description

Computes counterfactual choice probabilities, integrating over the posterior draws and (at the population level) over the random-coefficient distribution: for each kept draw (b_r, W_r, \delta_r, \ldots) one \beta \sim N(b_r, W_r) is drawn and the model probabilities are averaged. Alternatives in newdata that were not in the estimation sample receive a posterior-predictive \delta_{new} \sim N(z_{new}'\theta_r, \sigma_{d,r}^2) — the entry counterfactual unlocked by the random-effects \delta. Price or subsidy counterfactuals are just modified covariate columns in newdata.

Usage

## S3 method for class 'choicer_hb'
predict(
  object,
  newdata = NULL,
  level = c("population", "individual"),
  n_draws = 200L,
  aggregate = TRUE,
  ...
)

Arguments

object

A choicer_hmnl or choicer_hmnp fit.

newdata

Data frame with the estimation columns (choice column not required). NULL (default) predicts on the estimation data.

level

"population" (default) integrates over N(b, W); "individual" uses the respondent-level \beta_i (requires the prediction rows to belong to estimation respondents; fully posterior-integrated when the fit kept keep_beta_i = "draws").

n_draws

Number of posterior draws to integrate over (thinned evenly from the kept draws; default 200).

aggregate

If TRUE (default) return a per-alternative posterior share table (including the outside option); if FALSE return the posterior-mean probability per row of the prediction data.

...

Ignored.

Details

HMNL probabilities are closed-form logit; HMNP probabilities use the 1-D Gauss-Hermite representation of the iid-probit integral P(j) = \int \phi(u) \prod_{k \ne j} \Phi(V_j - V_k + u) du.

Value

With aggregate = TRUE, a data.table with columns alternative, share (posterior mean), sd, lower, upper (95% equal-tailed interval); the posterior share draws are attached as attr(, "draws"). With aggregate = FALSE, a numeric vector of posterior-mean choice probabilities, one per prediction row.

Examples


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)))
predict(fit)                       # posterior shares, estimation data
cf <- sim$data
cf$x1 <- cf$x1 + 0.5               # a counterfactual attribute change
predict(fit, newdata = cf)


choicer documentation built on Sept. 5, 2026, 1:07 a.m.