reggie
reggie copied to clipboard
Proof of concept: reggie and texreg
This morning, another student came to my office asking how to produce tables with clustered standard errors. So here I am again.
I just wanted to point out a neat feature that I contributed to texreg a little while ago: When the package doesn't recognize the model type, it tries to extract info using broom. Thanks to that, we can easily present side-by-side estimates from reggie by creating a couple very simple broom helpers:
library(reggie)
library(texreg)
library(broom)
# Broom Helper Functions
tidy.reg <- function(x, ...) {
ret <- data.frame('term' = row.names(x$coefficients),
'estimate' = x$coefficients[, 1],
'std.error' = x$coefficients[, 2],
'statistic' = x$coefficients[, 3],
'p.value' = x$coefficients[, 4],
row.names = NULL,
stringsAsFactors = FALSE)
return(ret)
}
glance.reg <- function(x, ...) {
ret <- glance(x$model)
return(ret)
}
# Simulated Data
dat <- data.frame('x' = rnorm(1000),
'z' = sample(letters, 1000, replace = TRUE))
dat$y <- ifelse(dat$x + rnorm(1000) > 0, 1, 0)
# Fitting models
models <- list()
models[['GLM']] <- glm(y ~ x, data = dat, family = binomial())
models[['Cluster']] <- reg(y ~ x, data = dat, family = binomial(), vcov_cluster = ~ z)
models[['HC1']] <- reg(y ~ x, data = dat, family = binomial(), vcov_type = 'HC1')
# Summary
screenreg(models, digits = 4)
============================================================
GLM Cluster HC1
------------------------------------------------------------
(Intercept) -0.1355 -0.1355 -0.1355
(0.0774) (0.0762) (0.0774)
x 1.6487 *** 1.6487 *** 1.6487 ***
(0.1099) (0.1060) (0.1138)
------------------------------------------------------------
AIC 1013.3481 1013.3481 1013.3481
BIC 1023.1637 1023.1637 1023.1637
Log Likelihood -504.6741 -504.6741 -504.6741
Deviance 1009.3481 1009.3481 1009.3481
Num. obs. 1000
Deviance (Null) 1382.6922 1382.6922
df.null 999 999
DF Resid. 998 998
============================================================
*** p < 0.001, ** p < 0.01, * p < 0.05
Not perfect, but pretty close...