Skip to contents

Extracts or computes a (cluster-)robust variance-covariance matrix for use in simulation-based inference. This function provides a unified interface for obtaining robust vcov from different model types.

Usage

get_vcov_robust(
  model,
  cluster = NULL,
  data = NULL,
  adjust = NULL,
  bread = c("observed", "vglm"),
  type = c("HC0", "HC1"),
  cadjust = NULL
)

Arguments

model

A fitted vglm, orm, or lrm object, or a robcov_vglm object that already contains robust covariance. Model-based SOP workflows separately require provenance from the corresponding *_markov() fitting wrapper.

cluster

Optional. Specifies how to compute cluster-robust standard errors:

  • NULL: Use robust (sandwich) vcov, but without clustering

  • Formula (e.g., ~id): Extract cluster variable from the model's original fitting data and compute cluster-robust vcov

  • Character/numeric vector: Cluster variable values directly (must match the number of observations used to fit the model)

data

Data frame. Optional. If cluster is a formula and the cluster variable cannot be found in the model's original data, it will be extracted from this data frame. Note: For cluster-robust SEs, the cluster vector must have the same length as the model's fitting data, not prediction data.

adjust

Deprecated compatibility alias for cadjust for vglm models.

bread

Bread used for a raw vglm model: "observed" or "vglm".

type

HC correction used when computing an ORM or VGLM sandwich: "HC0" or "HC1".

cadjust

Optional logical finite-cluster correction used when computing an ORM or VGLM sandwich. By default it is applied when cluster is supplied.

Value

A variance-covariance matrix.

Details

This function handles several cases:

1. Pre-computed robust vcov (robcov_vglm object): If the model is already a robcov_vglm object (from calling robcov_vglm()), the stored robust vcov is returned directly.

2. No clustering requested: If cluster = NULL, returns the robust sandwich covariance. VGLM models use robcov_vglm(), while ORM models use the package-internal analytic-score sandwich. A stored package-owned ORM covariance is reused only when its provenance and covariance identity remain valid and no correction override is requested.

3. Cluster formula: If cluster is a formula like ~id, the function first tries to extract the cluster variable from the model's original fitting data (by evaluating the data argument from the model's call). This ensures the cluster vector has the correct length for computing robust standard errors.

Important: Cluster-robust standard errors are computed from the model's score contributions, which have length equal to the number of observations used to fit the model. The cluster variable must therefore match this length, NOT the length of any new prediction data.

ORM and VGLM HC1 and finite-cluster corrections use the same definitions. type = "HC1" applies \((m - 1) / (m - p)\), where \(m\) is the number of fitting rows (positive-weight rows for ORM) and \(p\) is the number of coefficients. cadjust = TRUE independently applies \(n / (n - 1)\), where \(n\) is the number of clusters (or fitting rows when no cluster is supplied). An explicit cluster or correction request recomputes the ORM covariance; otherwise a valid covariance stored by orm_markov() is returned unchanged.

Examples

if (FALSE) { # \dontrun{
library(VGAM)

# Fit a model
fit <- vglm(y ~ yprev + time + tx,
            family = cumulative(parallel = TRUE, reverse = TRUE),
            data = mydata)

# Robust (unclustered) vcov
V1 <- get_vcov_robust(fit)

# Cluster-robust vcov using formula (extracts 'id' from model's data)
V2 <- get_vcov_robust(fit, cluster = ~id)

# Pre-computed robust vcov
fit_robust <- robcov_vglm(fit, cluster = mydata$id)
V3 <- get_vcov_robust(fit_robust)  # Same as fit_robust$var
} # }