0
votes

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.

1
texreg was made for lm objects and will only accept these. As mentioned in this post, you can simply create your individual lm objects by taking an already existing lm object and manipulate its numbers to characters. maybe textreg will accept this. emptyLM <- reg1; emptyLM$coefficients[1:2] <- as.character(rep( "",2)) and so on. Then call texreg(list(reg1,reg2,emptyLM,reg3). Good luck! - nilsole
the last possibility of course would be to manipulate the textreg function itself according to your needs. - nilsole
@nilsole: This is wrong. texreg was made for all sorts of model types. It currently supports more than 70 of them and is customizable. Customizable means you can easily write new extensions via providing methods for the generic extract function as described in my answer here: stackoverflow.com/questions/38894044/…. Forcing coefficients into lm containers or changing the texreg function is absolutely the wrong way to do it. - Philip Leifeld

1 Answers

2
votes

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)).