4

我一直在运行一堆不同的回归模型,现在想将他们的估计值放入 LaTeX 表中。为了使不同的规格具有可比性,我想使用包装或产品中的那种表,即不同模型显示在列中,这些模型的参数估计显示在适当的行中outreg。这就是我所拥有的:rockchalkmtablememisc

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 个级别和相应大量系数的因子。我想做的是要么从表中省略与该因子相关的系数,要么(为了奖励积分!)以简单的“是”或“否”表明该因子在模型中使用。所以,我理想的输出是这样的:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %
%
% 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}

这可能吗?

4

1 回答 1

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

瞧瞧

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)
于 2012-06-14T20:49:14.097 回答