Generate multiple column names for multiple models using `stargazer()`

I am trying to get multiple column labels, but I have problems parsing it from stargazer(). I am trying to display different models estimated over different sub-samples and describe the partitions in the column names. However, it is not displaying it correctly.

I already checked Getting Stargazer Column labels to print on two or three lines? and Stargazer column on multiple lines with multiple models? questions, but they are not exactly what I need.

PS: My question's motivation comes from capturing the tree structure from this type of model in a table.

var1<-rnorm(100)
var2<-rnorm(100)
df<-data.frame(var1, var2)
mod<-lm(var1~var2)
library(stargazer)

stargazer(mod,mod,mod,mod,
          column.labels='Gender=Male Gender=Female   \\\\ & Age > 25  Age <= 25 Educ>12  Educ <=12')

Result

enter image description here

Desired output

This is how my LaTeX should look like.

% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: vie., ene. 15, 2021 - 13:58:54
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lcccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{4}{c}{\textit{Dependent variable:}} \\ 
\cline{2-5} 
\\[-1.8ex] & \multicolumn{2}{c}{Gender$=$Male} & \multicolumn{2}{c}{Gender$=$Female} \\ 
\\[-1.8ex] & \multicolumn{1}{c}{Age$>$25} & \multicolumn{1}{c}{Age$<=$25} &\multicolumn{1}{c}{Educ$>$12} & \multicolumn{1}{c}{Educ$<=12$} \\
\\[-1.8ex] & (1) & (2) & (3) & (4)\\ 
\hline \\[-1.8ex] 
 var2 & 0.006 & 0.006 & 0.006 & 0.006 \\ 
  & (0.101) & (0.101) & (0.101) & (0.101) \\ 
  & & & & \\ 
 Constant & $-$0.012 & $-$0.012 & $-$0.012 & $-$0.012 \\ 
  & (0.105) & (0.105) & (0.105) & (0.105) \\ 
  & & & & \\ 
\hline \\[-1.8ex] 
Observations & 100 & 100 & 100 & 100 \\ 
R$^{2}$ & 0.00004 & 0.00004 & 0.00004 & 0.00004 \\ 
Adjusted R$^{2}$ & $-$0.010 & $-$0.010 & $-$0.010 & $-$0.010 \\ 
Residual Std. Error (df = 98) & 1.045 & 1.045 & 1.045 & 1.045 \\ 
F Statistic (df = 1; 98) & 0.004 & 0.004 & 0.004 & 0.004 \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{4}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 

This is how it should look when compiled.

enter image description here

Upvotes: 2

Views: 1618

Answers (1)

Vincent
Vincent

Reputation: 17853

I don't know how to achieve this in stargazer, but it easy to do using the modelsummary and kableExtra packages (disclaimer: I am the modelsummary maintainer):

library(kableExtra)
library(modelsummary)

mod<-list(
  "(1)" = mod, 
  "(2)" = mod, 
  "(3)" = mod, 
  "(4)" = mod
)

modelsummary(mod, output = "latex") %>%
  add_header_above(c(" ", "Age>25" = 1, "Age<=25" = 1, "Educ>12" = 1, "Educ<=25" = 1)) %>%
  add_header_above(c(" ", "Gender=Male" = 2, "Gender=Female" = 2)) %>%
  add_header_above(c(" ", "Dependent variable:" = 4))

enter image description here

Upvotes: 4

Related Questions