{stargazer} package for beautiful LaTeX tables from R statistical models output

stargazer is a new R package that creates LaTeX code for well-formatted regression tables, with multiple models side-by-side, as well as for summary statistics tables. It can also output the content of data frames directly into LaTeX. Compared to available alternatives, stargazer excels in three regards:  its ease of use, the large number of models it supports, and its beautiful aesthetics.

Ease of use

stargazer was designed with the user’s comfort in mind. The learning curve is very mild and all arguments are very intuitive, so that even a beginning user of R or LaTeX can quickly become familiar with the package’s many capabilities. The package is intelligent, and tries to minimize the amount of effort the user has to put into adjusting argument values. If stargazer is given a set of regression model objects, for instance, the package will create a side-by-side regression table. By contrast, if the user feeds it a data frame, stargazer will know that the user is most likely looking for a summary statistics table or – if the summary argument is set to false – wants to output the content of the data frame.

A quick reproducible example shows just how easy stargazer is to use. You can install stargazer from CRAN in the usual way:

install.packages("stargazer")
library(stargazer)

To create a summary statistics table from the ‘attitude’ data frame (which should be available with your default installation of R), simply run the following:

stargazer(attitude)

stargazer_summ_stat

To output the contents of the first four rows of same data frame, specify the part of the data frame you would like to see, and set the summary option to FALSE:

stargazer(attitude[1:4,], summary=FALSE)

stargazer_data_frame

Now, let us try to create a simple regression table with three side-by-side models – two Ordinary Least Squares (OLS) and one probit regression model – using the lm() and glm() functions. We can set the align argument to TRUE, so that coefficients in each column are aligned along the decimal point:

## 2 OLS models
linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
 
## create an indicator dependent variable, and run a probit model
 
attitude$high.rating <- (attitude$rating > 70)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit"))
 
stargazer(linear.1, linear.2, probit.model, title="Regression Results", align=TRUE)

stargazer_regression

Many supported models

stargazer supports objects from the most widely used statistical functions and packages. These include objects from betareg (betareg), coxph (survival), clm (ordinal), clogit (survival), ergm (ergm),gam (mgcv), gee (gee), glm (stats), glmer (lme4), gls (nlme), hurdle (pscl), ivreg (AER), lm (stats), lmer (lme4), lmrob (robustbase), multinom (nnet), nlmer (lme4), plm (plm), pmg (plm), polr (MASS), rlm (MASS), svyglm (survey), survreg (survival), tobit (AER), zeroinfl (pscl), as well as from the implementation of these in Zelig. In addition, stargazer also supports several Zelig models for social network analysis: cloglog.net, gamma.net, probit.net, and logit.net. The number of models and objects can stargazer can accommodate puts it ahead of most of the alternative R-to-LaTeX options. As the development of the package continues, this list will continue expanding to include linear mixed effects models, matching models, as well as new, user-made, or customized statistical models.

Beautiful aesthetics

stargazer is very pleasing to the eye, and allows the user to customize the formatting of the resulting table, including all variable labels. Below is an example of a good-looking, complex regression table created by stargazer:

stargazer_complex_table
 

If you’d like to create tables that look like those from your discipline’s leading journal, stargazer can help you with that as well. You can use the style argument to choose a template of your choice. Economics and management scholars can thus create tables that resemble those published in the American Economic Review, in the Quarterly Journal of Economics, or in Administrative Science Quarterly. Political scientists can avail themselves of templates based on the American Political Science Review, the American Journal of Political Science, and on International Organization. For sociologists and demographers, the American Sociological Review, the American Sociological Reviews and Demography are available.

stargazer, of course, is not the only R package that creates LaTeX code from R statistical output. Other packages with similar capabilities include apsrtable, xtable, memisc, texreg and outreg. Each of these has its own strengths and weaknesses, and users should explore all of them to find the best fit for their needs.

Some extra Q&A with Marek Hlavac (the package author)

What was your motivation for starting the package? (self use, for students, for other people etc.)

As a doctoral student in Political Economy and Government at Harvard University, I saw an urgent need for an easy-to-use tool to create well-formatted stargazer tables. Although other packages were available for this task, none of them combined a large number of supported models, good aesthetics, and simplicity of use in a way that I thought would be ideal.

Why is the “summary” parameter turned on as default for data.frames?

In published papers in the social sciences, summary statistics table are found more commonly than direct print-outs of chunks of data sets. For this reason, I thought the stargazer package should default to the user’s most likely need.

This has been a guest post by Marek Hlavac, the author of the {stargazer} R package for beautiful LaTeX tables from R’s statistical models’ outputs.

Finishing note: You (the readers) are invited to leave a comment to the author, or suggest your own guest post here, by contacting me.

  • http://blog.dakoller.net/ Daniel Koller

    …looks very good!

  • Pingback: stargazer: a package for beautiful latex tables from r statistical models output › musiker auf der bühne()

  • Jeremy

    Nice, but no changelog posted on CRAN? And no changelog in the .tar file?

  • roth

    thanks a lot!

  • Brian

    It seems that this package and many similar R routines combine what could be two separate steps. First, they take data (or other R-objects) along with simple formatting parameters and construct a generalized table (including such things as merged cells, and per-cell formatting). Second, they output that table to a back-end (LaTeX in this case). If these steps were formally separated, users could tweak formatting (making changes to the generalized table) and could provide new back-ends (e.g. HTML, Excel, etc.). Do any of these packages do this?

    • Tormod Bøe

      Look into the package “xtable”, this allows you to output the table as hmtl, perfect for use with markdown in Rstudio for instance!

    • Erik

      I tried to do this to some degree with mutable:

      https://github.com/erikriverson/mutable

  • Josh

    Awesome package! I’m sure I’ll be using it soon :)

  • Roberto

    These tables really look nice. Moreover it is really convenient tool for those who are no experts in TeX. Is there any chance for the user to modify the command such that standard errors are given (instead of t-statistics) and to report a Mc Fadden R² for Probit Models?

  • Pingback: texreg: A package for beautiful and easily customizable LaTeX regression tables from R « DiffusePrioR()

  • http://www.facebook.com/profile.php?id=692633374 Maxim Kovalenko

    I am not sure if this is a bug, but when I try to output a dataframe with decimal numbers, I get an error:

    > myframe
    V1 V2
    1 0.457 41
    2 23.000 57
    3 28.500 39
    4 45.000 58
    5 49.000 31

    > stargazer(myframe,summary=FALSE)
    % Table created by StarGazer v.3.0 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
    % Date and time: ma, jan 28, 2013 – 14:33:06
    begin{table}[htb] centering
    caption{}
    label{}
    footnotesize
    begin{tabular}{@{extracolsep{5pt}} c c }
    \[-1.8ex]hline
    hline \[-1.8ex]
    V1 & V2 \
    hline \[-1.8ex]
    Error in .iround(object[y, x], .how.much.to.round) : object ‘.how.much.to.round’ not found

    When I use the same dataframe with integers only, it works fine.

  • http://www.facebook.com/gd047 George Dontas

    Is there a way to embed stargazer output into an .Rmd file? Watch this question http://stackoverflow.com/questions/14797440/include-apsrtable-or-stargazer-output-in-an-rmd-file

  • ignacio martinez

    is it possible to create a .tex file with the table instead of writing the table to the screen?
    Thanks!

  • Pingback: Write MS-Word documents using R (with as little overhead as possible)R-statistics blog | R-statistics blog()

  • radek

    Fantastic work.
    Quick question – is it possible to display number of NAs in a summary table?

  • rasmus

    When i do stargazaer(dataframe) it omits the rows. How do I keep the rows?

  • http://twitter.com/ananelson Ana Nelson

    I blogged about ways to get Stargazer output into a file for including in LaTeX documents: http://blog.dexy.it/523 – easier if you use http://dexy.it but not required.

  • Pingback: Tailor Your Tables with stargazer: New Features for LaTeX and Text Output | R-statistics blog()