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", "clustered", "m-estimator", "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" and "dyadic" types.
cluster2 a vector or factor for the second clustering variable. Required for "dyadic" type.
type character string specifying the covariance type. One of:
  • "hetero": heteroskedasticity-robust (no clustering, also known as "HC0")

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

  • "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

ross2004_s1 <- ross2004[ross2004$year == 1994, ]
ross2004_s1 <- ross2004_s1[ross2004_s1$ltrade >
  quantile(ross2004_s1$ltrade, 0.75), ]

ross2004_s2 <- ross2004[ross2004$year == 1999, ]
ross2004_s2 <- ross2004_s2[ross2004_s2$ltrade >
  quantile(ross2004_s2$ltrade, 0.75), ]

ross2004_subset <- rbind(ross2004_s1, ross2004_s2)

fepoisson(
  ltrade ~ ldist | ctry1, ross2004_subset,
  control = fit_control(vcov_type = "hetero")
)

fepoisson(
  ltrade ~ ldist | ctry1, ross2004_subset,
  control = fit_control(vcov_type = "m-estimator")
)

# Reusing models

# Store required components
fit <- fepoisson(
  ltrade ~ ldist | ctry1, ross2004_subset,
  control = fit_control(keep_tx = TRUE, return_hessian = TRUE)
)

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

#' One-way cluster
sandwich_vcov(fit, cluster1 = ross2004_subset$year, type = "clustered")

Loading...