vcov.choicer_fit: Extract variance-covariance matrix from a choicer_fit object

View source: R/methods.R

vcov.choicer_fitR Documentation

Extract variance-covariance matrix from a choicer_fit object

Description

With no arguments, returns the variance-covariance matrix implied by the fit's own se_method (triggering lazy computation if needed). Passing type recomputes a different variance estimator post hoc from the stored data — no refit needed (requires keep_data = TRUE):

"hessian"

Inverse of the analytical negated Hessian.

"bhhh"

Inverse of the BHHH/OPG information \sum_i w_i s_i s_i'.

"robust"

Huber-White sandwich A^{-1} (\sum_i w_i^2 s_i s_i') A^{-1} — also the valid WESML variance under choice-based weighting.

"cluster"

Cluster-robust sandwich A^{-1} (\sum_g g_g g_g') A^{-1} with g_g = \sum_{i \in g} w_i s_i the within-cluster sum of weighted scores. Requires cluster (or a fit made with cluster_col). No small-sample correction is applied.

Here i indexes choice situations. For repeated choices by the same decision maker (panel data), cluster on the decision maker.

Usage

## S3 method for class 'choicer_fit'
vcov(object, type = NULL, cluster = NULL, ...)

Arguments

object

A choicer_fit object.

type

NULL (default; return the as-fitted vcov) or one of "hessian", "bhhh", "robust", "cluster".

cluster

Cluster labels for type = "cluster", one per choice situation. Alignment to the prepared (id-sorted) choice situations is handled as follows:

  • Named (recommended): names are matched against the choice-situation ids, so the vector is safe in any order. Build it by naming your per-situation labels with the id values.

  • Unnamed: taken to be in the prepared, id-sorted order; a warning flags that assumption. A vector of per-alternative (row-level) length is rejected.

Defaults to the labels stored at fit time via cluster_col (already aligned). Supplying cluster without type implies type = "cluster". The safest route is to pass cluster_col= at fit time, which sidesteps post-hoc alignment entirely.

...

Additional arguments (ignored).

Details

Note (mixed logit): clustering repairs the inference, not the estimand. run_mxlogit() treats each choice situation as an independent draw from the mixing distribution (a cross-sectional MSL likelihood, not the panel product form), so on panel data the point estimates target that cross-sectional model; type = "cluster" makes their standard errors robust to within-person dependence but does not turn the fit into a panel mixed logit. For panel random coefficients use run_hmnlogit (person_col).

Value

Named variance-covariance matrix, or NULL if unavailable.

Examples


library(data.table)
set.seed(42)
N <- 50; J <- 3
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
dt[, person := rep(1:10, each = 5)[id]]
dt[, choice := 0L]
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
fit <- run_mnlogit(dt, "id", "alt", "choice", c("x1", "x2"))
vcov(fit)                          # as fitted (hessian)
vcov(fit, type = "robust")         # Huber-White, post hoc
# named by situation id -> safe regardless of order
cl <- dt[, person[1L], by = id]
vcov(fit, type = "cluster", cluster = setNames(cl$V1, cl$id))


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