省略mtable / outreg-type表中的一些系数

时间:2012-06-14 19:52:29

标签: r latex

我一直在运行一系列不同的回归模型,现在想把他们的估计值放到LaTeX表中。为了使不同的规格具有可比性,我想使用来自outreg包的rockchalk或来自mtable的{​​{1}}生成的表格,即不同模型的表格以列显示,这些模型的参数估计显示在相应的行中。这就是我所拥有的:

memisc

这很好用,但我有一个大约有200个级别和相应大量系数的因子。我想做的是从表中省略与该因子相关的系数或(对于奖励积分!)来表明该因子在模型中使用时带有简单的“是”或“否”。所以,我的理想输出是:

df <- data.frame(x=rnorm(20),
                 z=rnorm(20),
                 group=gl(5,4,20,labels=paste('group',rep(1:5))))
df$y = 5 + 2*df$x + 5*df$z + rep(c(3.2,5,6.2,8.2,5),each=4) + rnorm(20)

model1 <- lm(y ~ x + z + factor(group),data=df)
model2 <- lm(y ~ x + factor(group),data=df)
model3 <- lm(y ~ x + z,data=df)

library(memisc)

reg.table <- mtable("Model 1"=model1,"Model 2"=model2,"Model 3"=model3,
                summary.stats=c("sigma","R-squared","F","p","N"))

toLatex(reg.table)

这可能吗?

1 个答案:

答案 0 :(得分:5)

只选择前三个系数非常简单:

reg.table$coefficients <- reg.table$coefficients[,,1:3,,drop=FALSE]
toLatex(reg.table)

“奖金”问题(即添加手工制作的第4行描述“群组”)需要更多的工作:

## Select the first three coeffients + one to be modified
reg.table$coefficients <- reg.table$coefficients[,,1:4,,drop=FALSE]

## Make a copy of all the coefficients, and in the copy, modify the 4th
j <- reg.table$coefficients
j[,,4,] <- c("yes", "", "yes", "", "no", "")
dimnames(j)[[3]][4] <- "group"

## Put the modified coefficients back into `reg.table`
reg.table$coefficients <- j

et voila

toLatex(reg.table)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Calls:
% Model 1:  lm(formula = y ~ x + z + factor(group), data = df) 
% Model 2:  lm(formula = y ~ x + factor(group), data = df) 
% Model 3:  lm(formula = y ~ x + z, data = df) 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tabular}{lcD{.}{.}{7}cD{.}{.}{7}cD{.}{.}{7}}
\toprule
&&\multicolumn{1}{c}{Model 1} && \multicolumn{1}{c}{Model 2} && \multicolumn{1}{c}{Model 3}\\
\midrule
(Intercept) &  &   8.830^{***} &&   9.846^{**}  && 10.342^{***} \\
            &  &   (0.626)     &&    (3.272)    &&   (0.442)    \\
x           &  &   2.047^{***} &&     1.765     &&  1.937^{***} \\
            &  &   (0.244)     &&    (1.276)    &&   (0.319)    \\
z           &  &   5.138^{***} &&               &&  4.847^{***} \\
            &  &   (0.267)     &&               &&   (0.372)    \\
group       &  &  yes          &&   yes         &&   no         \\
            &  &               &&               &&              \\
\midrule
sigma       &  &     1.204     &&     6.310     &&      1.812   \\
R-squared   &  &     0.975     &&     0.270     &&      0.927   \\
F           &  &    85.576     &&     1.033     &&    107.717   \\
p           &  &     0.000     &&     0.436     &&      0.000   \\
N           &  &    20         &&    20         &&     20       \\
\bottomrule
\end{tabular}

修改

这是我更喜欢的版本。它解决了OP在下面的第一条评论,并使用abind()(如数组rbind())将组信息添加到数组中,我发现它更清晰:

library(abind)

j <- reg.table$coefficients

groupFac <- array(c("yes", "", "yes", "", "no", ""), dim=c(2,1,3))
nonGroupFacs <- which(!grepl("group", dimnames(j)[[3]]))
j <- j[,,nonGroupFacs,,drop=FALSE]
j <- abind(j, groupFac, along=3)
dimnames(j)[[3]][length(nonGroupFacs)+1] <- "group"

reg.table$coefficients <- j

toLatex(reg.table)