Created
September 30, 2020 18:09
-
-
Save vv111y/211f740718a4622295eed5f966940ab8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
library(lmtest) ; library(sandwich) | |
# data sim | |
library(texreg) | |
set.seed(1) | |
x <- rnorm(1000) | |
y <- 5 + 2*x + rnorm(1000,0,40) | |
# regression | |
m1 <- lm(y~x) | |
summary(m1) | |
# triple data | |
dat <- data.frame(x=c(x,x,x),y=c(y,y,y),g=c(1:1000,1:1000,1:1000)) | |
# regressions | |
m2 <- lm(y~x, dat) # smaller StErrs | |
# cluster robust standard error function | |
robust.se <- function(model, cluster){ | |
require(sandwich) | |
require(lmtest) | |
M <- length(unique(cluster)) | |
N <- length(cluster) | |
K <- model$rank | |
dfc <- (M/(M - 1)) * ((N - 1)/(N - K)) | |
uj <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum)); | |
rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N) | |
rcse.se <- coeftest(model, rcse.cov) | |
return(list(rcse.cov, rcse.se)) | |
} | |
m3 <- robust.se(m2,dat$g)[[2]] # StErrs now back to what they are | |
texreg(list(m1,m2,m2), | |
caption="The Importance of Clustering Standard Errors", | |
dcolumn=FALSE, | |
model.names=c("M1","M2","M3"), | |
override.se=list(summary(m1)$coef[,2], | |
summary(m2)$coef[,2], | |
m3[,2]), | |
override.pval=list(summary(m1)$coef[,4], | |
summary(m2)$coef[,4], | |
m3[,4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment