忽略mtable/outreg类型表中的一些系数 [英] omit some coefficients from mtable/outreg-type table

查看:58
本文介绍了忽略mtable/outreg类型表中的一些系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行许多不同的回归模型,现在想将它们的估计值放入LaTeX表中.为了使不同的规范具有可比性,我想使用rockchalk包中的outregmemisc中的mtable生成的表的类型,即,其中的不同模型显示在列中,而参数估计值来自这些模型显示在适当的行中.这就是我所拥有的:

I've been running a bunch of different regression models and would now like to get their estimates into a LaTeX table. To make the different specifications comparable I would like to use the kind of table that outreg from the rockchalk package or mtable from memisc produce, i.e. one in which the different models are shown in columns and parameter estimates from those models are shown in the appropriate rows. This is what I've got:

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)

这已经足够好了,但是我有一个大约200个水平的系数和相应的大量系数.我想做的是从表中忽略与此因子相关的系数,或者(为了奖励积分!)以表明该因子在模型中使用了简单的是"或否".所以,我的理想输出是这样:

This works well enough, but I've got a factor with roughly 200 levels and a correspondingly large number of coefficients. What I'd like to do is to either omit the coefficients associated with this factor from the table or (for bonus points!) to show that the factor was used in the model with a simple 'yes' or 'no'. So, my ideal output would be this:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %
%
% 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.315^{***} &&    4.235     && 10.338^{***}\\
                               &  &  (0.537)     &&   (3.276)    &&  (0.468)    \\
x                              &  &  1.976^{***} &&    2.398     &&  1.858^{***}\\
                               &  &  (0.238)     &&   (1.530)    &&  (0.443)    \\
z                              &  &  5.389^{***} &&              &&  5.359^{***}\\
                               &  &  (0.226)     &&              &&  (0.463)    \\
group                          &  &   yes        &&    yes       &&     no      \\
\midrule
sigma                          &  &     0.929    &&     5.981    &&     2.092   \\
R-squared                      &  &     0.984    &&     0.265    &&     0.891   \\
F                              &  &   129.485    &&     1.009    &&    69.306   \\
p                              &  &     0.000    &&     0.448    &&     0.000   \\
N                              &  &    20        &&    20        &&    20       \\
\bottomrule
\end{tabular}

这可能吗?

推荐答案

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

Just selecting the first three coefficients is pretty simple:

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


奖励"问题(即添加一个手工绘制的描述组"的第四行)需要做更多的工作:


The "bonus" question (i.e. adding a hand-built 4th row describing "group") requires a bit more work:

## 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())将组信息添加到数组中,我发现它更干净:

Here's a version I like even better. It addresses the OP's 1st comment below, and uses abind() (like rbind() for arrays) to add the group info to the array, which I find to be cleaner:

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)

这篇关于忽略mtable/outreg类型表中的一些系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆