wesml_vcov: Robust (sandwich) variance for a weighted / choice-based...

View source: R/methods.R

wesml_vcovR Documentation

Robust (sandwich) variance for a weighted / choice-based logit fit

Description

Recomputes the robust Huber-White sandwich variance V = A^{-1} B A^{-1} for a fitted multinomial (MNL), mixed (MXL) or nested (NL) logit, where the bread A = \sum_i w_i (-H_i) is the weighted negated Hessian and the meat B = \sum_i w_i^2 s_i s_i' is the weight-squared outer product of the per-individual scores. This is the appropriate variance under choice-based (endogenous stratified) / WESML weighting, where the inverse-Hessian and the ordinary BHHH variance are invalid. It can be called on any fitted model (e.g. one estimated with se_method = "hessian") to obtain robust standard errors post hoc, without refitting.

Usage

wesml_vcov(object, ...)

## S3 method for class 'choicer_mxl'
wesml_vcov(object, type = c("vcov", "se"), ...)

## S3 method for class 'choicer_mnl'
wesml_vcov(object, type = c("vcov", "se"), ...)

## S3 method for class 'choicer_nl'
wesml_vcov(object, type = c("vcov", "se"), ...)

Arguments

object

A fitted choicer_mnl, choicer_mxl or choicer_nl object (requires keep_data = TRUE).

...

Unused.

type

Either "vcov" (default) to return the variance-covariance matrix or "se" to return the standard-error vector.

Details

If the stored weights are uniform (all equal), a warning is emitted: the returned variance is then the ordinary robust (Huber-White) variance, not a WESML-weighted variance. Refit with WESML weights for a choice-based-sampling correction.

Value

A variance-covariance matrix (type = "vcov") or a named numeric vector of standard errors (type = "se"), in the raw parameter space.

See Also

wesml_weights, sample_by_choice, run_mxlogit

Examples


library(data.table)
set.seed(1)
N <- 200L; J <- 3L
dt <- data.table(id = rep(seq_len(N), each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), w1 = rnorm(.N))]
dt[, choice := as.integer(seq_len(.N) == sample.int(.N, 1L)), by = id]
fit <- run_mxlogit(dt, "id", "alt", "choice", "x1", "w1", S = 50L)
wesml_vcov(fit, "se")


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