From now on, we’ll be producing a lot of tables showing results from your models. We’ll talk in the spring semester about alternative ways of presenting results, but the humble table is always lurking somewhere even behind the fanciest new visualization. The good news is that producing your second, third, or _n_th table is pretty easy in R. The bad news is that the first one is kind of a pain. However, numerous packages exist that make the task much easier than it was even a few years ago; this note introduces one such package, stargazer.
Begin by installing it: install.packages("stargazer"); like all package installs, this is a one-time thing.
Let’s use the swiss dataset:
library(stargazer) # remember we have to call packages every time
data(swiss)
lm1 <- lm(Fertility~Education+Catholic,data=swiss)
lm2 <- lm(Fertility~Education+Catholic+Examination,data=swiss)
lm3 <- lm(Fertility~Education+Catholic+Examination+Agriculture,data=swiss)
For informal investigation of results, using summary(lm1) and so on is okay, but when we present final results or when we just want to show multiple models at a glance to ourselves, a table is more useful.
stargazer(lm1,lm2,lm3,type="html",
dep.var.labels="Swiss Fertility Rates",
covariate.labels=c("Pct Beyond Primary Education",
"Pct Catholic",
"Pct With Highest Mark on Exam",
"Pct Farmers"))
| Dependent variable: | |||
| Swiss Fertility Rates | |||
| (1) | (2) | (3) | |
| Pct Beyond Primary Education | -0.788*** | -0.760*** | -0.962*** |
| (0.129) | (0.199) | (0.195) | |
| Pct Catholic | 0.111*** | 0.106** | 0.124*** |
| (0.030) | (0.040) | (0.037) | |
| Pct With Highest Mark on Exam | -0.054 | -0.261 | |
| (0.289) | (0.274) | ||
| Pct Farmers | -0.221*** | ||
| (0.074) | |||
| Constant | 74.234*** | 75.016*** | 91.055*** |
| (2.352) | (4.828) | (6.949) | |
| Observations | 47 | 47 | 47 |
| R2 | 0.575 | 0.575 | 0.650 |
| Adjusted R2 | 0.555 | 0.545 | 0.616 |
| Residual Std. Error | 8.331 (df = 44) | 8.424 (df = 43) | 7.736 (df = 42) |
| F Statistic | 29.705*** (df = 2; 44) | 19.380*** (df = 3; 43) | 19.482*** (df = 4; 42) |
| Note: | p<0.1; p<0.05; p<0.01 | ||
So let’s show you how to make a version of this, step by step. The code we’ll write will create an html file that you will open in Word and then copy and paste into your document.
IMPORTANT NOTE: R will save the resulting output file WHEREVER YOU TELL IT TO. That means that you should begin using setwd() to save your files to the proper directory. So if your file folder for materials related to this class is something like POLISCI499, then your code will now include a header like:
library(foreign) # to import datafiles
library(stargazer) # to use stargazer
setwd("~/Desktop/POLISCI499")
If you don’t do this, then you may find that you can’t find the output file without searching every time. There’s lots of tutorials available on setwd() and your computer’s OS’s directory system—this is valuable for you to know beyond this course in any case.
The other side of this is that you should write clear, simple, informative filenames. StupidHandoutAssignment.html isn’t really useful or distinguishing; Handout3Table1.html will be more useful. You’ll come up with ways of organizing this for yourself, but some thought about organizing files in advance will help you a lot in the long run.
The basic step is to run regression models, as earlier, and save them in distinct names (here, lm1, lm2, and lm3).
stargazer(lm1,lm2,lm3,
type="html",
out="HandoutTable1.html")
| Dependent variable: | |||
| Fertility | |||
| (1) | (2) | (3) | |
| Education | -0.788*** | -0.760*** | -0.962*** |
| (0.129) | (0.199) | (0.195) | |
| Catholic | 0.111*** | 0.106** | 0.124*** |
| (0.030) | (0.040) | (0.037) | |
| Examination | -0.054 | -0.261 | |
| (0.289) | (0.274) | ||
| Agriculture | -0.221*** | ||
| (0.074) | |||
| Constant | 74.234*** | 75.016*** | 91.055*** |
| (2.352) | (4.828) | (6.949) | |
| Observations | 47 | 47 | 47 |
| R2 | 0.575 | 0.575 | 0.650 |
| Adjusted R2 | 0.555 | 0.545 | 0.616 |
| Residual Std. Error | 8.331 (df = 44) | 8.424 (df = 43) | 7.736 (df = 42) |
| F Statistic | 29.705*** (df = 2; 44) | 19.380*** (df = 3; 43) | 19.482*** (df = 4; 42) |
| Note: | p<0.1; p<0.05; p<0.01 | ||
It’s really that simple: specify a filename (out), a type (you will always be using “html”), and the list of models (lm1, lm2, lm3), and that’s it.
However, that’s not going to be your last step for presentations. By default, R will use the variable names in your tables; that’s fine when we’re dealing with the swiss data, which has pretty variable names, but not great when we want civilians to interpret pwt_rgdpch or oipc_1k, variables that might (or not) mean something to you but mean nothing to anyone else. Fortunately, we can specify labels:
stargazer(lm1,lm2,lm3,
covariate.labels=c("Pct Beyond Primary Education",
"Pct Catholic",
"Pct With Highest Mark on Exam",
"Pct Farmers"),
type="html",
out="HandoutTable2.html")
| Dependent variable: | |||
| Fertility | |||
| (1) | (2) | (3) | |
| Pct Beyond Primary Education | -0.788*** | -0.760*** | -0.962*** |
| (0.129) | (0.199) | (0.195) | |
| Pct Catholic | 0.111*** | 0.106** | 0.124*** |
| (0.030) | (0.040) | (0.037) | |
| Pct With Highest Mark on Exam | -0.054 | -0.261 | |
| (0.289) | (0.274) | ||
| Pct Farmers | -0.221*** | ||
| (0.074) | |||
| Constant | 74.234*** | 75.016*** | 91.055*** |
| (2.352) | (4.828) | (6.949) | |
| Observations | 47 | 47 | 47 |
| R2 | 0.575 | 0.575 | 0.650 |
| Adjusted R2 | 0.555 | 0.545 | 0.616 |
| Residual Std. Error | 8.331 (df = 44) | 8.424 (df = 43) | 7.736 (df = 42) |
| F Statistic | 29.705*** (df = 2; 44) | 19.380*** (df = 3; 43) | 19.482*** (df = 4; 42) |
| Note: | p<0.1; p<0.05; p<0.01 | ||
An important note here: Run one version of your table without the fancy labels and hold on to that as a reference when you begin to add labels. Order matters (stargazer adds labels in the same order in which variables are added) and so you want to make sure the labels match up with the variables.
Sometimes, you’ll want to have two dependent variables in the same table—for instance, models predicting Fertility and Infant Mortality. You can visually present this by adding dependent variable labels:
fert.lm1 <- lm(Fertility~Education+Catholic+Examination,data=swiss)
fert.lm2 <- lm(Fertility~Education+Catholic+Examination+Agriculture,data=swiss)
infmort.lm1 <- lm(Infant.Mortality~Education+Catholic+Examination,data=swiss)
infmort.lm2 <- lm(Infant.Mortality~Education+Catholic+Examination+Agriculture,data=swiss)
stargazer(fert.lm1,fert.lm2,infmort.lm1,infmort.lm2,
dep.var.labels = c("Fertility Rate", "Infant Mortality"),
covariate.labels=c("Pct Beyond Primary Education",
"Pct Catholic",
"Pct With Highest Mark on Exam",
"Pct Farmers"),
omit.stat=c("f"), # omits F statistic to narrow the table
type="html",
out="HandoutTable3.html")
| Dependent variable: | ||||
| Fertility Rate | Infant Mortality | |||
| (1) | (2) | (3) | (4) | |
| Pct Beyond Primary Education | -0.760*** | -0.962*** | -0.043 | -0.084 |
| (0.199) | (0.195) | (0.070) | (0.073) | |
| Pct Catholic | 0.106** | 0.124*** | 0.015 | 0.019 |
| (0.040) | (0.037) | (0.014) | (0.014) | |
| Pct With Highest Mark on Exam | -0.054 | -0.261 | 0.040 | -0.002 |
| (0.289) | (0.274) | (0.101) | (0.103) | |
| Pct Farmers | -0.221*** | -0.045 | ||
| (0.074) | (0.028) | |||
| Constant | 75.016*** | 91.055*** | 19.138*** | 22.413*** |
| (4.828) | (6.949) | (1.692) | (2.602) | |
| Observations | 47 | 47 | 47 | 47 |
| R2 | 0.575 | 0.650 | 0.040 | 0.097 |
| Adjusted R2 | 0.545 | 0.616 | -0.027 | 0.011 |
| Residual Std. Error | 8.424 (df = 43) | 7.736 (df = 42) | 2.952 (df = 43) | 2.896 (df = 42) |
| Note: | p<0.1; p<0.05; p<0.01 | |||
Note that Stargazer defaults to providing standard errors in parantheses below the beta coefficient estimate. You can fiddle with that to show p-values or t-statistics instead, but since the standard error is the more useful quantity, it’s okay to stick with the defaults.
This makes presenting results much easier. From here, you’d open the file you outputted and copy it into Word.
There are lots of stargazer tutorials on the Web (remember to add the letter r to any search for this, otherwise you’ll end up looking at astronomy guides). I like the help files, available with ?stargazer.