R-statistics blog

{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)

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)

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)

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:


 

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.

Exit mobile version