Setting up a dataset for this cheatsheet allows me to spotlight two recent R packages created by Hadley Wickham. The first, dplyr, is a set of new tools for data manipulation. Using dplyr, I will extract flights and weather data from another new package called nycflights13. With this data I will show how to estimate a couple of regression models and nicely format the results into tables with stargazer.
Note: stargazer v. 5.1 does not play nicely with dplyr’s tbl_df class. As a temporary work-around I pipe the merged dataset to data.frame.
Code
library("dplyr")library("nycflights13")library("AER") # Applied Econometrics with Rlibrary("stargazer")daily <- flights %>%filter(origin =="EWR") %>%group_by(year, month, day) %>%summarise(delay =mean(dep_delay, na.rm =TRUE))daily_weather <- weather %>%filter(origin =="EWR") %>%group_by(year, month, day) %>%summarise(temp =mean(temp, na.rm =TRUE),wind =mean(wind_speed, na.rm =TRUE),precip =sum(precip, na.rm =TRUE))# Merge flights with weather data framesboth <-inner_join(daily, daily_weather) %>%data.frame() # Temporary fix# Create an indicator for quarterboth$quarter <-cut(both$month, breaks =c(0, 3, 6, 9, 12), labels =c("1", "2", "3", "4"))# Create a vector of class logicalboth$hot <-as.logical(both$temp >85)head(both)
We can use the both data frame to estimate a couple of linear models predicting the average delay out of Newark controlling for the weather. The first model will use only the weather variables and in the second I’ll add dummy variables indicating the quarter. I also estimate a third model, using using the ivreg command from package AER to demonstrate output with mixed models. The raw R output:
Since I’m using knitr and R markdown to create this webpage, in the code that follows I will include the stargazer option type = "html". stargazer is set to produce LaTeX output by default. If you desire LaTeX output, just remove the type option from the code below.
Also, while I have added an example for many of the available stargazer options, I have not included all of them. So while you’re likely to find a relevant example don’t assume if it’s not listed that stargazer can’t do it. Check the documentation for additional features and updates to the package. It is often the case that an omitted argument is specific for LaTeX output and I can’t demonstrate it here.
2.1 HTML formatting
It is possible to change the formatting of html tables generated with stargazer via an html style sheet. See the R Markdown documentation about incorporating a custom CSS.
When supplied a data frame, by default stargazer creates a table with summary statistics. If the summary option is set to FALSE then stargazer will instead print the contents of the data frame.
Code
# Use only a few rowsboth2 <- both %>%slice(1:6)stargazer(both2, type ="html", summary =FALSE, rownames =FALSE)
year
month
day
delay
temp
wind
precip
quarter
hot
2,013
1
1
17.484
36.819
13.234
0
1
FALSE
2,013
1
2
25.323
28.700
10.884
0
1
FALSE
2,013
1
3
8.450
29.578
8.583
0
1
FALSE
2,013
1
4
12.104
34.333
14.001
0
1
FALSE
2,013
1
5
5.696
36.560
9.398
0
1
FALSE
2,013
1
6
12.383
39.920
9.110
0
1
FALSE
3.1 Remove row and column names
Code
stargazer(both2, type ="html", summary =FALSE,rownames =FALSE,colnames =FALSE)
2,013
1
1
17.484
36.819
13.234
0
1
FALSE
2,013
1
2
25.323
28.700
10.884
0
1
FALSE
2,013
1
3
8.450
29.578
8.583
0
1
FALSE
2,013
1
4
12.104
34.333
14.001
0
1
FALSE
2,013
1
5
5.696
36.560
9.398
0
1
FALSE
2,013
1
6
12.383
39.920
9.110
0
1
FALSE
3.2 Change which statistics are displayed
In order to customize which summary statistics are displayed, change any of the the following (logical) parameters, nobs, mean.sd, min.max, median, and iqr.
Code
stargazer(both, type ="html", nobs =FALSE, mean.sd =TRUE, median =TRUE,iqr =TRUE)
Statistic
Mean
St. Dev.
Min
Pctl(25)
Median
Pctl(75)
Max
year
2,013.000
0.000
2,013
2,013
2,013
2,013
2,013
month
6.511
3.445
1
4
7
9.2
12
day
15.679
8.784
1
8
16
23
31
delay
15.080
13.883
-1.349
5.446
10.501
20.007
97.771
temp
55.531
17.598
15.455
40.114
56.356
71.647
91.085
wind
9.464
4.398
2.637
6.797
8.895
11.508
56.388
precip
0.121
0.335
0.000
0.000
0.000
0.030
3.710
hot
0.022
0.147
0
0
0
0
1
3.3 Change which statistics are displayed (a second way)
Code
stargazer(both, type ="html", summary.stat =c("n", "p75", "sd"))
Statistic
N
Pctl(75)
St. Dev.
year
364
2,013
0.000
month
364
9.2
3.445
day
364
23
8.784
delay
364
20.007
13.883
temp
364
71.647
17.598
wind
364
11.508
4.398
precip
364
0.030
0.335
hot
364
0
0.147
3.4 Remove logical variables in the summary statistics
stargazer reports summary statistics for logical variables by default (0 = FALSE and 1 = TRUE). To supress the reporting of logical vectors change summary.logical to FALSE. Note the stats for our vector hot are gone.
Code
stargazer(both, type ="html", summary.logical =FALSE)
stargazer(output, output2, type ="html", title ="These are awesome results!",covariate.labels =c("Temperature", "Wind speed", "Rain (inches)","2nd quarter", "3rd quarter", "Fourth quarter"),dep.var.caption ="A better caption",dep.var.labels ="Flight delay (in minutes)")
These are awesome results!
A better caption
Flight delay (in minutes)
(1)
(2)
Temperature
0.079**
0.164**
(0.039)
(0.066)
Wind speed
0.285*
0.237
(0.157)
(0.157)
Rain (inches)
15.255***
14.845***
(2.013)
(2.004)
2nd quarter
-2.060
(2.579)
3rd quarter
-6.755**
(3.121)
Fourth quarter
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
6.2 Exclude the dependent variable label or the model numbers
Note the dependent variable caption stays. To additionally remove the caption add dep.var.caption = "".
Code
stargazer(output, output2, type ="html", dep.var.labels.include =FALSE,model.numbers =FALSE)
Dependent variable:
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
6.3 Change the column names
To change the column names just supply a character vector with the new labels, as shown below.
Code
stargazer(output, output2, type ="html", column.labels =c("Good", "Better"))
Dependent variable:
delay
Good
Better
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
6.4 Apply a label to more than one column
The option column.separate allows for assigning a label to more than one column. In this example I told stargazer to report each regression twice, for a total of four columns. Using column.separate, stargazer now applies the first label to the first two columns and the second label to the next two columns.
When the results from different types of regression models (e.g., “OLS”, “probit”) are displayed in the same table stargazer adds a row indicating model type. Remove these labels by including model.names = FALSE (not shown).
Code
stargazer(output, output2, output3, type ="html")
Dependent variable:
delay
OLS
instrumental
variable
(1)
(2)
(3)
temp
0.079**
0.164**
0.041
(0.039)
(0.066)
(0.135)
wind
0.285*
0.237
0.250
(0.157)
(0.157)
(0.198)
precip
15.255***
14.845***
15.335***
(2.013)
(2.004)
(2.034)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
8.577
(2.989)
(3.412)
(8.797)
Observations
364
364
364
R2
0.154
0.175
0.152
Adjusted R2
0.147
0.161
0.145
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
12.839 (df = 360)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
6.6 Model names (again)
The example above shows the default behavior of stargazer is to display only one model name (and dependent variable caption) for adjacent columns with the same model type. To repeat these labels for all of the columns, do the following:
Code
stargazer(output, output2, output3, type ="html",multicolumn =FALSE)
Dependent variable:
delay
delay
delay
OLS
OLS
instrumental
variable
(1)
(2)
(3)
temp
0.079**
0.164**
0.041
(0.039)
(0.066)
(0.135)
wind
0.285*
0.237
0.250
(0.157)
(0.157)
(0.198)
precip
15.255***
14.845***
15.335***
(2.013)
(2.004)
(2.034)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
8.577
(2.989)
(3.412)
(8.797)
Observations
364
364
364
R2
0.154
0.175
0.152
Adjusted R2
0.147
0.161
0.145
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
12.839 (df = 360)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
6.7 Add a custom row to the reported statistics
I use this example to show how to add a row(s), such as reporting fixed effects.
Code
stargazer(output, output2, type ="html",add.lines =list(c("Fixed effects?", "No", "No"),c("Results believable?", "Maybe", "Try again later")))
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Fixed effects?
No
No
Results believable?
Maybe
Try again later
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
6.8 Include R object names
Code
stargazer(output, output2, type ="html", object.names =TRUE)
7.1 Report t-statistics or p-values instead of standard errors
Standard errors are reported by default. To report the t-statistics or p-values instead, see the report argument. Notice that I’ve used this option to move the star characters to the t-statistics instead of being next to the coefficients.
Code
stargazer(output, output2, type ="html",report ="vct*")
Dependent variable:
delay
(1)
(2)
temp
0.079
0.164
t = 2.010**
t = 2.479**
wind
0.285
0.237
t = 1.812*
t = 1.507
precip
15.255
14.845
t = 7.579***
t = 7.407***
quarter2
-2.060
t = -0.799
quarter3
-6.755
t = -2.164**
quarter4
-4.408
t = -2.164**
Constant
6.152
5.246
t = 2.058**
t = 1.537
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
7.2 Report confidence intervals
Code
stargazer(output, output2, type ="html",ci =TRUE)
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.002, 0.156)
(0.034, 0.294)
wind
0.285*
0.237
(-0.023, 0.594)
(-0.071, 0.545)
precip
15.255***
14.845***
(11.310, 19.200)
(10.917, 18.773)
quarter2
-2.060
(-7.116, 2.995)
quarter3
-6.755**
(-12.872, -0.638)
quarter4
-4.408**
(-8.401, -0.416)
Constant
6.152**
5.246
(0.293, 12.010)
(-1.442, 11.934)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
7.3 Adjust the confidence intervals
By default ci.level = 0.95. You may also change the character that separates the intervals with the ci.separator argument.
7.4 Robust standard errors (replicating Stata’s robust option)
If you want to use robust standard errors (or clustered), stargazer allows for replacing the default output by supplying a new vector of values to the option se. For this example I will display the same model twice and adjust the standard errors in the second column with the HC1 correction from the sandwich package (i.e. the same correction Stata uses).
I also need to adjust the F statistic with the corrected variance-covariance matrix (matching Stata’s results). Currently, this must be done manually (via add.lines) as stargazer does not (yet) have an option for directly replacing the F statistic.
Similar options exist to supply adjusted values to the coefficients, t-statistics, confidence intervals, and p-values. See coef, t, ci.custom, or p.
Code
library(sandwich)library(lmtest) # waldtest; see also coeftest.# Adjust standard errorscov1 <-vcovHC(output, type ="HC1")robust_se <-sqrt(diag(cov1))# Adjust F statistic wald_results <-waldtest(output, vcov = cov1)stargazer(output, output, type ="html",se =list(NULL, robust_se),omit.stat ="f",add.lines =list(c("F Statistic (df = 3; 360)", "12.879***", "7.73***")))
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.079*
(0.039)
(0.041)
wind
0.285*
0.285
(0.157)
(0.177)
precip
15.255***
15.255***
(2.013)
(4.827)
Constant
6.152**
6.152**
(2.989)
(3.074)
F Statistic (df = 3; 360)
12.879***
7.73***
Observations
364
364
R2
0.154
0.154
Adjusted R2
0.147
0.147
Residual Std. Error (df = 360)
12.822
12.822
Note:
p<0.1; p<0.05; p<0.01
7.5 Move the intercept term to the top of the table
Code
stargazer(output, output2, type ="html",intercept.bottom =FALSE)
Dependent variable:
delay
(1)
(2)
Constant
6.152**
5.246
(2.989)
(3.412)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
7.6 Compress the table output
We can condense the table output by placing all the the output on the same row. When single.row is set to TRUE, the argument no.space is automatically set to TRUE.
Code
stargazer(output, output2, type ="html",single.row =TRUE)
In fixed effect model specifications it is often undesirable to report the fixed effect coefficients. To omit any coefficient, supply a regular expression to omit.
Code
stargazer(output, output2, type ="html", omit ="quarter")
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
8.1 Reporting omitted variables
Add the omit.labels parameter to report which variables have been omitted. Must be the same length as the number of regular expressions supplied to omit. By default omit.labels reports “Yes” or “No”. To change this supply a new vector of length 2 to omit.yes.no = c("Yes", "No").
Code
stargazer(output, output2, type ="html", omit ="quarter",omit.labels ="Quarter dummies?")
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
Constant
6.152**
5.246
(2.989)
(3.412)
Quarter dummies?
No
No
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
8.2 Omit summary statistics
Code
## Remove r-square and f-statisticstargazer(output, output2, type ="html", omit.stat =c("rsq", "f"))
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
Note:
p<0.1; p<0.05; p<0.01
See also keep.stat a related argument with the opposite behavior.
8.3 Omit whole parts of the table
If you just want to remove parts of the table it is easier to use omit.table.layout to explicitly specify table elements. See table layout chracters for a list of codes.
Code
# Remove statistics and notes sections completelystargazer(output, output2, type ="html", omit.table.layout ="sn")
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
8.4 Omit whole parts of the table (a second way)
Another way to achieve the result above is through the argument table.layout. It also accepts a character string that tells stargazer which table elements to include.
Code
# Include everything except the statistics and notes sectionsstargazer(output, output2, type ="html", table.layout ="-ld#-t-")
Dependent variable:
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
8.5 Omit the degrees of freedom
Code
stargazer(output, output2, type ="html", df =FALSE)
By default stargazer uses ***, **, and * to denote statistical significance at the one, five, and ten percent levels. This behavior can be changed by altering the star.char option.
Code
stargazer(output, output2, type ="html", star.char =c("@", "@@", "@@@"))
Dependent variable:
delay
(1)
(2)
temp
0.079@@
0.164@@
(0.039)
(0.066)
wind
0.285@
0.237
(0.157)
(0.157)
precip
15.255@@@
14.845@@@
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755@@
(3.121)
quarter4
-4.408@@
(2.037)
Constant
6.152@@
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860@@@ (df = 3; 360)
12.593@@@ (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
9.1 Change the cutoffs for significance
Notice that temperature, quarter3, and quarter4 have each lost a gold star because we made it tougher to earn them.
Code
stargazer(output, output2, type ="html", star.cutoffs =c(0.05, 0.01, 0.001)) # star.cutoffs = NULL by default
# For LaTeX output you can also wrap table text in commands like \textbf{...}, # just remember to escape the first backslash, e.g., "A \\textbf{better} caption"stargazer(output, output2, type ="html", title ="These are <em> awesome </em> results!", # Italicsdep.var.caption ="A <b> better </b> caption") # Bold
These are awesome results!
A better caption
delay
(1)
(2)
temp
0.079**
0.164**
(0.039)
(0.066)
wind
0.285*
0.237
(0.157)
(0.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
11.2 Change decimal character
Code
stargazer(output, output2, type ="html", decimal.mark =",")
Dependent variable:
delay
(1)
(2)
temp
0,079**
0,164**
(0,039)
(0,066)
wind
0,285*
0,237
(0,157)
(0,157)
precip
15,255***
14,845***
(2,013)
(2,004)
quarter2
-2,060
(2,579)
quarter3
-6,755**
(3,121)
quarter4
-4,408**
(2,037)
Constant
6,152**
5,246
(2,989)
(3,412)
Observations
364
364
R2
0,154
0,175
Adjusted R2
0,147
0,161
Residual Std. Error
12,822 (df = 360)
12,718 (df = 357)
F Statistic
21,860*** (df = 3; 360)
12,593*** (df = 6; 357)
Note:
p<0,1; p<0,05; p<0,01
11.3 Control the number of decimal places
Code
stargazer(output, output2, type ="html", digits =1)
Dependent variable:
delay
(1)
(2)
temp
0.1**
0.2**
(0.04)
(0.1)
wind
0.3*
0.2
(0.2)
(0.2)
precip
15.3***
14.8***
(2.0)
(2.0)
quarter2
-2.1
(2.6)
quarter3
-6.8**
(3.1)
quarter4
-4.4**
(2.0)
Constant
6.2**
5.2
(3.0)
(3.4)
Observations
364
364
R2
0.2
0.2
Adjusted R2
0.1
0.2
Residual Std. Error
12.8 (df = 360)
12.7 (df = 357)
F Statistic
21.9*** (df = 3; 360)
12.6*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
11.4 Additional decimal controls
You may also specify the number of additional decimal places to be used if a number, when rounded to digits decimal places, is equal to zero (Use argument digits.extra).
My example models do not have any numbers in the thousands, so I won’t show them, but digit.separate and digits.separator are also available for customizing the output of those characters.
Code
stargazer(output, output2, type ="html", digits =1,digits.extra =1)
Dependent variable:
delay
(1)
(2)
temp
0.1**
0.2**
(0.04)
(0.1)
wind
0.3*
0.2
(0.2)
(0.2)
precip
15.3***
14.8***
(2.0)
(2.0)
quarter2
-2.1
(2.6)
quarter3
-6.8**
(3.1)
quarter4
-4.4**
(2.0)
Constant
6.2**
5.2
(3.0)
(3.4)
Observations
364
364
R2
0.2
0.2
Adjusted R2
0.1
0.2
Residual Std. Error
12.8 (df = 360)
12.7 (df = 357)
F Statistic
21.9*** (df = 3; 360)
12.6*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
11.5 Drop leading zeros from decimals
Code
stargazer(output, output2, type ="html", initial.zero =FALSE)
Dependent variable:
delay
(1)
(2)
temp
.079**
.164**
(.039)
(.066)
wind
.285*
.237
(.157)
(.157)
precip
15.255***
14.845***
(2.013)
(2.004)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
.154
.175
Adjusted R2
.147
.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
11.6 Change the order of the variables
The order argument will also accept a vector of regular expressions.
Code
stargazer(output, output2, type ="html", order =c(4, 5, 6, 3, 2, 1))
Dependent variable:
delay
(1)
(2)
quarter2
-2.060
(2.579)
quarter3
-6.755**
(3.121)
quarter4
-4.408**
(2.037)
precip
15.255***
14.845***
(2.013)
(2.004)
wind
0.285*
0.237
(0.157)
(0.157)
temp
0.079**
0.164**
(0.039)
(0.066)
Constant
6.152**
5.246
(2.989)
(3.412)
Observations
364
364
R2
0.154
0.175
Adjusted R2
0.147
0.161
Residual Std. Error
12.822 (df = 360)
12.718 (df = 357)
F Statistic
21.860*** (df = 3; 360)
12.593*** (df = 6; 357)
Note:
p<0.1; p<0.05; p<0.01
11.7 Select which variables to keep in the table
By default keep = NULL meaning all variables are included. keep accepts a vector of regular expressions.
Code
# Regex for keep "precip" but not "precipitation"stargazer(output, output2, type ="html", keep =c("\\bprecip\\b"))
---title: "A Stargazer Cheatsheet"date: "Updated `r format(Sys.time(), '%d %B, %Y')`"format: aps-html: code-tools: truecategories: [table, stargazer, package]---## Dataset: dplyr and nycflights13Setting up a dataset for this cheatsheet allows me to spotlight two recent Rpackages created by [Hadley Wickham](http://had.co.nz/). The first,[dplyr](https://github.com/hadley/dplyr), is a set of new tools for datamanipulation. Using `dplyr`, I will extract flights and weather data from anothernew package called [nycflights13](https://github.com/hadley/nycflights13). Withthis data I will show how to estimate a couple of regression models and nicelyformat the results into tables with `stargazer`.Note: `stargazer` v. 5.1 does not play nicely with `dplyr`'s tbl_df class. As atemporary work-around I pipe the merged dataset to `data.frame`.```{r, echo = TRUE, warning = FALSE, message = FALSE}library("dplyr")library("nycflights13")library("AER") # Applied Econometrics with Rlibrary("stargazer")daily <- flights %>% filter(origin == "EWR") %>% group_by(year, month, day) %>% summarise(delay = mean(dep_delay, na.rm = TRUE))daily_weather <- weather %>% filter(origin == "EWR") %>% group_by(year, month, day) %>% summarise(temp = mean(temp, na.rm = TRUE), wind = mean(wind_speed, na.rm = TRUE), precip = sum(precip, na.rm = TRUE))# Merge flights with weather data framesboth <- inner_join(daily, daily_weather) %>% data.frame() # Temporary fix# Create an indicator for quarterboth$quarter <- cut(both$month, breaks = c(0, 3, 6, 9, 12), labels = c("1", "2", "3", "4"))# Create a vector of class logicalboth$hot <- as.logical(both$temp > 85)head(both)```\We can use the `both` data frame to estimate a couple of linear models predictingthe average delay out of Newark controlling for the weather. The first model willuse only the weather variables and in the second I'll add dummy variablesindicating the quarter. I also estimate a third model, using using the `ivreg`command from package [AER](http://cran.r-project.org/web/packages/AER/index.html)to demonstrate output with mixed models. The raw R output:```{r, echo = TRUE}output <- lm(delay ~ temp + wind + precip, data = both)output2 <- lm(delay ~ temp + wind + precip + quarter, data = both)# Instrumental variables model output3 <- ivreg(delay ~ temp + wind + precip | . - temp + hot, data = both)summary(output)summary(output2)summary(output3)```\[Back to table of contents](#TOC)## Quick notesSince I'm using [knitr](http://cran.rstudio.com/web/packages/knitr/index.html)and [R markdown](http://rmarkdown.rstudio.com/) to create this webpage, in thecode that follows I will include the `stargazer` option `type = "html"`.`stargazer` is set to produce **LaTeX** output by default. If you desire**LaTeX** output, just remove the type option from the code below.\Also, while I have added an example for many of the available `stargazer`options, I have not included all of them. So while you're likely to find arelevant example don't assume if it's not listed that `stargazer` can't do it.Check the documentation for additional features and updates to the package. It isoften the case that an omitted argument is specific for **LaTeX** output and Ican't demonstrate it here.### HTML formattingIt is possible to change the formatting of html tables generated with `stargazer`via an html style sheet. See the [R Markdowndocumentation](http://rmarkdown.rstudio.com/html_document_format.html#custom_css)about incorporating a custom CSS.\[Back to table of contents](#TOC)## The default summary statistics table```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(both, type = "html")```\When supplied a data frame, by default `stargazer` creates a table with summarystatistics. If the `summary` option is set to `FALSE` then stargazer will insteadprint the contents of the data frame.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}# Use only a few rowsboth2 <- both %>% slice(1:6)stargazer(both2, type = "html", summary = FALSE, rownames = FALSE)```### Remove row and column names```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(both2, type = "html", summary = FALSE, rownames = FALSE, colnames = FALSE)```### Change which statistics are displayedIn order to customize which summary statistics are displayed, change any of thethe following (logical) parameters, `nobs`, `mean.sd`, `min.max`, `median`, and`iqr`.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(both, type = "html", nobs = FALSE, mean.sd = TRUE, median = TRUE, iqr = TRUE)```### Change which statistics are displayed (a second way)```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(both, type = "html", summary.stat = c("n", "p75", "sd"))```### Remove logical variables in the summary statistics`stargazer` reports summary statistics for logical variables by default (0 =FALSE and 1 = TRUE). To supress the reporting of logical vectors change`summary.logical` to `FALSE`. Note the stats for our vector `hot` are gone.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(both, type = "html", summary.logical = FALSE)```### Flip the table axes```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(both, type = "html", flip = TRUE)```\[Back to table of contents](#TOC)## The default regression table```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html")```\[Back to table of contents](#TOC)## Change the style`stargazer` includes several pre-formatted styles that imitate popular academicjournals. Use the `style` argument.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", style = "qje")```\[Back to table of contents](#TOC)## Labelling the table### Add a title; change the variable labels```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", title = "These are awesome results!", covariate.labels = c("Temperature", "Wind speed", "Rain (inches)", "2nd quarter", "3rd quarter", "Fourth quarter"), dep.var.caption = "A better caption", dep.var.labels = "Flight delay (in minutes)")```### Exclude the dependent variable label or the model numbersNote the dependent variable caption stays. To additionally remove the caption add`dep.var.caption = ""`.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", dep.var.labels.include = FALSE, model.numbers = FALSE)```### Change the column namesTo change the column names just supply a character vector with the new labels, asshown below.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", column.labels = c("Good", "Better"))```### Apply a label to more than one columnThe option `column.separate` allows for assigning a label to more than onecolumn. In this example I told `stargazer` to report each regression twice, for atotal of four columns. Using `column.separate`, `stargazer` now applies the firstlabel to the first two columns and the second label to the next two columns.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output, output2, output2, type = "html", column.labels = c("Good", "Better"), column.separate = c(2, 2))```### Model namesWhen the results from different types of regression models (e.g., "OLS","probit") are displayed in the same table `stargazer` adds a row indicating modeltype. Remove these labels by including `model.names = FALSE` (not shown).```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, output3, type = "html")```### Model names (again)The example above shows the default behavior of `stargazer` is to display onlyone model name (and dependent variable caption) for adjacent columns with thesame model type. To repeat these labels for all of the columns, do the following:```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, output3, type = "html", multicolumn = FALSE)```### Add a custom row to the reported statisticsI use this example to show how to add a row(s), such as reporting fixed effects.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", add.lines = list(c("Fixed effects?", "No", "No"), c("Results believable?", "Maybe", "Try again later")))```### Include R object names```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", object.names = TRUE)```\[Back to table of contents](#TOC)## Change the default output### Report t-statistics or p-values instead of standard errorsStandard errors are reported by default. To report the t-statistics or p-valuesinstead, see the `report` argument. Notice that I've used this option to move thestar characters to the t-statistics instead of being next to the coefficients.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", report = "vct*")```### Report confidence intervals```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", ci = TRUE)```### Adjust the confidence intervalsBy default `ci.level = 0.95`. You may also change the character that separatesthe intervals with the `ci.separator` argument.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", ci = TRUE, ci.level = 0.90, ci.separator = " @@ ")```### Robust standard errors (replicating Stata's robust option)If you want to use robust standard errors (or clustered), `stargazer` allows forreplacing the default output by supplying a new vector of values to the option`se`. For this example I will display the same model twice and adjust thestandard errors in the second column with the `HC1` correction from the`sandwich` package (i.e. the same correction Stata uses).I also need to adjust the F statistic with the corrected variance-covariancematrix (matching Stata's results). Currently, this must be done manually (via`add.lines`) as `stargazer` does not (yet) have an option for directly replacingthe F statistic.Similar options exist to supply adjusted values to the coefficients,t-statistics, confidence intervals, and p-values. See `coef`, `t`, `ci.custom`,or `p`.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}library(sandwich)library(lmtest) # waldtest; see also coeftest.# Adjust standard errorscov1 <- vcovHC(output, type = "HC1")robust_se <- sqrt(diag(cov1))# Adjust F statistic wald_results <- waldtest(output, vcov = cov1)stargazer(output, output, type = "html", se = list(NULL, robust_se), omit.stat = "f", add.lines = list(c("F Statistic (df = 3; 360)", "12.879***", "7.73***")))```### Move the intercept term to the top of the table```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", intercept.bottom = FALSE)```### Compress the table outputWe can condense the table output by placing all the the output on the same row.When `single.row` is set to `TRUE`, the argument `no.space` is automatically setto `TRUE`.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", single.row = TRUE)```\[Back to table of contents](#TOC)## Omit parts of the default outputIn fixed effect model specifications it is often undesirable to report the fixedeffect coefficients. To omit any coefficient, supply a regular expression to`omit`.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", omit = "quarter")```### Reporting omitted variablesAdd the `omit.labels` parameter to report which variables have been omitted. Mustbe the same length as the number of regular expressions supplied to `omit`. Bydefault `omit.labels` reports "Yes" or "No". To change this supply a new vectorof length 2 to `omit.yes.no = c("Yes", "No")`.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", omit = "quarter", omit.labels = "Quarter dummies?")```### Omit summary statistics```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}## Remove r-square and f-statisticstargazer(output, output2, type = "html", omit.stat = c("rsq", "f"))```\See also `keep.stat` a related argument with the opposite behavior.### Omit whole parts of the tableIf you just want to remove parts of the table it is easier to use`omit.table.layout` to explicitly specify table elements. See`table layout chracters` for a list of codes.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}# Remove statistics and notes sections completelystargazer(output, output2, type = "html", omit.table.layout = "sn")```### Omit whole parts of the table (a second way)Another way to achieve the result above is through the argument `table.layout`.It also accepts a character string that tells `stargazer` which table elements to**include**.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}# Include everything except the statistics and notes sectionsstargazer(output, output2, type = "html", table.layout = "-ld#-t-")```### Omit the degrees of freedom```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", df = FALSE)```\[Back to table of contents](#TOC)## Statistical significance optionsBy default `stargazer` uses \*\*\*, \*\*, and \* to denote statisticalsignificance at the one, five, and ten percent levels. This behavior can bechanged by altering the `star.char` option.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", star.char = c("@", "@@", "@@@"))```### Change the cutoffs for significanceNotice that temperature, quarter3, and quarter4 have each lost a gold starbecause we made it tougher to earn them.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", star.cutoffs = c(0.05, 0.01, 0.001)) # star.cutoffs = NULL by default```\[Back to table of contents](#TOC)## Modifying table notes### Make an addition to the existing note section```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", notes = "I make this look good!")```### Replace the note section```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", notes = "Sometimes you just have to start over.", notes.append = FALSE)```### Change note alignment```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", notes.align = "l")```### Change the note section label```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", notes.label = "New note label")```\[Back to table of contents](#TOC)## Table aesthetics### Use html tags to modify table elements```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}# For LaTeX output you can also wrap table text in commands like \textbf{...}, # just remember to escape the first backslash, e.g., "A \\textbf{better} caption"stargazer(output, output2, type = "html", title = "These are <em> awesome </em> results!", # Italics dep.var.caption = "A <b> better </b> caption") # Bold```### Change decimal character```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", decimal.mark = ",")```### Control the number of decimal places```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", digits = 1)```### Additional decimal controlsYou may also specify the number of additional decimal places to be used if anumber, when rounded to `digits` decimal places, is equal to zero (Use argument`digits.extra`).My example models do not have any numbers in the thousands, so I won't show them,but `digit.separate` and `digits.separator` are also available for customizingthe output of those characters.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", digits = 1, digits.extra = 1)```### Drop leading zeros from decimals```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", initial.zero = FALSE)```### Change the order of the variablesThe `order` argument will also accept a vector of regular expressions.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}stargazer(output, output2, type = "html", order = c(4, 5, 6, 3, 2, 1))```### Select which variables to keep in the tableBy default `keep = NULL` meaning all variables are included. `keep` accepts avector of regular expressions.```{r, echo = TRUE, warning = FALSE, message = FALSE, results='asis'}# Regex for keep "precip" but not "precipitation"stargazer(output, output2, type = "html", keep = c("\\bprecip\\b"))```\[Back to table of contents](#TOC)## FeedbackThe .Rmd file for this cheatsheet [is onGitHub](https://github.com/JakeRuss/cheatsheets) and I welcome suggestions orpull requests.\[Back to table of contents](#TOC)