Overview

This lesson introduces the stargazer package for presenting regression results.

Objectives

After completing this module, students should be able to:

  1. Use the stargazer package to present nice-looking regression results.

Reading

NA

1 Presenting regression results

In the previous module we covered multiple regression in R and interpreting the output. But after you’ve done your analysis and settled on your model, you will often have to present your results to others. Journals and reports demand something a little cleaner than the raw console output, but luckily, as usual there are R packages to make your life easier. Perhaps the most elegant is the stargazer package, which autmatically takes a regression output object and returns a beautifully formatted table in HTML or Latex.

Stargazer has many many options, which are described here and demonstrated via a few examples here.

Let’s look at a brief example, using the regression from the previous module:

anes_2008tr <- read.table("anes_2008tr.csv",sep=",",header=TRUE,stringsAsFactors=FALSE)
mr3 <- lm(ideology_con ~ age + gender_male + race_white + 
                          education + income + partyid_rep,data=anes_2008tr)
summary(mr3)

Call:
lm(formula = ideology_con ~ age + gender_male + race_white + 
    education + income + partyid_rep, data = anes_2008tr)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5688 -0.6376  0.0913  0.6234  3.8186 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  2.952996   0.105709  27.935  < 2e-16 ***
age          0.007132   0.001291   5.524 3.69e-08 ***
gender_male  0.037217   0.047631   0.781 0.434662    
race_white  -0.191276   0.052315  -3.656 0.000262 ***
education   -0.059686   0.015963  -3.739 0.000189 ***
income       0.024077   0.024092   0.999 0.317723    
partyid_rep  0.324924   0.012876  25.236  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.127 on 2315 degrees of freedom
Multiple R-squared:  0.2409,    Adjusted R-squared:  0.2389 
F-statistic: 122.4 on 6 and 2315 DF,  p-value: < 2.2e-16

That looks fine, but we can do better.

1.1 Stargazer in html

Be sure to install stargazer first

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

Now let’s use it to output HTML regression results. This can be done directly in RMarkdown using the “results=‘asis’” option: {r results='asis'} if your RMarkdown document outputs to HTML format (as is the case with these lectures):

stargazer(mr3, no.space=TRUE, dep.var.labels=c("Ideology"), 
          covariate.labels=c("Age","Gender (male)","Race (white)", 
                             "Education","Income","Party ID (R)"), 
          omit.stat=c("LL","ser","f"),header=FALSE,type="html")
Dependent variable:
Ideology
Age 0.007***
(0.001)
Gender (male) 0.037
(0.048)
Race (white) -0.191***
(0.052)
Education -0.060***
(0.016)
Income 0.024
(0.024)
Party ID (R) 0.325***
(0.013)
Constant 2.953***
(0.106)
Observations 2,322
R2 0.241
Adjusted R2 0.239
Note: p<0.1; p<0.05; p<0.01

As usual, the standard errors are in parentheses, and the stars indicate the p value level for that variable’s coefficient. But before going into the details of the stargazer options, note that there is currently a flaw in the html output: the p-values at the bottom of the table, which explain what the stars mean, are missing the stars! This is an error in the code at the moment. In any case, the Latex version looks much nicer, so let’s look at that.

1.2 Stargazer in latex

If you are kniting an RMarkdown document straight to latex, as hopefully most of you are by now for the homework, then you can again use the {r results='asis'} option in the code block containing the stargazer line, and this should embed the latex table directly in your homework or other document. Since these lectures are knit to html, that’s not an option here, but instead I’ve pasted an image of the latex table to examine:

Much prettier!

1.3 Stargazer options

stargazer(mr3, no.space=TRUE, dep.var.labels=c("Ideology"), 
          covariate.labels=c("Age","Gender (male)","Race (white)", 
                             "Education","Income","Party ID (R)"), 
          omit.stat=c("LL","ser","f"),header=FALSE)

Most of the options in the stargazer function are simple enough, and many more can be found in the documentation. Note that the previous version had type="html" as an option, whereas this one has nothing, since without specifying, latex is the default.

As you can see from the other options, we can provide the labels for the dependent variable and the independent variables very straightforwardly. The no.space option just tightens things up aesthetically; the header=FALSE option removes some comments appended to the table; the omit.stat option determines which statistics are included at the bottom – in this table we include the N and the \(R^2\), but not the ones in the omit.stat list, which would otherwise be included by default.

You of course can also run the stargazer(...) code directly in the console, and it outputs the raw latex (or html if you choose), so if you know how to create latex (or html) documents directly, you can just paste this code directly into your document. But for now, using the {r results='asis'} option in your RMarkdown (and knitting directly to latex – now’s a good time to start if you are still knitting to html!) works fine.

Finally, the align=TRUE option makes the numbers line up nicely with the decimal points all lined up (compare the latex above to the html on the previous page, where the numbers are centered but unaligned). Unfortunately, this option only works for latex output, and requires adding this to your RMarkdown header (at the top of your rmd file in between the --- marks):

header-includes:
   - \usepackage{dcolumn}

(Note that that the option “align=TRUE” doesn’t work for some versions Stargazer and RMarkdown.)