Andy Andy - 3 months ago 18
LaTeX Question

texreg - create empty column

I use texreg to create a LaTeX table from R. I would like to insert an empty column into the output (for stylistic reasons). I can of course do this manually, but it would great to be able to create this automatically. Here is an example:

library(texreg)

x1 <- rnorm(n=100,m=1,s=1)
x2 <- rnorm(n=100,m=1,s=1)
e <- rnorm(n=100,m=0,s=1)

y <- 0.1 + 0.3 * x1 + 0.4 * x2 + e

reg1 <- lm(y~x1)
reg2 <- lm(y~x2)
reg3 <- lm(y~x1+x2)

texreg(list(reg1,reg2,reg3)
,stars=c(0.01,0.05,0.1)
,digits=2
,dcolumn=T
,use.packages=F
,file="tabfile.tex")


This gives me the output:

\begin{table}
\begin{center}
\begin{tabular}{l D{.}{.}{3.5} D{.}{.}{3.5} D{.}{.}{3.5} }
\hline
& \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} & \multicolumn{1}{c}{Model 3} \\
\hline
(Intercept) & 0.59^{***} & 0.53^{***} & 0.05 \\
& (0.16) & (0.16) & (0.19) \\
x1 & 0.36^{***} & & 0.40^{***} \\
& (0.11) & & (0.10) \\
x2 & & 0.43^{***} & 0.48^{***} \\
& & (0.11) & (0.11) \\
\hline
R$^2$ & 0.10 & 0.13 & 0.25 \\
Adj. R$^2$ & 0.09 & 0.12 & 0.23 \\
Num. obs. & 100 & 100 & 100 \\
RMSE & 1.05 & 1.03 & 0.96 \\
\hline
\multicolumn{4}{l}{\scriptsize{$^{***}p<0.01$, $^{**}p<0.05$, $^*p<0.1$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}


What I would like to have instead is:

\begin{table}
\begin{center}
\begin{tabular}{lc D{.}{.}{3.5} D{.}{.}{3.5} D{.}{.}{3.5} }
\hline
&& \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} & \multicolumn{1}{c}{Model 3} \\
\hline
(Intercept) && 0.59^{***} & 0.53^{***} & 0.05 \\
&& (0.16) & (0.16) & (0.19) \\
x1 && 0.36^{***} & & 0.40^{***} \\
&& (0.11) & & (0.10) \\
x2 && & 0.43^{***} & 0.48^{***} \\
&& & (0.11) & (0.11) \\
\hline
R$^2$ && 0.10 & 0.13 & 0.25 \\
Adj. R$^2$ && 0.09 & 0.12 & 0.23 \\
Num. obs. && 100 & 100 & 100 \\
RMSE && 1.05 & 1.03 & 0.96 \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.01$, $^{**}p<0.05$, $^*p<0.1$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}


It would of course be even better, if there was a way to insert arbitrary empty columns.

Answer

You can insert new columns using the custom.columns argument of texreg. The new columns should be handed over as a named list. The custom.col.pos argument can be used to indicate where the columns should be located. For example, you could insert a new, empty column by adding the argument custom.columns = list("new column" = rep("", 3)).