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.
…looks very good!
Nice, but no changelog posted on CRAN? And no changelog in the .tar file?
thanks a lot!
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?
Look into the package “xtable”, this allows you to output the table as hmtl, perfect for use with markdown in Rstudio for instance!
Or my “pander” package that creates markdown tables even in PHP MarkdownExtra/rmarkdown format — see e.g.: http://stackoverflow.com/a/16926950/564164
I tried to do this to some degree with mutable:
https://github.com/erikriverson/mutable
Awesome package! I’m sure I’ll be using it soon 🙂
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?
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.
Same here!
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
is it possible to create a .tex file with the table instead of writing the table to the screen?
Thanks!
Fantastic work.
Quick question – is it possible to display number of NAs in a summary table?
When i do stargazaer(dataframe) it omits the rows. How do I keep the rows?
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.
Hi, thanks for this package! I was wondering if stargazer had an option for long tables?
I have trouble reading the table.
When I type:
library(stargazer)
stargazer(attitude)
The output is like:
% Table created by stargazer v.5.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Tue, May 05, 2015 – 14:07:16
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lccccc}
\\[-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}{Max} \\
\hline \\[-1.8ex]
rating & 30 & 64.633 & 12.173 & 40 & 85 \\
complaints & 30 & 66.600 & 13.315 & 37 & 90 \\
privileges & 30 & 53.133 & 12.235 & 30 & 83 \\
learning & 30 & 56.367 & 11.737 & 34 & 75 \\
raises & 30 & 64.633 & 10.397 & 43 & 88 \\
critical & 30 & 74.767 & 9.895 & 49 & 92 \\
advance & 30 & 42.933 & 10.289 & 25 & 72 \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
Could anybody help me fix it ? I am using a Mac and I just installed MacTax.
Thanks!
What are you trying to fix though? Can you be more specific? This is what you expect from the output, it gives you LaTeX code that will create a table when compiled (you do have to compile it). Or are you getting some errors when compiling?
What are you trying to fix though? Can you be more specific? This is what you expect from the output, it gives you LaTeX code that will create a table when compiled (you do have to compile it within a LaTeX document). Or are you getting some errors when compiling?
Is there an implementation of stargazer or similar function for an object of type ‘geeglm’ for modelling repeated measures using a generalized linear model? I tried but got the error ‘Error: Unrecognized object type.’.
Thankyou very much Marek Hlavac you make a great contribution !
thanks a bunch, Marek. This looks wonderful. I wonder if there is anything similar available for JAGS output?
Hy!
I get the same problem like Maxim Kovalenko. Can you let me knwo how to fix it? thanx in advance 🙂
i have used this package but this is not giving me output as tables. i have used for regression