Skip to contents

Generates synthetic choice data with random coefficients drawn from a multivariate normal (optionally log-normal per dimension) and an additional mean shifter mu. Random coefficients are parameterized via the lower Cholesky factor of Sigma. Covariates are Uniform(-1, 1) by default; columns named in price_cols are drawn as -Uniform(0.1, 3) to mimic strictly-negative price variables. With T > 1 the data are a panel: each of the N decision makers draws one taste vector and keeps it in all of their T choice situations (the model of run_mxlogit(person_col = )).

Usage

simulate_mxl_data(
  N = 5000,
  J = 4,
  beta = c(0.8, -0.6),
  delta = NULL,
  mu = NULL,
  Sigma = matrix(c(1, 0.5, 0.5, 1.5), nrow = 2),
  rc_dist = NULL,
  rc_correlation = NULL,
  price_cols = NULL,
  seed = 123,
  outside_option = TRUE,
  vary_choice_set = TRUE,
  T = 1L
)

Arguments

N

Number of decision makers; equals the number of choice situations when T = 1.

J

Number of inside alternatives.

beta

Fixed coefficients for x1..x{K_x} (length K_x = length(beta)).

delta

ASCs for inside alternatives (length J). Defaults to an alternating pattern of c(0.5, -0.5).

mu

Mean shifter for random coefficients (length K_w = ncol(Sigma)). Defaults to a zero vector.

Sigma

Covariance matrix of random coefficients (square, K_w x K_w).

rc_dist

Integer vector (length K_w): 0L for normal, 1L for log-normal. Default NULL is treated as all-normal.

rc_correlation

Logical; if NULL (default) it is auto-detected from the off-diagonal entries of Sigma.

price_cols

Character vector of w* column names to draw as -Uniform(0.1, 3) instead of Uniform(-1, 1). Default NULL.

seed

Random seed (NULL skips set.seed()).

outside_option

Logical; include outside option with alt = 0.

vary_choice_set

Logical; if TRUE (default) choice set size is sampled uniformly from 2:J.

T

Number of choice situations per decision maker (a single integer >= 1; default 1, a cross-section). Situation ids 1..N*T stay globally unique, so id_col = "id" works with or without person_col = "pid"; with T > 1 the data gain a leading pid column identifying decision makers. Choice sets, covariates and shocks are drawn per situation. For a given seed, T = 1 reproduces the data of earlier versions exactly.

Value

A choicer_sim object. true_params includes beta, delta, Sigma, L_params (packed Cholesky parameters), mu, rc_dist, rc_correlation, and gamma_i: the K_w x N matrix of realized random coefficients (rows are the w* columns, columns the decision makers), on the scale they enter utility: the realized tastes, which the conditional means from conditional_tastes() can be compared with (they track them with shrinkage). settings records T.

Details

Random coefficients are constructed to match the estimator's parameterization in src/mxlogit.cpp. For every dimension the raw draw is L %*% eta where eta ~ N(0, I). A normal random coefficient (rc_dist = 0) is then gamma_k = mu_k + (L %*% eta)_k. A log-normal random coefficient (rc_dist = 1) follows the shifted log-normal beta_k = exp(mu_k) + exp((L %*% eta)_k) – not the textbook exp(mu_k + sigma_k * eta) – so mu_k in true_params$mu is on the same scale the estimator recovers and recovery_table() can compare like-for-like.

Examples

# \donttest{
sim <- simulate_mxl_data(N = 1000, J = 4, seed = 123)
print(sim)
#> <choicer_sim: mxl>
#>   settings:
#>     N = 1000
#>     T = 1
#>     J = 4
#>     K_x = 2
#>     K_w = 2
#>     outside_option = TRUE
#>     vary_choice_set = TRUE
#>   rows in $data: 3989
#>   true_params: beta, delta, Sigma, L_params, mu, rc_dist, rc_correlation, gamma_i
# panel: 200 decision makers x 5 choice situations
panel <- simulate_mxl_data(N = 200, T = 5, J = 4, seed = 123)
head(panel$data)
#> Key: <id, alt>
#>      pid    id   alt         w1          w2         x1         x2 choice
#>    <int> <int> <int>      <num>       <num>      <num>      <num>  <int>
#> 1:     1     1     0  0.0000000  0.00000000  0.0000000  0.0000000      0
#> 2:     1     1     1 -0.4829672  0.01392497  0.4604484  0.7977606      0
#> 3:     1     1     2 -0.4183136  0.76606674  0.9195028 -0.2709202      0
#> 4:     1     1     3  0.8231983  0.80627507  0.9026704 -0.8451227      0
#> 5:     1     1     4 -0.2533855 -0.72885927 -0.1546041 -0.9897135      1
#> 6:     1     2     0  0.0000000  0.00000000  0.0000000  0.0000000      1
# }