3

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

关于如何获得我想要的输出的任何想法(类似于我从中得到的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")

(function (classes, fdef, mtable) 中的错误:无法为签名 '"coeftest"' 的函数 'extract' 找到继承的方法</p>

4

1 回答 1

3

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

更具体地说,必须从result矩阵中提取稳健的标准误差和 p 值,并texreg通过override.seandoverride.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
)

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

tr <- extract(m)
tr@pvalues <- result[, 4]
tr@se <- result[, 2]
screenreg(tr)  # or texreg including your original arguments...
于 2013-10-17T07:10:16.893 回答