Professional Tables with Stargazer

[This article was first published on R on The Data Sandbox, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post was inspired by the following youtube video.

Introduction

Tables from regression models or just plain data in R are poorly organized. The have little formatting and are unfit for publishing. This point is illustrated with the CO2 dataset. This dataset looks at the CO2 uptake of six different types of grass.

data(co2)
mdl <- lm(CO2$uptake ~ CO2$Plant)
mdl
##
## Call:
## lm(formula = CO2$uptake ~ CO2$Plant)
##
## Coefficients:
## (Intercept) CO2$Plant.L CO2$Plant.Q CO2$Plant.C CO2$Plant^4
## 27.21310 -22.94230 -4.61669 4.66802 2.33920
## CO2$Plant^5 CO2$Plant^6 CO2$Plant^7 CO2$Plant^8 CO2$Plant^9
## 4.31450 -0.03902 -2.03649 -3.27825 -9.06702
## CO2$Plant^10 CO2$Plant^11
## 0.54631 1.91215

Stargazer

The solution is to use the Stargazer library. The stargazer library contains the stargazer function which does a lot of work for the user to create tables in Latex format. It can even compare multiple models in the same table.

library(stargazer)
mdl2 <- glm(CO2$uptake ~ CO2$Plant, family = Gamma)
stargazer(mdl, mdl2, title = "CO2 Uptake of plants model")
##
## % Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
## % Date and time: Fri, Mar 18, 2022 - 4:40:14 PM
## \begin{table}[!htbp] \centering
## \caption{CO2 Uptake of plants model}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}}lcc}
## \\[-1.8ex]\hline
## \hline \\[-1.8ex]
## & \multicolumn{2}{c}{\textit{Dependent variable:}} \\
## \cline{2-3}
## \\[-1.8ex] & \multicolumn{2}{c}{uptake} \\
## \\[-1.8ex] & \textit{OLS} & \textit{gamma} \\
## \\[-1.8ex] & (1) & (2)\\
## \hline \\[-1.8ex]
## Plant.L & $-$22.942$^{***}$ & 0.041$^{***}$ \\
## & (3.100) & (0.005) \\
## & & \\
## Plant.Q & $-$4.617 & 0.012$^{***}$ \\
## & (3.100) & (0.004) \\
## & & \\
## Plant.C & 4.668 & $-$0.010$^{**}$ \\
## & (3.100) & (0.005) \\
## & & \\
## Plant$\hat{\mkern6mu}$4 & 2.339 & $-$0.013$^{***}$ \\
## & (3.100) & (0.005) \\
## & & \\
## Plant$\hat{\mkern6mu}$5 & 4.314 & $-$0.011$^{**}$ \\
## & (3.100) & (0.004) \\
## & & \\
## Plant$\hat{\mkern6mu}$6 & $-$0.039 & 0.001 \\
## & (3.100) & (0.004) \\
## & & \\
## Plant$\hat{\mkern6mu}$7 & $-$2.036 & 0.011$^{**}$ \\
## & (3.100) & (0.005) \\
## & & \\
## Plant$\hat{\mkern6mu}$8 & $-$3.278 & 0.016$^{***}$ \\
## & (3.100) & (0.005) \\
## & & \\
## Plant$\hat{\mkern6mu}$9 & $-$9.067$^{***}$ & 0.019$^{***}$ \\
## & (3.100) & (0.005) \\
## & & \\
## Plant$\hat{\mkern6mu}$10 & 0.546 & 0.004 \\
## & (3.100) & (0.004) \\
## & & \\
## Plant$\hat{\mkern6mu}$11 & 1.912 & $-$0.001 \\
## & (3.100) & (0.004) \\
## & & \\
## Constant & 27.213$^{***}$ & 0.041$^{***}$ \\
## & (0.895) & (0.001) \\
## & & \\
## \hline \\[-1.8ex]
## Observations & 84 & 84 \\
## R$^{2}$ & 0.501 & \\
## Adjusted R$^{2}$ & 0.425 & \\
## Log Likelihood & & $-$290.414 \\
## Akaike Inf. Crit. & & 604.827 \\
## Residual Std. Error & 8.203 (df = 72) & \\
## F Statistic & 6.569$^{***}$ (df = 11; 72) & \\
## \hline
## \hline \\[-1.8ex]
## \textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
## \end{tabular}
## \end{table}
stargazer(CO2)
##
## % Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
## % Date and time: Fri, Mar 18, 2022 - 4:40:14 PM
## \begin{table}[!htbp] \centering
## \caption{}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}}lccccccc}
## \\[-1.8ex]\hline
## \hline \\[-1.8ex]
## Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Pctl(25)} & \multicolumn{1}{c}{Pctl(75)} & \multicolumn{1}{c}{Max} \\
## \hline \\[-1.8ex]
## conc & 84 & 435.000 & 295.924 & 95 & 175 & 675 & 1,000 \\
## uptake & 84 & 27.213 & 10.814 & 8 & 17.9 & 37.1 & 46 \\
## \hline \\[-1.8ex]
## \end{tabular}
## \end{table}

The stargazer function will return a latex table by default which requires a latex compiler. This post is written in R markdown and knitted to a html without a latex compiler so I can not demonstrate it natively. You can however knit a RMD file to a pdf which will utilize a latex compiler. I’ve copied these images of the tables from such a pdf file.

Model Table Data Table It is important to note that when displaying a dataframe in stargazer, the function will default to a summary of results. This performance can be undone will the summary = FALSE as a parameter of the function.

With some basic understanding of the La-Tex language, you can understand what stargazer is providing. Its nothing fancy that you couldn’t write yourself. It is the convenience at which you can create these tables that makes it important.

Extra Parameters

There are some additional parameters to the function that are useful for different applications.

  1. title - creates a title for your table
  2. dep.var.labels - accepts a list if you would like to rename your variables to make them easier to read
  3. covariate.labels - likewise for renaming covariates
  4. omit.stat - accepts a list of stats that you don’t want in your table
  5. type- this parameter will change the output format, the options include: “text” as plain text, “html” and “latex”
  6. order - lets you reorganize the order of covariates
To leave a comment for the author, please follow the link and comment on their blog: R on The Data Sandbox.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)