Estimation of sandwich covariances a la Beck and Katz (1995) for panel data.

vcovPC(x, cluster = NULL, = NULL,
  pairwise = FALSE, sandwich = TRUE, fix = FALSE, ...)

meatPC(x, cluster = NULL, = NULL,
  pairwise = FALSE, kronecker = TRUE, ...)



a fitted model object.


a single variable indicating the clustering of observations, or a list (or data.frame) of one or two variables, or a formula specifying which one ore two variables from the fitted model should be used (see examples). In case two variables are specified, the second variable is assumed to provide the time ordering (instead of using the argument By default (cluster = NULL), either attr(x, "cluster") is used (if any) or otherwise every observation is assumed to be its own cluster.

a variable, list/data.frame, or formula indicating the aggregation within time periods. By default attr(x, "") is used (if any) or specified through the second variable in cluster (see above). If neither is available, observations within clusters are assumed to be ordered.


logical. For unbalanced panels. Indicating whether the meat should be estimated pair- or casewise.


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. Calculate the meat via the Kronecker-product, shortening the computation time for small matrices. For large matrices, set kronecker = FALSE.


arguments passed to the meatPC or estfun function, respectively.


vcovPC is a function for estimating Beck and Katz (1995) panel-corrected covariance matrix.

The function meatPC is the work horse for estimating the meat of Beck and Katz (1995) covariance matrix estimators. vcovPC is a wrapper calling sandwich and bread (Zeileis 2006).

Following Bailey and Katz (2011), there are two alternatives to estimate the meat for unbalanced panels. For pairwise = FALSE, a balanced subset of the panel is used, whereas for pairwise = TRUE, a pairwise balanced sample is employed.

The cluster/ specification can be made in a number of ways: Either both can be a single variable or cluster can be a list/data.frame of two variables. If expand.model.frame works for the model object x, the cluster (and potentially additionally can also be a formula. By default (cluster = NULL, = NULL), attr(x, "cluster") and attr(x, "") are checked and used if available. If not, every observation is assumed to be its own cluster, and observations within clusters are assumed to be ordered accordingly. If the number of observations in the model x is smaller than in the original data due to NA processing, then the same NA processing can be applied to cluster if necessary (and x$na.action being available).


A matrix containing the covariance matrix estimate.


Bailey D, Katz JN (2011). “Implementing Panel-Corrected Standard Errors in R: The pcse Package”, Journal of Statistical Software, Code Snippets, 42(1), 1--11. doi:10.18637/jss.v042.c01

Beck N, Katz JN (1995). “What To Do (and Not To Do) with Time-Series-Cross-Section Data in Comparative Politics”, American Political Science Review, 89(3), 634--647. doi:10.2307/2082979

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

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

Zeileis A, Köll S, Graham N (2020). “Various Versatile Variances: An Object-Oriented Implementation of Clustered Covariances in R.” Journal of Statistical Software, 95(1), 1--36. doi:10.18637/jss.v095.i01

See also


## Petersen's data
data("PetersenCL", package = "sandwich")
m <- lm(y ~ x, data = PetersenCL)

## Beck and Katz (1995) standard errors
## balanced panel
sqrt(diag(vcovPC(m, cluster = ~ firm + year)))
#> (Intercept)           x 
#>  0.02220064  0.02527598 

## unbalanced panel
PU <- subset(PetersenCL, !(firm == 1 & year == 10))
pu_lm <- lm(y ~ x, data = PU)
sqrt(diag(vcovPC(pu_lm, cluster = ~ firm + year, pairwise = TRUE)))
#> (Intercept)           x 
#>  0.02206979  0.02533772 
sqrt(diag(vcovPC(pu_lm, cluster = ~ firm + year, pairwise = FALSE)))
#> (Intercept)           x 
#>  0.02260277  0.02524119 

# \donttest{
## the following specifications of cluster/ are equivalent
vcovPC(m, cluster = ~ firm + year)
#>               (Intercept)             x
#> (Intercept)  4.928685e-04 -4.396037e-05
#> x           -4.396037e-05  6.388754e-04
vcovPC(m, cluster = PetersenCL[, c("firm", "year")])
#>               (Intercept)             x
#> (Intercept)  4.928685e-04 -4.396037e-05
#> x           -4.396037e-05  6.388754e-04
vcovPC(m, cluster = ~ firm, = ~ year)
#>               (Intercept)             x
#> (Intercept)  4.928685e-04 -4.396037e-05
#> x           -4.396037e-05  6.388754e-04
vcovPC(m, cluster = PetersenCL$firm, = PetersenCL$year)
#>               (Intercept)             x
#> (Intercept)  4.928685e-04 -4.396037e-05
#> x           -4.396037e-05  6.388754e-04

## these are also the same when observations within each
## cluster are already ordered
vcovPC(m, cluster = ~ firm)
#>               (Intercept)             x
#> (Intercept)  4.928685e-04 -4.396037e-05
#> x           -4.396037e-05  6.388754e-04
vcovPC(m, cluster = PetersenCL$firm)
#>               (Intercept)             x
#> (Intercept)  4.928685e-04 -4.396037e-05
#> x           -4.396037e-05  6.388754e-04
# }