View source: R/mnprobit_utils.R
| prepare_mnp_data | R Documentation |
Prepares and validates inputs for Bayesian multinomial probit estimation.
Covariates are differenced against the base alternative, so the design
matrix has one row per (choice situation, non-base alternative) pair.
Balanced choice sets are required: every choice situation must contain
the same J alternatives.
prepare_mnp_data(
data,
id_col,
alt_col,
choice_col,
covariate_cols,
base_alt = NULL,
use_asc = TRUE
)
data |
Data frame containing choice data. |
id_col |
Name of the column identifying choice situations (individuals). |
alt_col |
Name of the column identifying alternatives. |
choice_col |
Name of the column indicating chosen alternative (1 = chosen, 0 = not chosen). |
covariate_cols |
Vector of names of columns to be used as covariates. |
base_alt |
Label of the base (reference) alternative used for utility
differencing. If |
use_asc |
Logical indicating whether to include alternative-specific constants (one intercept per non-base alternative). |
A list containing:
X: Stacked differenced design matrix ((N * p) x K), covariate
columns first, then ASC columns when use_asc = TRUE.
y: Integer vector of choices (0 = base alternative, j in 1..p
for the j-th non-base alternative), one per choice situation.
p: Number of utility differences (J - 1).
J: Number of alternatives.
N: Number of choice situations.
K: Number of columns of X.
alt_mapping: Data.table mapping alternatives to summary statistics
(the base alternative is alt_int = 1).
base_alt: Resolved label of the base alternative.
param_map: Named list of integer index vectors (beta, asc).
use_asc: Logical flag.
dropped_cols: Names of columns dropped due to collinearity, if any.
data_spec: List with column name metadata.
library(data.table)
set.seed(42)
N <- 50; J <- 3
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
dt[, choice := 0L]
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
input <- prepare_mnp_data(dt, "id", "alt", "choice", c("x1", "x2"))
str(input$X)
input$alt_mapping
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.