Dataset: dplyr and nycflights13

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.

library("dplyr")
library("nycflights13")
library("AER") # Applied Econometrics with R
library("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 frames
both <- inner_join(daily, daily_weather) %>% 
  data.frame()  # Temporary fix

# Create an indicator for quarter
both$quarter <- cut(both$month, breaks = c(0, 3, 6, 9, 12), 
                                labels = c("1", "2", "3", "4"))

# Create a vector of class logical
both$hot <- as.logical(both$temp > 85)

head(both)
##   year month day     delay    temp      wind precip quarter   hot
## 1 2013     1   1 17.483553 38.4800 12.758648      0       1 FALSE
## 2 2013     1   2 25.322674 28.8350 12.514732      0       1 FALSE
## 3 2013     1   3  8.450450 29.4575  7.863663      0       1 FALSE
## 4 2013     1   4 12.103858 33.4775 13.857309      0       1 FALSE
## 5 2013     1   5  5.696203 36.7325 10.836512      0       1 FALSE
## 6 2013     1   6 12.383333 37.9700  8.007511      0       1 FALSE


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:

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)
## 
## Call:
## lm(formula = delay ~ temp + wind + precip, data = both)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -26.201  -8.497  -3.533   4.708  75.727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  7.26298    3.09879   2.344   0.0196 *  
## temp         0.08808    0.04068   2.165   0.0310 *  
## wind         0.16648    0.16392   1.016   0.3105    
## precip      18.91805    3.24948   5.822 1.29e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.25 on 360 degrees of freedom
## Multiple R-squared:  0.09693,    Adjusted R-squared:  0.0894 
## F-statistic: 12.88 on 3 and 360 DF,  p-value: 5.19e-08
summary(output2)
## 
## Call:
## lm(formula = delay ~ temp + wind + precip + quarter, data = both)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -26.927  -8.740  -3.937   5.181  74.631 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.14163    3.53599   1.737  0.08327 .  
## temp         0.18396    0.06856   2.683  0.00763 ** 
## wind         0.11445    0.16357   0.700  0.48459    
## precip      18.16739    3.22973   5.625 3.75e-08 ***
## quarter2    -2.26471    2.66038  -0.851  0.39519    
## quarter3    -7.52596    3.22585  -2.333  0.02020 *  
## quarter4    -4.75737    2.10380  -2.261  0.02434 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.12 on 357 degrees of freedom
## Multiple R-squared:  0.1218, Adjusted R-squared:  0.107 
## F-statistic: 8.253 on 6 and 357 DF,  p-value: 2.221e-08
summary(output3)
## 
## Call:
## ivreg(formula = delay ~ temp + wind + precip | . - temp + hot, 
##     data = both)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -25.739  -8.929  -3.834   5.015  74.767 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  10.7297     9.1384   1.174    0.241    
## temp          0.0342     0.1397   0.245    0.807    
## wind          0.1157     0.2070   0.559    0.577    
## precip       18.8775     3.2589   5.793 1.51e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.28 on 360 degrees of freedom
## Multiple R-Squared: 0.09253, Adjusted R-squared: 0.08496 
## Wald test: 11.28 on 3 and 360 DF,  p-value: 4.326e-07


Back to table of contents

Quick notes

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.

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.


Back to table of contents

The default summary statistics table

stargazer(both, type = "html")
Statistic N Mean St. Dev. Min Max
year 364 2,013.000 0.000 2,013 2,013
month 364 6.511 3.445 1 12
day 364 15.679 8.784 1 31
delay 364 15.080 13.883 -1.349 97.771
temp 364 55.481 17.581 15.492 91.168
wind 364 9.339 4.363 2.014 55.669
precip 364 0.073 0.214 0.000 1.890
hot 364 0.022 0.147 0 1


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.

# Use only a few rows
both2 <- 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 38.480 12.759 0 1 FALSE
2,013 1 2 25.323 28.835 12.515 0 1 FALSE
2,013 1 3 8.450 29.457 7.864 0 1 FALSE
2,013 1 4 12.104 33.477 13.857 0 1 FALSE
2,013 1 5 5.696 36.733 10.837 0 1 FALSE
2,013 1 6 12.383 37.970 8.008 0 1 FALSE

Remove row and column names

stargazer(both2, type = "html", summary = FALSE,
          rownames = FALSE,
          colnames = FALSE)
2,013 1 1 17.484 38.480 12.759 0 1 FALSE
2,013 1 2 25.323 28.835 12.515 0 1 FALSE
2,013 1 3 8.450 29.457 7.864 0 1 FALSE
2,013 1 4 12.104 33.477 13.857 0 1 FALSE
2,013 1 5 5.696 36.733 10.837 0 1 FALSE
2,013 1 6 12.383 37.970 8.008 0 1 FALSE

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.

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.481 17.581 15.492 39.873 56.960 71.570 91.168
wind 9.339 4.363 2.014 6.557 8.847 11.556 55.669
precip 0.073 0.214 0.000 0.000 0.000 0.020 1.890
hot 0.022 0.147 0 0 0 0 1

Change which statistics are displayed (a second way)

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.570 17.581
wind 364 11.556 4.363
precip 364 0.020 0.214
hot 364 0 0.147

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.

stargazer(both, type = "html", summary.logical = FALSE)
Statistic N Mean St. Dev. Min Max
year 364 2,013.000 0.000 2,013 2,013
month 364 6.511 3.445 1 12
day 364 15.679 8.784 1 31
delay 364 15.080 13.883 -1.349 97.771
temp 364 55.481 17.581 15.492 91.168
wind 364 9.339 4.363 2.014 55.669
precip 364 0.073 0.214 0.000 1.890

Flip the table axes

stargazer(both, type = "html", flip = TRUE)
Statistic year month day delay temp wind precip hot
N 364 364 364 364 364 364 364 364
Mean 2,013.000 6.511 15.679 15.080 55.481 9.339 0.073 0.022
St. Dev. 0.000 3.445 8.784 13.883 17.581 4.363 0.214 0.147
Min 2,013 1 1 -1.349 15.492 2.014 0.000 0
Max 2,013 12 31 97.771 91.168 55.669 1.890 1


Back to table of contents

The default regression table

stargazer(output, output2, type = "html")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01


Back to table of contents

Change the style

stargazer includes several pre-formatted styles that imitate popular academic journals. Use the style argument.

stargazer(output, output2, type = "html", style = "qje")
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
N 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Notes: ***Significant at the 1 percent level.
**Significant at the 5 percent level.
*Significant at the 10 percent level.


Back to table of contents

Labelling the table

Add a title; change the variable labels

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.088** 0.184***
(0.041) (0.069)
Wind speed 0.166 0.114
(0.164) (0.164)
Rain (inches) 18.918*** 18.167***
(3.249) (3.230)
2nd quarter -2.265
(2.660)
3rd quarter -7.526**
(3.226)
Fourth quarter -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Exclude the dependent variable label or the model numbers

Note the dependent variable caption stays. To additionally remove the caption add dep.var.caption = "".

stargazer(output, output2, type = "html", 
          dep.var.labels.include = FALSE,
          model.numbers          = FALSE)
Dependent variable:
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Change the column names

To change the column names just supply a character vector with the new labels, as shown below.

stargazer(output, output2, type = "html", column.labels = c("Good", "Better"))
Dependent variable:
delay
Good Better
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

stargazer(output, output, output2, output2, type = "html", 
          column.labels   = c("Good", "Better"),
          column.separate = c(2, 2))
Dependent variable:
delay
Good Better
(1) (2) (3) (4)
temp 0.088** 0.088** 0.184*** 0.184***
(0.041) (0.041) (0.069) (0.069)
wind 0.166 0.166 0.114 0.114
(0.164) (0.164) (0.164) (0.164)
precip 18.918*** 18.918*** 18.167*** 18.167***
(3.249) (3.249) (3.230) (3.230)
quarter2 -2.265 -2.265
(2.660) (2.660)
quarter3 -7.526** -7.526**
(3.226) (3.226)
quarter4 -4.757** -4.757**
(2.104) (2.104)
Constant 7.263** 7.263** 6.142* 6.142*
(3.099) (3.099) (3.536) (3.536)
Observations 364 364 364 364
R2 0.097 0.097 0.122 0.122
Adjusted R2 0.089 0.089 0.107 0.107
Residual Std. Error 13.248 (df = 360) 13.248 (df = 360) 13.119 (df = 357) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 12.879*** (df = 3; 360) 8.253*** (df = 6; 357) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Model names

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

stargazer(output, output2, output3, type = "html")
Dependent variable:
delay
OLS instrumental
variable
(1) (2) (3)
temp 0.088** 0.184*** 0.034
(0.041) (0.069) (0.140)
wind 0.166 0.114 0.116
(0.164) (0.164) (0.207)
precip 18.918*** 18.167*** 18.877***
(3.249) (3.230) (3.259)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142* 10.730
(3.099) (3.536) (9.138)
Observations 364 364 364
R2 0.097 0.122 0.093
Adjusted R2 0.089 0.107 0.085
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357) 13.280 (df = 360)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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:

stargazer(output, output2, output3, type = "html",
          multicolumn = FALSE)
Dependent variable:
delay delay delay
OLS OLS instrumental
variable
(1) (2) (3)
temp 0.088** 0.184*** 0.034
(0.041) (0.069) (0.140)
wind 0.166 0.114 0.116
(0.164) (0.164) (0.207)
precip 18.918*** 18.167*** 18.877***
(3.249) (3.230) (3.259)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142* 10.730
(3.099) (3.536) (9.138)
Observations 364 364 364
R2 0.097 0.122 0.093
Adjusted R2 0.089 0.107 0.085
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357) 13.280 (df = 360)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

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.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Fixed effects? No No
Results believable? Maybe Try again later
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Include R object names

stargazer(output, output2, type = "html", 
          object.names = TRUE)
Dependent variable:
delay
(1) (2)
output output2
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01


Back to table of contents

Change the default output

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.

stargazer(output, output2, type = "html",
          report = "vct*")
Dependent variable:
delay
(1) (2)
temp 0.088 0.184
t = 2.165** t = 2.683***
wind 0.166 0.114
t = 1.016 t = 0.700
precip 18.918 18.167
t = 5.822*** t = 5.625***
quarter2 -2.265
t = -0.851
quarter3 -7.526
t = -2.333**
quarter4 -4.757
t = -2.261**
Constant 7.263 6.142
t = 2.344** t = 1.737*
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Report confidence intervals

stargazer(output, output2, type = "html",
          ci = TRUE)
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.008, 0.168) (0.050, 0.318)
wind 0.166 0.114
(-0.155, 0.488) (-0.206, 0.435)
precip 18.918*** 18.167***
(12.549, 25.287) (11.837, 24.498)
quarter2 -2.265
(-7.479, 2.950)
quarter3 -7.526**
(-13.849, -1.203)
quarter4 -4.757**
(-8.881, -0.634)
Constant 7.263** 6.142*
(1.189, 13.337) (-0.789, 13.072)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

stargazer(output, output2, type = "html",
          ci = TRUE, ci.level = 0.90, ci.separator = " @@ ")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.021 @@ 0.155) (0.071 @@ 0.297)
wind 0.166 0.114
(-0.103 @@ 0.436) (-0.155 @@ 0.384)
precip 18.918*** 18.167***
(13.573 @@ 24.263) (12.855 @@ 23.480)
quarter2 -2.265
(-6.641 @@ 2.111)
quarter3 -7.526**
(-12.832 @@ -2.220)
quarter4 -4.757**
(-8.218 @@ -1.297)
Constant 7.263** 6.142*
(2.166 @@ 12.360) (0.325 @@ 11.958)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

library(sandwich)
library(lmtest)   # waldtest; see also coeftest.

# Adjust standard errors
cov1         <- 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.088** 0.088**
(0.041) (0.043)
wind 0.166 0.166
(0.164) (0.159)
precip 18.918*** 18.918***
(3.249) (4.735)
Constant 7.263** 7.263**
(3.099) (3.053)
F Statistic (df = 3; 360) 12.879*** 7.73***
Observations 364 364
R2 0.097 0.097
Adjusted R2 0.089 0.089
Residual Std. Error (df = 360) 13.248 13.248
Note: p<0.1; p<0.05; p<0.01

Move the intercept term to the top of the table

stargazer(output, output2, type = "html",
          intercept.bottom = FALSE)
Dependent variable:
delay
(1) (2)
Constant 7.263** 6.142*
(3.099) (3.536)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

stargazer(output, output2, type = "html",
          single.row = TRUE)
Dependent variable:
delay
(1) (2)
temp 0.088** (0.041) 0.184*** (0.069)
wind 0.166 (0.164) 0.114 (0.164)
precip 18.918*** (3.249) 18.167*** (3.230)
quarter2 -2.265 (2.660)
quarter3 -7.526** (3.226)
quarter4 -4.757** (2.104)
Constant 7.263** (3.099) 6.142* (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01


Back to table of contents

Omit parts of the default output

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.

stargazer(output, output2, type = "html", omit = "quarter")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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").

stargazer(output, output2, type = "html", 
          omit        = "quarter",
          omit.labels = "Quarter dummies?")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
Constant 7.263** 6.142*
(3.099) (3.536)
Quarter dummies? No No
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Omit summary statistics

## Remove r-square and f-statistic
stargazer(output, output2, type = "html", 
          omit.stat = c("rsq", "f"))
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
Note: p<0.1; p<0.05; p<0.01


See also keep.stat a related argument with the opposite behavior.

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.

# Remove statistics and notes sections completely
stargazer(output, output2, type = "html", 
          omit.table.layout = "sn")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)

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.

# Include everything except the statistics and notes sections
stargazer(output, output2, type = "html", 
          table.layout = "-ld#-t-")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)

Omit the degrees of freedom

stargazer(output, output2, type = "html", 
          df = FALSE)
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 13.119
F Statistic 12.879*** 8.253***
Note: p<0.1; p<0.05; p<0.01


Back to table of contents

Statistical significance options

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.

stargazer(output, output2, type = "html", 
          star.char = c("@", "@@", "@@@"))
Dependent variable:
delay
(1) (2)
temp 0.088@@ 0.184@@@
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918@@@ 18.167@@@
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526@@
(3.226)
quarter4 -4.757@@
(2.104)
Constant 7.263@@ 6.142@
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879@@@ (df = 3; 360) 8.253@@@ (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

stargazer(output, output2, type = "html", 
          star.cutoffs = c(0.05, 0.01, 0.001)) # star.cutoffs = NULL by default
Dependent variable:
delay
(1) (2)
temp 0.088* 0.184**
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526*
(3.226)
quarter4 -4.757*
(2.104)
Constant 7.263* 6.142
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.05; p<0.01; p<0.001


Back to table of contents

Modifying table notes

Make an addition to the existing note section

stargazer(output, output2, type = "html", 
          notes = "I make this look good!")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01
I make this look good!

Replace the note section

stargazer(output, output2, type = "html", 
          notes        = "Sometimes you just have to start over.", 
          notes.append = FALSE)
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: Sometimes you just have to start over.

Change note alignment

stargazer(output, output2, type = "html", 
          notes.align = "l")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Change the note section label

stargazer(output, output2, type = "html", 
          notes.label = "New note label")
Dependent variable:
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
New note label p<0.1; p<0.05; p<0.01


Back to table of contents

Table aesthetics

Use html tags to modify table elements

# 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
These are awesome results!
A better caption
delay
(1) (2)
temp 0.088** 0.184***
(0.041) (0.069)
wind 0.166 0.114
(0.164) (0.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Change decimal character

stargazer(output, output2, type = "html", 
          decimal.mark = ",")
Dependent variable:
delay
(1) (2)
temp 0,088** 0,184***
(0,041) (0,069)
wind 0,166 0,114
(0,164) (0,164)
precip 18,918*** 18,167***
(3,249) (3,230)
quarter2 -2,265
(2,660)
quarter3 -7,526**
(3,226)
quarter4 -4,757**
(2,104)
Constant 7,263** 6,142*
(3,099) (3,536)
Observations 364 364
R2 0,097 0,122
Adjusted R2 0,089 0,107
Residual Std. Error 13,248 (df = 360) 13,119 (df = 357)
F Statistic 12,879*** (df = 3; 360) 8,253*** (df = 6; 357)
Note: p<0,1; p<0,05; p<0,01

Control the number of decimal places

stargazer(output, output2, type = "html", 
          digits = 1)
Dependent variable:
delay
(1) (2)
temp 0.1** 0.2***
(0.04) (0.1)
wind 0.2 0.1
(0.2) (0.2)
precip 18.9*** 18.2***
(3.2) (3.2)
quarter2 -2.3
(2.7)
quarter3 -7.5**
(3.2)
quarter4 -4.8**
(2.1)
Constant 7.3** 6.1*
(3.1) (3.5)
Observations 364 364
R2 0.1 0.1
Adjusted R2 0.1 0.1
Residual Std. Error 13.2 (df = 360) 13.1 (df = 357)
F Statistic 12.9*** (df = 3; 360) 8.3*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

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.

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.2 0.1
(0.2) (0.2)
precip 18.9*** 18.2***
(3.2) (3.2)
quarter2 -2.3
(2.7)
quarter3 -7.5**
(3.2)
quarter4 -4.8**
(2.1)
Constant 7.3** 6.1*
(3.1) (3.5)
Observations 364 364
R2 0.1 0.1
Adjusted R2 0.1 0.1
Residual Std. Error 13.2 (df = 360) 13.1 (df = 357)
F Statistic 12.9*** (df = 3; 360) 8.3*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Drop leading zeros from decimals

stargazer(output, output2, type = "html", 
          initial.zero = FALSE)
Dependent variable:
delay
(1) (2)
temp .088** .184***
(.041) (.069)
wind .166 .114
(.164) (.164)
precip 18.918*** 18.167***
(3.249) (3.230)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 .097 .122
Adjusted R2 .089 .107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Change the order of the variables

The order argument will also accept a vector of regular expressions.

stargazer(output, output2, type = "html", 
          order = c(4, 5, 6, 3, 2, 1))
Dependent variable:
delay
(1) (2)
quarter2 -2.265
(2.660)
quarter3 -7.526**
(3.226)
quarter4 -4.757**
(2.104)
precip 18.918*** 18.167***
(3.249) (3.230)
wind 0.166 0.114
(0.164) (0.164)
temp 0.088** 0.184***
(0.041) (0.069)
Constant 7.263** 6.142*
(3.099) (3.536)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01

Select which variables to keep in the table

By default keep = NULL meaning all variables are included. keep accepts a vector of regular expressions.

# Regex for keep "precip" but not "precipitation"
stargazer(output, output2, type = "html", 
          keep = c("\\bprecip\\b"))
Dependent variable:
delay
(1) (2)
precip 18.918*** 18.167***
(3.249) (3.230)
Observations 364 364
R2 0.097 0.122
Adjusted R2 0.089 0.107
Residual Std. Error 13.248 (df = 360) 13.119 (df = 357)
F Statistic 12.879*** (df = 3; 360) 8.253*** (df = 6; 357)
Note: p<0.1; p<0.05; p<0.01


Back to table of contents

Feedback

The .Rmd file for this cheatsheet is on GitHub and I welcome suggestions or pull requests.


Back to table of contents