I've been using the excellent package texreg
to produce LaTeX-ready regression tables from fitted model objects, but it doesn't seem to be compatible with various functions that adjust my standard errors for clustering. Some fake data and code gives an example and an error message below.
Any thoughts on how to get the output that I want (similar to what I get from texreg
)?
x = rnorm(1000)
IDs = ceiling(seq(from = .1, to = 10,length.out=1000))
s = c(rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100),rep(rnorm(1),100))
y = 3*x + 1.5^(IDs)*rnorm(n=1000,sd=s^2)*x + rnorm(1000)*.3
IDs = as.factor(IDs)
d = data.frame(x,IDs,y)
m = lm(y~IDs+x,data=d)
summary(m)
library(texreg)
texreg(m,omit.coef="IDs")
\begin{table}
\begin{center}
\begin{tabular}{l c }
\hline
& Model 1 \\
\hline
(Intercept) & $0.12$ \\
& $(4.50)$ \\
x & $5.28^{***}$ \\
& $(1.41)$ \\
\hline
R$^2$ & 0.02 \\
Adj. R$^2$ & 0.01 \\
Num. obs. & 1000 \\
\hline
\multicolumn{2}{l}{\scriptsize{\textsuperscript{***}$p<0.001$,
\textsuperscript{**}$p<0.01$,
\textsuperscript{*}$p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
cl <- function(dat,fm, cluster){
cluster = as.numeric(cluster)
attach(dat, warn.conflicts = F)
library(sandwich)
library(lmtest)
M <- length(unique(cluster))
N <- length(cluster)
K <- fm$rank
dfc <- (M/(M-1))*((N-1)/(N-K))
uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
coeftest(fm, vcovCL) }
result = cl(d,m,IDs)
texreg(result,omit.coef="IDs")
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"coeftest"’