9

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.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198

2 Answers2

6

This may be late but still useful.

A new version of texreg (1.36.28) just came out on GitHub (not on CRAN yet, May 22nd, 2020). It adds the option custom.header to create groups of regressions.

Applied to your example, it creates:

library(texreg)

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

screenreg(
  regs, 
  custom.header = list("A" = 1:2, "B" = 3:4),
  custom.coef.names = c("Intercept", rep("x", 4)),
  custom.model.names = c("1", "2", "1", "2"),
)

=============================================
                   A                 B       
           ----------------  ----------------
           1        2        1        2      
---------------------------------------------
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)
---------------------------------------------
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    
=============================================
*** p < 0.001; ** p < 0.01; * p < 0.05

I used screenreg() to show the output more easily but it works with texreg() too.

bretauv
  • 7,756
  • 2
  • 20
  • 57
0

You can read code of the function by typing it into the console, or from the github website of the package texreg.

texreg
function (l, file = NULL, single.row = FALSE, stars = c(0.001, 
    0.01, 0.05), custom.model.names = NULL,...

This is the output of texreg for a table of 4 models:

\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 \\

I looked into the code line 469, the beginning of the table:

 string <- paste0(string, "\\begin{tabular}{", coldef, 
            "}", linesep)

Then I added some changes of my own:

string <- paste0(string, "\\begin{tabular}{", coldef, 
                 "}", linesep)
## Additions
string <- paste0(string, "\\\\[-1.8ex]\\hline", linesep)
string <- paste0(string, "& \\multicolumn{", length(l), 
                 "}{c}{\\textit{Dependent variable:}} \\\\", linesep)
string <- paste0(string, "\\cline{2-",length(modnames), "}", linesep)
string <- paste0(string, "\\\\[-1.8ex] & \\multicolumn{", length(l), 
                 "}{c}{", dep.var, "} \\\\", linesep)

Then, save the function with a different name for instance:

texreg2 <- function (l, file = NULL, single.row = FALSE, ...)

Now, the function requires internal functions from the package, so you need to attach your custom function to the namespace of the package in your environment. Easy peasy:

environment(texreg2) <- asNamespace('texreg')

Now you can call your new function. My additions include three lines and a name for the dependent variable, similar to stargazer.

texreg2(out, dep.var = "Normalize Citation Score")

\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\\[-1.8ex]\hline
& \multicolumn{4}{c}{\textit{Dependent variable:}} \\
\cline{2-5}
\\[-1.8ex] & \multicolumn{4}{c}{Normalize Citation Score} \\
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 \\

Finally, if you don't like this method, you can manipulate the output with regex, checkout this question .

Community
  • 1
  • 1
Mario GS
  • 859
  • 8
  • 22
  • 1
    indeed this is how I have been going about adding `multicolumn`s for some time now. I find it's a bit too idiosyncratic to have bothered writing it up into a function, and just handle it case-by-case. Thanks for the response. – MichaelChirico May 08 '17 at 19:55