I want to make a table with grouped columns via texreg
. I can only see options for grouped rows (groups
).
Here's an example:
set.seed(01349)
DF <- data.frame(y = rnorm(100), x1A = rnorm(100), x2A = rnorm(100),
x1B = rnorm(100), x2B = rnorm(100))
regs <- lapply(paste0("x", 1:2, c("A", "A", "B", "B")), function(x)
lm(paste0("y ~ ", x), data = DF))
Here's as close as I can get with plain texreg
:
texreg(regs, custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2"))
With LaTeX output:
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
& 1 & 2 & 1 & 2 \\
\hline
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \\
& $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \\
x & $0.02$ & $0.07$ & $0.13$ & $-0.11$ \\
& $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \\
\hline
R$^2$ & 0.00 & 0.00 & 0.01 & 0.01 \\
Adj. R$^2$ & -0.01 & -0.01 & 0.00 & -0.00 \\
Num. obs. & 100 & 100 & 100 & 100 \\
RMSE & 1.18 & 1.17 & 1.17 & 1.17 \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
I'd prefer an extra line (highlighted with %
comments):
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
%*************A HEADER LINE HERE*********************
& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} \\ %
%****************************************************
& 1 & 2 & 1 & 2 \\
\hline
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \\
& $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \\
x & $0.02$ & $0.07$ & $0.13$ & $-0.11$ \\
& $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \\
\hline
R$^2$ & 0.00 & 0.00 & 0.01 & 0.01 \\
Adj. R$^2$ & -0.01 & -0.01 & 0.00 & -0.00 \\
Num. obs. & 100 & 100 & 100 & 100 \\
RMSE & 1.18 & 1.17 & 1.17 & 1.17 \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
Am I missing something, or is there no built-in way to do this?
My workaround is:
x <- capture.output(texreg(
regs, custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2")))
x[6] <- paste0("& \\multicolumn{2}{c}{A} & \\multicolumn{2}{c}{B} \\\\ \n", x[6])
cat(x, sep = "\n")
But that's obviously a bit duct-tape-y.