Clustered Covariance Matrix Estimation

Estimation of one-way and multi-way clustered covariance matrices using an object-oriented approach.

vcovCL(x, cluster = NULL, type = NULL, sandwich = TRUE, fix = FALSE, …)
meatCL(x, cluster = NULL, type = NULL, cadjust = TRUE, multi0 = FALSE, …)

a fitted model object.


a variable indicating the clustering of observations or a list (or data.frame) thereof. By default, either attr(x, "cluster") is used. If that is also NULL each observation is its own cluster.


a character string specifying the estimation type (HC0--HC3). The default is to use "HC1" for "lm" objects and "HC0" otherwise.


logical. Should the sandwich estimator be computed? If set to FALSE only the meat matrix is returned.


logical. Should the covariance matrix be fixed to be positive semi-definite in case it is not?


logical. Should a cluster adjustment be applied?


logical. Should the HC0 estimate be used for the final adjustment in multi-way clustered covariances?

arguments passed to meatCL.


Clustered sandwich estimators are used to adjust inference when errors are correlated within (but not between) clusters. vcovCL allows for clustering in arbitrary many cluster dimensions (e.g., firm, time, industry), given all dimensions have enough clusters (for more details, see Cameron et al. 2011). If each observation is its own cluster, the clustered sandwich collapses to the basic sandwich covariance.

The function meatCL is the work horse for estimating the meat of clustered sandwich estimators. vcovCL is a wrapper calling sandwich and bread (Zeileis 2006). vcovCL is applicable beyond lm or glm class objects.

bread and meat matrices are multiplied to construct clustered sandwich estimators. The meat of a clustered sandwich estimator is the cross product of the clusterwise summed estimating functions. Instead of summing over all individuals, first sum over cluster.

A two-way clustered sandwich estimator \(M\) (e.g., for cluster dimensions "firm" and "industry") is a linear combination of one-way clustered sandwich estimators for both dimensions (\(M_{firm}, M_{time}\)) minus the clustered sandwich estimator, with clusters formed out of the intersection of both dimensions (\(M_{id \cap time}\)): $$M = M_{id} + M_{time} - M_{id \cap time}$$ Instead of substracting \(M_{id \cap time}\) as the last substacted matrix, Ma (2014) suggests to substract the basic HC0 covariance matrix when only a single observation is in each intersection of \(id\) and \(time\). Set multi0 = TRUE to substract the basic HC0 covariance matrix as the last substracted matrix in multi-way clustering. For details, see also Petersen (2009) and Thompson (2011).

With the type argument, HC0 to HC3 types of bias adjustment can be employed. HC2 and HC3 types of bias adjustment are geared towards the linear model, but they are also applicable for GLMs (see Mc Caffrey and Bell (2002) and Kauermann and Carroll (2001) for details). A precondition for HC2 and HC3 types of bias adjustment is the existence of a hat matrix or a weighted version of the hat matrix for GLMs, respectively.

The cadjust argument allows to switch the cluster bias adjustment factor \(G/(G-1)\) on and off (where \(G\) is the number of clusters in a cluster dimension \(g\)) See Cameron et al. (2008) and Cameron et al. (2011) for more details about small-sample modifications.

Cameron et al. (2011) observe that sometimes the covariance matrix is not positive-semidefinite. To force the covariance matrix to be positive-semidefinite, set fix = TRUE. Following Cameron et al. (2011), the eigendecomposition of the estimated covariance matrix is used and any negative eigenvalue(s) are converted to zero.


A matrix containing the covariance matrix estimate.


Cameron AC & Gelbach JB & Miller DL (2008). “Bootstrap-Based Improvements for Inference with Clustered Errors”, The Review of Economics and Statistics, 90(3), 414--427. 10.3386/t0344

Cameron AC & Gelbach JB & Miller DL (2011). “Robust Inference With Multiway Clustering”, Journal of Business & Ecomomic Statistics, 29(2), 238--249. 10.1198/jbes.2010.07136

Kauermann G & Carroll RJ (2001). “A Note on the Efficiency of Sandwich Covariance Matrix Estimation”, Journal of the American Statistical Association, 96(456), 1387--1396. 10.1198/016214501753382309

Ma MS (2014). “Are We Really Doing What We Think We Are Doing? A Note on Finite-Sample Estimates of Two-Way Cluster-Robust Standard Errors”, Mimeo, Availlable at SSRN: URL

McCaffrey DF & Bell RM (2002). “Bias Reduction in Standard Errors for Linear Regression with Multi-Stage Samples”, Survey Methodology, 28(2), 169--181.

Petersen MA (2009). “Estimating Standard Errors in Finance Panel Data Sets: Comparing Approaches”, The Review of Financial Studies, 22(1), 435--480. 10.1093/rfs/hhn053

Thompson SB (2011). “Simple Formulas for Standard Errors That Cluster by Both Firm and Time”, Journal of Financial Economics, 99(1), 1--10. 10.1016/j.jfineco.2010.08.016

Zeileis A (2004). “Econometric Computing with HC and HAC Covariance Matrix Estimator”, Journal of Statistical Software, 11(10), 1--17. 10.18637/jss.v011.i10

Zeileis A (2006). “Object-Oriented Computation of Sandwich Estimators”, Journal of Statistical Software, 16(9), 1--16. 10.18637/jss.v016.i09

See Also


  • vcovCL
  • meatCL
library(sandwich) # NOT RUN { ## Petersen's data data("PetersenCL", package = "sandwich") m <- lm(y ~ x, data = PetersenCL) ## clustered covariances ## one-way vcovCL(m, cluster = PetersenCL$firm) ## one-way with HC2 vcovCL(m, cluster = PetersenCL$firm, type = "HC2") ## two-way vcovCL(m, cluster = PetersenCL[, c("firm", "year")]) ## comparison with cross-section sandwiches ## HC0 all.equal(sandwich(m), vcovCL(m, type = "HC0", cadjust = FALSE)) ## HC2 all.equal(vcovHC(m, type = "HC2"), vcovCL(m, type = "HC2")) ## HC3 all.equal(vcovHC(m, type = "HC3"), vcovCL(m, type = "HC3")) ## Innovation data data("InstInnovation", package = "sandwich") ## replication of one-way clustered standard errors for model 3, Table I ## and model 1, Table II in Berger et al. (2016) ## count regression formula f1 <- cites ~ institutions + log(capital/employment) + log(sales) + industry + year ## model 3, Table I: Poisson model ## one-way clustered standard errors tab_I_3_pois <- glm(f1, data = InstInnovation, family = poisson) vcov_pois <- vcovCL(tab_I_3_pois, InstInnovation$company) sqrt(diag(vcov_pois))[2:4] ## coefficient tables if(require("lmtest")) { coeftest(tab_I_3_pois, vcov = vcov_pois)[2:4, ] } # } # NOT RUN { ## model 1, Table II: negative binomial hurdle model ## (requires "pscl" or alternatively "countreg" from R-Forge) library("pscl") library("lmtest") tab_II_3_hurdle <- hurdle(f1, data = InstInnovation, dist = "negbin") # dist = "negbin", zero.dist = "negbin", separate = FALSE) vcov_hurdle <- vcovCL(tab_II_3_hurdle, InstInnovation$company) sqrt(diag(vcov_hurdle))[c(2:4, 149:151)] coeftest(tab_II_3_hurdle, vcov = vcov_hurdle)[c(2:4, 149:151), ] # }
Documentation reproduced from package sandwich, version 2.4-0, License: GPL-2 | GPL-3

Community examples

Looks like there are no examples yet.