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)
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}
Is this possible?
Just selecting the first three coefficients is pretty simple:
The “bonus” question (i.e. adding a hand-built 4th row describing “group”) requires a bit more work:
et voila
Edit:
Here’s a version I like even better. It addresses the OP’s 1st comment below, and uses
abind()(likerbind()for arrays) to add the group info to the array, which I find to be cleaner: