| sample_by_choice | R Documentation |
Subsamples whole choice situations from a population data set according to
fixed per-stratum quotas, where strata are defined by the chosen
alternative. The input data set is treated as the population, so the
population shares Q(j) are known exactly; the returned sample carries a
ready-to-use WESML weight column (see wesml_weights).
sample_by_choice(
data,
id_col,
alt_col,
choice_col,
n_per_alt = NULL,
frac_per_alt = NULL,
seed = NULL,
weight_name = ".wesml_weight",
outside_opt_label = NULL,
include_outside_option = FALSE
)
data, id_col, alt_col, choice_col |
As in |
n_per_alt |
Either a single integer applied to every stratum, or a named
integer vector of per-stratum counts (names matched to
|
frac_per_alt |
Either a single fraction in |
seed |
Optional integer seed for reproducible sampling. |
weight_name |
Name of the attached weight column (default
|
outside_opt_label, include_outside_option |
As in
|
Sampling is by choice situation (id), never by row: all alternative-rows of a sampled situation are kept together. Sampling is without replacement.
A data.table subsample with the weight column appended and
"Q", "H", and "choice_sampling" attributes (the last
records the scheme, shares, quotas, and meat = "robust").
Manski, C. F. and Lerman, S. R. (1977). Econometrica 45(8), 1977-1988.
wesml_weights, run_mxlogit
library(data.table)
set.seed(1)
N <- 600L; 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]
s <- sample_by_choice(pop, "id", "alt", "choice", n_per_alt = 50L, seed = 1L)
attr(s, "choice_sampling")$H # realized sample shares
head(s[[".wesml_weight"]])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.