A Stargazer Cheatsheet

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 todata.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")
 
StatisticNMeanSt. Dev.MinMax
 
year3642,013.0000.0002,0132,013
month3646.5113.445112
day36415.6798.784131
delay36415.08013.883-1.34997.771
temp36455.48117.58115.49291.168
wind3649.3394.3632.01455.669
precip3640.0730.2140.0001.890
hot3640.0220.14701
 


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)
 
yearmonthdaydelaytempwindprecipquarterhot
 
2,0131117.48438.48012.75901FALSE
2,0131225.32328.83512.51501FALSE
2,013138.45029.4577.86401FALSE
2,0131412.10433.47713.85701FALSE
2,013155.69636.73310.83701FALSE
2,0131612.38337.9708.00801FALSE
 

Remove row and column names

stargazer(both2, type = "html", summary = FALSE,
          rownames = FALSE,
          colnames = FALSE)
 
2,0131117.48438.48012.75901FALSE
2,0131225.32328.83512.51501FALSE
2,013138.45029.4577.86401FALSE
2,0131412.10433.47713.85701FALSE
2,013155.69636.73310.83701FALSE
2,0131612.38337.9708.00801FALSE
 

Change which statistics are displayed

In order to customize which summary statistics are displayed, change any of the the following (logical) parameters, nobsmean.sd,min.maxmedian, and iqr.

stargazer(both, type = "html", nobs = FALSE, mean.sd = TRUE, median = TRUE,
          iqr = TRUE)
 
StatisticMeanSt. Dev.MinPctl(25)MedianPctl(75)Max
 
year2,013.0000.0002,0132,0132,0132,0132,013
month6.5113.4451479.212
day15.6798.78418162331
delay15.08013.883-1.3495.44610.50120.00797.771
temp55.48117.58115.49239.87356.96071.57091.168
wind9.3394.3632.0146.5578.84711.55655.669
precip0.0730.2140.0000.0000.0000.0201.890
hot0.0220.14700001
 

Change which statistics are displayed (a second way)

stargazer(both, type = "html", summary.stat = c("n", "p75", "sd"))
 
StatisticNPctl(75)St. Dev.
 
year3642,0130.000
month3649.23.445
day364238.784
delay36420.00713.883
temp36471.57017.581
wind36411.5564.363
precip3640.0200.214
hot36400.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)
 
StatisticNMeanSt. Dev.MinMax
 
year3642,013.0000.0002,0132,013
month3646.5113.445112
day36415.6798.784131
delay36415.08013.883-1.34997.771
temp36455.48117.58115.49291.168
wind3649.3394.3632.01455.669
precip3640.0730.2140.0001.890
 

Flip the table axes

stargazer(both, type = "html", flip = TRUE)
 
Statisticyearmonthdaydelaytempwindpreciphot
 
N364364364364364364364364
Mean2,013.0006.51115.67915.08055.4819.3390.0730.022
St. Dev.0.0003.4458.78413.88317.5814.3630.2140.147
Min2,01311-1.34915.4922.0140.0000
Max2,013123197.77191.16855.6691.8901
 


Back to table of contents

The default regression table

stargazer(output, output2, type = "html")
 
 Dependent variable:
   
 delay
 (1)(2)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
N364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
Temperature0.088**0.184***
 (0.041)(0.069)
   
Wind speed0.1660.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)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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:
   
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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
 GoodBetter
 (1)(2)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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.separatestargazer 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
 GoodBetter
 (1)(2)(3)(4)
 
temp0.088**0.088**0.184***0.184***
 (0.041)(0.041)(0.069)(0.069)
     
wind0.1660.1660.1140.114
 (0.164)(0.164)(0.164)(0.164)
     
precip18.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)
     
Constant7.263**7.263**6.142*6.142*
 (3.099)(3.099)(3.536)(3.536)
     
 
Observations364364364364
R20.0970.0970.1220.122
Adjusted R20.0890.0890.1070.107
Residual Std. Error13.248 (df = 360)13.248 (df = 360)13.119 (df = 357)13.119 (df = 357)
F Statistic12.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
 OLSinstrumental
 variable
 (1)(2)(3)
 
temp0.088**0.184***0.034
 (0.041)(0.069)(0.140)
    
wind0.1660.1140.116
 (0.164)(0.164)(0.207)
    
precip18.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) 
    
Constant7.263**6.142*10.730
 (3.099)(3.536)(9.138)
    
 
Observations364364364
R20.0970.1220.093
Adjusted R20.0890.1070.085
Residual Std. Error13.248 (df = 360)13.119 (df = 357)13.280 (df = 360)
F Statistic12.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:
   
 delaydelaydelay
 OLSOLSinstrumental
 variable
 (1)(2)(3)
 
temp0.088**0.184***0.034
 (0.041)(0.069)(0.140)
    
wind0.1660.1140.116
 (0.164)(0.164)(0.207)
    
precip18.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) 
    
Constant7.263**6.142*10.730
 (3.099)(3.536)(9.138)
    
 
Observations364364364
R20.0970.1220.093
Adjusted R20.0890.1070.085
Residual Std. Error13.248 (df = 360)13.119 (df = 357)13.280 (df = 360)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Fixed effects?NoNo
Results believable?MaybeTry again later
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 outputoutput2
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.0880.184
 t = 2.165**t = 2.683***
   
wind0.1660.114
 t = 1.016t = 0.700
   
precip18.91818.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**
   
Constant7.2636.142
 t = 2.344**t = 1.737*
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.008, 0.168)(0.050, 0.318)
   
wind0.1660.114
 (-0.155, 0.488)(-0.206, 0.435)
   
precip18.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)
   
Constant7.263**6.142*
 (1.189, 13.337)(-0.789, 13.072)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.021 @@ 0.155)(0.071 @@ 0.297)
   
wind0.1660.114
 (-0.103 @@ 0.436)(-0.155 @@ 0.384)
   
precip18.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)
   
Constant7.263**6.142*
 (2.166 @@ 12.360)(0.325 @@ 11.958)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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 theHC1 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 coeftci.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)
 
temp0.088**0.088**
 (0.041)(0.043)
   
wind0.1660.166
 (0.164)(0.159)
   
precip18.918***18.918***
 (3.249)(4.735)
   
Constant7.263**7.263**
 (3.099)(3.053)
   
 
F Statistic (df = 3; 360)12.879***7.73***
Observations364364
R20.0970.097
Adjusted R20.0890.089
Residual Std. Error (df = 360)13.24813.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)
 
Constant7.263**6.142*
 (3.099)(3.536)
   
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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 argumentno.space is automatically set to TRUE.

stargazer(output, output2, type = "html",
          single.row = TRUE)
 
 Dependent variable:
   
 delay
 (1)(2)
 
temp0.088** (0.041)0.184*** (0.069)
wind0.166 (0.164)0.114 (0.164)
precip18.918*** (3.249)18.167*** (3.230)
quarter2 -2.265 (2.660)
quarter3 -7.526** (3.226)
quarter4 -4.757** (2.104)
Constant7.263** (3.099)6.142* (3.536)
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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 toomit.yes.no = c("Yes", "No").

stargazer(output, output2, type = "html", 
          omit        = "quarter",
          omit.labels = "Quarter dummies?")
 
 Dependent variable:
   
 delay
 (1)(2)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Quarter dummies?NoNo
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
Adjusted R20.0890.107
Residual Std. Error13.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. Seetable 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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.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 stargazerwhich 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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 

Omit the degrees of freedom

stargazer(output, output2, type = "html", 
          df = FALSE)
 
 Dependent variable:
   
 delay
 (1)(2)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.24813.119
F Statistic12.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)
 
temp0.088@@0.184@@@
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918@@@18.167@@@
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526@@
  (3.226)
   
quarter4 -4.757@@
  (2.104)
   
Constant7.263@@6.142@
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088*0.184**
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526*
  (3.226)
   
quarter4 -4.757*
  (2.104)
   
Constant7.263*6.142
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.879*** (df = 3; 360)8.253*** (df = 6; 357)
 
New note labelp<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!
 
 better caption
   
 delay
 (1)(2)
 
temp0.088**0.184***
 (0.041)(0.069)
   
wind0.1660.114
 (0.164)(0.164)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
temp0,088**0,184***
 (0,041)(0,069)
   
wind0,1660,114
 (0,164)(0,164)
   
precip18,918***18,167***
 (3,249)(3,230)
   
quarter2 -2,265
  (2,660)
   
quarter3 -7,526**
  (3,226)
   
quarter4 -4,757**
  (2,104)
   
Constant7,263**6,142*
 (3,099)(3,536)
   
 
Observations364364
R20,0970,122
Adjusted R20,0890,107
Residual Std. Error13,248 (df = 360)13,119 (df = 357)
F Statistic12,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)
 
temp0.1**0.2***
 (0.04)(0.1)
   
wind0.20.1
 (0.2)(0.2)
   
precip18.9***18.2***
 (3.2)(3.2)
   
quarter2 -2.3
  (2.7)
   
quarter3 -7.5**
  (3.2)
   
quarter4 -4.8**
  (2.1)
   
Constant7.3**6.1*
 (3.1)(3.5)
   
 
Observations364364
R20.10.1
Adjusted R20.10.1
Residual Std. Error13.2 (df = 360)13.1 (df = 357)
F Statistic12.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)
 
temp0.1**0.2***
 (0.04)(0.1)
   
wind0.20.1
 (0.2)(0.2)
   
precip18.9***18.2***
 (3.2)(3.2)
   
quarter2 -2.3
  (2.7)
   
quarter3 -7.5**
  (3.2)
   
quarter4 -4.8**
  (2.1)
   
Constant7.3**6.1*
 (3.1)(3.5)
   
 
Observations364364
R20.10.1
Adjusted R20.10.1
Residual Std. Error13.2 (df = 360)13.1 (df = 357)
F Statistic12.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)
   
precip18.918***18.167***
 (3.249)(3.230)
   
quarter2 -2.265
  (2.660)
   
quarter3 -7.526**
  (3.226)
   
quarter4 -4.757**
  (2.104)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R2.097.122
Adjusted R2.089.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
   
precip18.918***18.167***
 (3.249)(3.230)
   
wind0.1660.114
 (0.164)(0.164)
   
temp0.088**0.184***
 (0.041)(0.069)
   
Constant7.263**6.142*
 (3.099)(3.536)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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)
 
precip18.918***18.167***
 (3.249)(3.230)
   
 
Observations364364
R20.0970.122
Adjusted R20.0890.107
Residual Std. Error13.248 (df = 360)13.119 (df = 357)
F Statistic12.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值