sandwich_vcov {capybara}R Documentation

Recompute Sandwich Variance-Covariance Matrix

Description

Recompute the variance-covariance matrix for a fitted model using a different clustering structure or covariance type. This allows changing the vcov estimator without re-fitting the model, provided the model was fit with keep_tx = TRUE and return_hessian = TRUE in the control parameters.

Usage

sandwich_vcov(
  object,
  cluster1 = NULL,
  cluster2 = NULL,
  type = c("hetero", "HC0", "clustered", "m-estimator", "two-way", "dyadic",
    "m-estimator-dyadic"),
  ...
)

Arguments

object a fitted model object of class "feglm" or "felm".
cluster1 a vector or factor for the first clustering variable. Required for "clustered", "two-way", and "dyadic" types.
cluster2 a vector or factor for the second clustering variable. Required for "two-way" and "dyadic" types.
type character string specifying the covariance type. One of:
  • "hetero" or "HC0": heteroskedasticity-robust (no clustering)

  • "clustered" or "m-estimator": one-way cluster-robust

  • "two-way": two-way cluster-robust (Cameron-Gelbach-Miller)

  • "dyadic" or "m-estimator-dyadic": dyadic cluster-robust for network/trade data

... additional arguments (currently ignored).

Details

The model must be fit with fit_control(keep_tx = TRUE, return_hessian = TRUE) to store the centered design matrix, Hessian, and working residuals needed for vcov recomputation. Note that keep_data = TRUE is NOT required - residuals are stored automatically when keep_tx = TRUE.

For dyadic clustering (used in gravity/trade models), cluster1 and cluster2 represent the two entity dimensions (e.g., exporter and importer). The function handles the full dyadic correlation structure including cross-entity correlations.

Value

A named matrix of covariance estimates.

References

Cameron, C., J. Gelbach, and D. Miller (2011). "Robust Inference With Multiway Clustering". Journal of Business & Economic Statistics 29(2).

Cameron, C. and D. Miller (2014). "Robust Inference for Dyadic Data". Unpublished manuscript.

See Also

feglm, felm, fit_control

Examples

# Refitting models

fepoisson(
  mpg ~ wt | cyl,
  mtcars,
  control = fit_control(vcov_type = "hetero")
)

fepoisson(
  mpg ~ wt | cyl | am,
  mtcars,
  control = fit_control(vcov_type = "m-estimator")
)

# Reusing models

# Store required components
mod <- fepoisson(
  mpg ~ wt | cyl,
  mtcars,
  control = fit_control(keep_tx = TRUE, return_hessian = TRUE)
)

# Heteroskedastic-robust HC0 sandwich (no cluster variable needed)
sandwich_vcov(mod, type = "hetero")

# One-way M-estimator sandwich (cluster variable required)
sandwich_vcov(mod, cluster1 = mtcars$am, type = "m-estimator")

# Two-way Cameron-Miller sandwich (two cluster variables required)
sandwich_vcov(mod, cluster1 = mtcars$am, cluster2 = mtcars$gear, type = "two-way")

Loading...