| wesml_weights | R Documentation |
Computes Manski-Lerman (1977) Weighted Exogenous Sample Maximum Likelihood
(WESML) weights for a choice-based sample. The weight for a choice situation
whose chosen alternative is j is w = Q(j) / H(j), where
Q(j) is the population share of alternative j and H(j) its
sample share among choosers. Using these weights in
run_mxlogit restores consistency under choice-based sampling;
pair them with se_method = "sandwich" for valid (robust) standard
errors (the plain inverse-Hessian is invalid under weighting).
wesml_weights(
data,
id_col,
alt_col,
choice_col,
Q,
H = NULL,
normalize = TRUE,
attach = FALSE,
weight_name = ".wesml_weight",
outside_opt_label = NULL,
include_outside_option = FALSE
)
data |
A long-format choice data set (data.frame or data.table), one row per alternative per choice situation. |
id_col, alt_col, choice_col |
Column names identifying the choice situation, the alternative, and the 0/1 chosen indicator. |
Q |
Named numeric vector of population shares, one entry per chosen
stratum (names matched to |
H |
Optional named numeric vector of sample shares. If |
normalize |
If |
attach |
If |
weight_name |
Name of the weight column (default |
outside_opt_label, include_outside_option |
Set
|
Strata are defined by the chosen alternative and keyed by
as.character(alt) so numeric and character alternative codes match
supplied share names unambiguously.
Either an id-keyed data.table with columns id_col and
weight_name (default), or, when attach = TRUE, a copy of
data with the weight column appended. The result carries "Q",
"H", and "choice_sampling" attributes recording provenance.
Manski, C. F. and Lerman, S. R. (1977). The Estimation of Choice Probabilities from Choice Based Samples. Econometrica 45(8), 1977-1988. Train, K. E. (2009). Discrete Choice Methods with Simulation, Section 3.7. Cambridge University Press.
sample_by_choice, run_mxlogit,
wesml_vcov
library(data.table)
set.seed(1)
N <- 300L; J <- 3L
pop <- data.table(id = rep(seq_len(N), each = J), alt = rep(1:J, N))
pop[, x1 := rnorm(.N)]
pop[, w1 := rnorm(.N)]
pop[, choice := as.integer(seq_len(.N) == sample.int(.N, 1L)), by = id]
# Population shares of the chosen alternative
Q <- prop.table(table(pop[choice == 1, alt]))
wt <- wesml_weights(pop, "id", "alt", "choice", Q = Q)
head(wt)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.