I was wondering if someone came with a solution to show up the reference categories of categorical variables using stargazer?
library(stargazer)
Let us imagine that gear and carb are categorical variables
mtcars$gear = factor(mtcars$gear)
mtcars$carb = factor(mtcars$carb)
I run an ols with
lm1 = lm(disp ~ gear + carb, mtcars)
and stargaze the results.
stargazer(lm1, single.row = TRUE, omit.table.layout = "sn")
I get

However, I find myself always going back to the tex file to custom the reference categories to get

Basically, what I do is to add to the latex in between variables :
gear (ref = 3) & \\
\-\hspace{0.3cm} gear4 & $-$202.921$^{***}$ (22.477) \\
and so on.
Anyone had any idea if I can add these kind of lines in the stargazer function ?
Reference categories for discrete predictors. For a factor with several levels/categories, the “reference category” is the category that is dropped so that the design matrix X will have full rank. Here are a few more remarks about the effect of different choices of “reference category”.
Highlight the factor(s) for which you want the first level to be the reference. Then click the Options button and choose "Descending" under "Category Order for Factors". The first category will then be treated as the reference, as you will see in the parameter estimates in the output.
This can be done by typing “install. packages(“stargazer”)”, and then “library(stargazer)” in the next line. Installing Stargazer will only need to be done once, but the second command, which loads the package will need to be typed each session you wish to use it.
A stargazer is someone who studies the stars as an astronomer or astrologer.
You can achieve the output you want by providing covariate.labels to stargazer:
library(magrittr)
library(stringr)
library(stargazer)
covlabels <-
names(lm1$coefficients)[-1] %>%
if_else(str_sub(., 1, 4) == "gear" | str_sub(., 1, 4) == "carb", paste("\\-\\hspace{0.3cm}", .), .) %>%
if_else(str_sub(., 18, 24) == "gear4", paste("gear (ref=3) \\\\", .), .) %>%
if_else(str_sub(., 18, 24) == "carb2", paste("carb (ref=1) \\\\", .), .)
stargazer(lm1, single.row = TRUE, omit.table.layout = "sn", covariate.labels=covlabels)
yields
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Mon, Jan 08, 2018 - 3:18:09 AM
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & disp \\
\hline \\[-1.8ex]
gear (ref=3) \\ \-\hspace{0.3cm} gear4 & $-$202.921$^{***}$ (22.477) \\
\-\hspace{0.3cm} gear5 & $-$160.898$^{***}$ (36.282) \\
carb (ref=1) \\ \-\hspace{0.3cm} carb2 & 71.282$^{**}$ (27.919) \\
\-\hspace{0.3cm} carb3 & 25.574 (39.919) \\
\-\hspace{0.3cm} carb4 & 155.852$^{***}$ (27.355) \\
\-\hspace{0.3cm} carb6 & 55.672 (68.065) \\
\-\hspace{0.3cm} carb8 & 211.672$^{***}$ (68.065) \\
Constant & 250.226$^{***}$ (24.363) \\
\hline \\[-1.8ex]
\hline
\hline \\[-1.8ex]
\end{tabular}
\end{table}
If you are willing to accept my revised strategy, then extract the names of the xlevels-lidt-item in the lm1-object, and their associated first levels and substitute the pasted character values for the "(Intercept) value:
baselines = sapply( lm1$xlevels, "[[", 1)
names(lm1$coefficients)[1] = paste0( names(baselines), " = ", baselines,
collapse="; ")
I now get:
stargazer(lm1, single.row = TRUE, omit.table.layout = "sn")
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Nov 19, 2016 - 07:49:18
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & disp \\
\hline \\[-1.8ex]
gear = 3; carb = 1 & 250.226$^{***}$ (24.363) \\
gear4 & $-$202.921$^{***}$ (22.477) \\
gear5 & $-$160.898$^{***}$ (36.282) \\
carb2 & 71.282$^{**}$ (27.919) \\
carb3 & 25.574 (39.919) \\
carb4 & 155.852$^{***}$ (27.355) \\
carb6 & 55.672 (68.065) \\
carb8 & 211.672$^{***}$ (68.065) \\
\hline \\[-1.8ex]
\hline
\hline \\[-1.8ex]
\end{tabular}
\end{table}
I don't seem to have a properly configured Latex toolchain anymore, probably due to the "enhanced security features" Apple introduced in the last OSX "upgrade".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With