6

I'm actually surprised to find that no one has asked this question. Here it goes.

I have a 3 models, first 2 computed for black people and the last one for whites. I produce a regression output with stargazer and over the first two models I write the column label "Black" using column.labels. I label the remaining column "White". So, 3 models, two with the same column label and the last with another one.

However, I want to also add another column label above the first model specifying "Males"(I also mean above the "Black" label) and add a similar label for the 2 last models specifying "Female".

I can't see a way to add the second columns labels above the Racial category.

library(stargazer)
data <-     data.frame(dv1=rnorm(100),dv2=rnorm(100),dv3=rnorm(100),race=sample(c(1,0)    ,100, replace = T),
               iv=sample(c(1,0),100,replace = T))

m1 <- lm(dv1 ~ + iv, data = data, subset = race == 1)
m2 <- lm(dv2 ~ + sqrt(iv), data = data, subset = race == 0)
m3 <- lm(dv3 ~ + iv, data = data, subset = race == 0)

models <- stargazer(m1,m2,m3, type = "text", column.labels = c("Blacks","Whites"), column.separate = c(2,1),
                    dep.var.labels.include = FALSE)
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
cimentadaj
  • 1,414
  • 10
  • 23

2 Answers2

2

I suspect the best you can do is not quite what you want, assuming I have understood the column attribution correctly....

     models <- stargazer(m1,m2,m3, type = "text", 
                         column.labels = c("Male Blacks","Female Blacks","Female Whites"),
                         column.separate = c(1,1,1), dep.var.labels.include = FALSE)

Using dep.var.caption you could try...

dep.var.caption="\b\b\b  Males ................... Females"

but IMHO this illustrates how confused a multi-row heading might look. Since the caption seems to be centre justified, the control characters and the dots are a kludge (spaces seem to be ignored).

Big Old Dave
  • 343
  • 2
  • 11
0

I encountered the same problem, I did some research and here is what I found :

You cannot do it directly from stargazer. However if you export it in latex format you can then modify the latex code and easily obtain the result you're looking for.

The base latex code coming from your stargazer function is : (I added the document class, package and the begin/end document so that it is readily readable)

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{3}{c}{\textit{Dependent variable:}} \\ 
\cline{2-4} 
 & Male Blacks & Female Blacks & Female Whites \\ 
\\[-1.8ex] & (1) & (2) & (3)\\ 
\hline \\[-1.8ex] 
 iv & 0.138 &  & 0.632$^{*}$ \\ 
  & (0.297) &  & (0.315) \\ 
  & & & \\ 
 sqrt(iv) &  & 0.246 &  \\ 
  &  & (0.339) &  \\ 
  & & & \\ 
 Constant & 0.182 & $-$0.182 & $-$0.103 \\ 
  & (0.206) & (0.255) & (0.237) \\ 
  & & & \\ 
\hline \\[-1.8ex] 
Observations & 54 & 46 & 46 \\ 
R$^{2}$ & 0.004 & 0.012 & 0.084 \\ 
Adjusted R$^{2}$ & $-$0.015 & $-$0.011 & 0.063 \\ 
Residual Std. Error & 1.091 (df = 52) & 1.139 (df = 44) & 1.060 (df = 44) \\ 
F Statistic & 0.216 (df = 1; 52) & 0.526 (df = 1; 44) & 4.020$^{*}$ (df = 1; 44) \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 

\end{document}

Now, I will highlight the modifications with %<========= :

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: ven., nov. 13, 2020 - 20:41:11
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 


 & \multicolumn{1}{c}{Male}  & \multicolumn{2}{c}{Female}      \\  %<=========== for 1 column and then for 2 columns
 \cline{2-2}   %<=========== aka a line from column 2 to 2
\cline{3-4}    %<===========  a line from column 3 to 4
 &  Blacks & Blacks &  Whites \\ 
\\[-1.8ex] & (1) & (2) & (3)\\ 
\hline \\[-1.8ex] 
 iv & 0.138 &  & 0.632$^{*}$ \\ 
  & (0.297) &  & (0.315) \\ 
  & & & \\ 
 sqrt(iv) &  & 0.246 &  \\ 
  &  & (0.339) &  \\ 
  & & & \\ 
 Constant & 0.182 & $-$0.182 & $-$0.103 \\ 
  & (0.206) & (0.255) & (0.237) \\ 
  & & & \\ 
\hline \\[-1.8ex] 
Observations & 54 & 46 & 46 \\ 
R$^{2}$ & 0.004 & 0.012 & 0.084 \\ 
Adjusted R$^{2}$ & $-$0.015 & $-$0.011 & 0.063 \\ 
Residual Std. Error & 1.091 (df = 52) & 1.139 (df = 44) & 1.060 (df = 44) \\ 
F Statistic & 0.216 (df = 1; 52) & 0.526 (df = 1; 44) & 4.020$^{*}$ (df = 1; 44) \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 


\end{document}


Dharman
  • 30,962
  • 25
  • 85
  • 135