使用texreg(或类似的)为集群标准错误函数(cl)输出生成一个回归表。

时间:2022-01-25 14:57:00

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.

我一直在使用优秀的texreg软件包,从拟合的模型对象生成准备就绪的回归表,但它似乎不兼容用于调整集群标准错误的各种函数。一些假数据和代码给出了一个示例和一个错误消息。

Any thoughts on how to get the output that I want (similar to what I get from texreg)?

对于如何获得我想要的输出(类似于从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"’

(函数(类、fdef、mtable)中的错误:无法找到函数“提取”的继承方法用于签名“coeftest”

1 个解决方案

#1


3  

Section 5.5 of the package vignette offers a solution. The package vignette was published as an article in the Journal of Statistical Software. It can be found here: http://www.jstatsoft.org/v55/i08/.

软件包vignette的5.5节提供了一个解决方案。这篇文章发表在《统计软件杂志》上。可以在这里找到:http://www.jstatsoft.org/v55/i08/。

More specifically, the robust standard errors and p values have to be extracted from your result matrix and handed over to texreg via the override.se and override.pval arguments:

更具体地说,健壮的标准错误和p值必须从结果矩阵中提取出来,并通过覆盖传递给texreg。se和覆盖。pval参数:

se <- result[, 2]
pval <- result[, 4]
screenreg(    # display the results in the R console
    m, 
    omit.coef = "IDs", 
    override.se = se, 
    override.pval = pval
)
texreg(       # for LaTeX output
    m, 
    omit.coef = "IDs", 
    override.se = se, 
    override.pval = pval
)

Another way to do it would be to extract the coefficients from your original model, save them into a texreg object, manipulate this object, and then hand it over to the texreg function:

另一种方法是从原始模型中提取系数,将它们保存到一个texreg对象中,操作这个对象,然后将其交给texreg函数:

tr <- extract(m)
tr@pvalues <- result[, 4]
tr@se <- result[, 2]
screenreg(tr)  # or texreg including your original arguments...

#1


3  

Section 5.5 of the package vignette offers a solution. The package vignette was published as an article in the Journal of Statistical Software. It can be found here: http://www.jstatsoft.org/v55/i08/.

软件包vignette的5.5节提供了一个解决方案。这篇文章发表在《统计软件杂志》上。可以在这里找到:http://www.jstatsoft.org/v55/i08/。

More specifically, the robust standard errors and p values have to be extracted from your result matrix and handed over to texreg via the override.se and override.pval arguments:

更具体地说,健壮的标准错误和p值必须从结果矩阵中提取出来,并通过覆盖传递给texreg。se和覆盖。pval参数:

se <- result[, 2]
pval <- result[, 4]
screenreg(    # display the results in the R console
    m, 
    omit.coef = "IDs", 
    override.se = se, 
    override.pval = pval
)
texreg(       # for LaTeX output
    m, 
    omit.coef = "IDs", 
    override.se = se, 
    override.pval = pval
)

Another way to do it would be to extract the coefficients from your original model, save them into a texreg object, manipulate this object, and then hand it over to the texreg function:

另一种方法是从原始模型中提取系数,将它们保存到一个texreg对象中,操作这个对象,然后将其交给texreg函数:

tr <- extract(m)
tr@pvalues <- result[, 4]
tr@se <- result[, 2]
screenreg(tr)  # or texreg including your original arguments...