View source: R/mnlogit_utils.R
| prepare_mnl_data | R Documentation |
Prepares and validates inputs for multinomial logit estimation routine.
prepare_mnl_data(
data,
id_col,
alt_col,
choice_col,
covariate_cols,
weights = NULL,
outside_opt_label = NULL,
include_outside_option = FALSE,
weights_col = NULL,
cluster_col = NULL
)
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. |
weights |
Optional vector of weights for each choice situation. If |
outside_opt_label |
Label for the outside option (if any). If |
include_outside_option |
Logical indicating whether to include an outside option in the model. |
weights_col |
Optional name of a column in |
cluster_col |
Optional name of a column in |
A list containing:
X: Design matrix (sum(M) x K).
alt_idx: Integer vector of alternative indices.
choice_idx: Integer vector of chosen alternative indices.
M: Integer vector with number of alternatives per choice situation.
N: Number of choice situations.
weights: Vector of weights.
cluster: Vector of cluster labels (or NULL).
situation_ids: Choice-situation ids in prepared (sorted) order.
include_outside_option: Logical flag.
alt_mapping: Data.table mapping alternatives to summary statistics.
dropped_cols: Names of columns dropped due to collinearity, if any.
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_mnl_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.