nl_elasticities_parallel: Compute aggregate elasticities for the Nested Logit model

View source: R/RcppExports.R

nl_elasticities_parallelR Documentation

Compute aggregate elasticities for the Nested Logit model

Description

Computes the aggregate (weighted-average) elasticity matrix for the Nested Logit model. Reduces to the MNL elasticities when all lambda = 1.

Usage

nl_elasticities_parallel(
  theta,
  X,
  alt_idx,
  choice_idx,
  nest_idx,
  M,
  weights,
  elast_var_idx,
  use_asc = TRUE,
  include_outside_option = FALSE
)

Arguments

theta

(K + n_non_singleton_nests + n_delta) vector with model parameters. Order: ⁠[beta (K), lambda (non-singleton), delta]⁠.

X

sum(M) x K design matrix with covariates.

alt_idx

sum(M) x 1 vector with indices of alternatives; 1-based indexing.

choice_idx

N x 1 vector (kept for API consistency, not used).

nest_idx

J x 1 vector with nest indices for each alternative; 1-based indexing.

M

N x 1 vector with number of alternatives for each individual.

weights

N x 1 vector with weights for each observation.

elast_var_idx

1-based index of the column in X for which to compute the elasticity.

use_asc

whether to use alternative-specific constants.

include_outside_option

whether to include outside option normalized to V=0, lambda=1.

Value

J x J matrix of aggregate elasticities (row = responding alt, col = perturbed alt).

Examples


library(data.table)
set.seed(42)
N <- 50; J <- 4
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
dt[, nest := ifelse(alt <= 2, "A", "B")]
dt[, choice := 0L]
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
fit <- run_nestlogit(dt, "id", "alt", "choice", c("x1", "x2"), "nest")
elas <- choicer:::nl_elasticities_parallel(coef(fit), fit$data$X, fit$data$alt_idx,
  fit$data$choice_idx, fit$data$nest_idx, fit$data$M, fit$data$weights,
  elast_var_idx = 1L)
elas


choicer documentation built on Sept. 5, 2026, 1:07 a.m.