Description Usage Arguments Details Value References Examples
Compute a cluster-robust variance matrix for a linear regression, with or without instruments, where clustering occurs along one dimension. Useful for calculating a variance matrix when a regression is calculated manually.
1 |
u |
An nx1 vector of residuals from a linear regression. |
X |
An nxk matrix of covariates. |
Z |
An optional nxl matrix of instruments, with l >= k. |
P |
An optional lxl weighting matrix. |
ids |
A vector of length n that groups observations in |
df |
An optional degrees of freedom correction. Default is Stata's degrees of freedom correction. |
This function calculates the standard robust variance matrix for a linear regression, as in Manski (1988, Theorem 1 in Chapter 8), White (2001, Theorem 6.3), or Wooldridge (2010, Theorem 8.2); that is,
(X'ZPZ'X)^-1 X'ZPVPZ'X (X'ZPZ'X)^-1.
It is useful when a regression is calculated by hand. This function gives the same result (up to rounding) as vcovHC(x, type = 'sss', cluster = 'group')
from the plm package, but can be quite a bit faster.
A kxk matrix.
Manski, C. (1988). Analog Estimation Methods in Econometrics. Chapman and Hall.
White, H. (2001). Asymptotic Theory for Econometricians (revised edition). Emerald Publishing.
Wooldridge, J. M. (2010). Econometric Analysis of Cross Section and Panel Data (2nd edition). MIT University Press.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ## Not run:
# Makes some groups in mtcars
mtcars$clust<- letters[1:4]
# Matrices for regression
x <- model.matrix(~ cyl + disp, mtcars)
y <- matrix(mtcars$mpg)
# Regression coefficients
b <- solve(crossprod(x)) %*% crossprod(x, y)
# Residuals
r <- y - x %*% b
# Robust variance matrix
vcov <- rvar(r, x, ids = mtcars$clust)
## End(Not run)
## Not run:
# Same as plm
library(plm)
mdl <- plm(mpg ~ cyl + disp, mtcars, model = 'pooling', index = 'clust')
vcov2 <- vcovHC(mdl, type = 'sss', cluster = 'group')
vcov - vcov2
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.