Necessary Packages

#install.packages("stargazer")
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer

Formula notation in R

data(iris)
mod <- lm(data=iris,Sepal.Length~Petal.Length+Petal.Width+Species)
summary(mod)
## 
## Call:
## lm(formula = Sepal.Length ~ Petal.Length + Petal.Width + Species, 
##     data = iris)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.75238 -0.23089 -0.00211  0.23100  1.03108 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        3.682982   0.107403  34.291  < 2e-16 ***
## Petal.Length       0.905946   0.074311  12.191  < 2e-16 ***
## Petal.Width       -0.005995   0.156260  -0.038    0.969    
## Speciesversicolor -1.598362   0.205706  -7.770 1.32e-12 ***
## Speciesvirginica  -2.112647   0.304024  -6.949 1.16e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3392 on 145 degrees of freedom
## Multiple R-squared:  0.8367, Adjusted R-squared:  0.8322 
## F-statistic: 185.8 on 4 and 145 DF,  p-value: < 2.2e-16

lm above is R’s function for fitting Ordinary Least Squares (OLS) regressions. There are many other specialized regression models available in R, but fortunately the vast majority use similar notation for formulas.

The arguments given to lm are:

Symbol Role Example Equivalent
+ Add variable mpg~vs+disp \[mpg = intercept + \beta_1 vs + \beta_2 disp\]
* Interactions mpg~vs*disp \[mpg = intercept + \beta_1 vs + \beta_2 disp + \beta_3 vs*disp\]
. Include all variables in dataframe mpg~. \[mpg = intercept + \beta_1 cyl + \beta_2 disp + ... + \beta_{10} carb\]
- Exclude variable mpg~.-disp-hp \[mpg = intercept + \beta_1 cyl + \beta_2 drat + ... + \beta_{8} carb\]

Let’s fit a simple model with the iris built-in data:

Formatting lm style model results - stargazer package

Once you’ve fit a linear or some other model, you may want to report results. The stargazer package makes this relatively simple to do, especially in an R Markdown document. The below code will produce a common model summary format for a journal or presentation.

The code block has the R markdown option {r results = "asis"}, which instructs R Markdown to use the HTML code that stargazer produces as part of the output document.

mod <- lm(data=iris,Sepal.Length~Species)
mod1 <- lm(data=iris,Sepal.Length~Petal.Width+Species)
mod2 <- lm(data=iris,Sepal.Length~Petal.Length+Petal.Width+Species)

stargazer(mod,mod1,mod2, type = "html",  #we use html output to match our planned R Markdown output
     title = "My iris models")
My iris models
Dependent variable:
Sepal.Length
(1) (2) (3)
Petal.Length 0.906***
(0.074)
Petal.Width 0.917*** -0.006
(0.194) (0.156)
Speciesversicolor 0.930*** -0.060 -1.598***
(0.103) (0.230) (0.206)
Speciesvirginica 1.582*** -0.050 -2.113***
(0.103) (0.358) (0.304)
Constant 5.006*** 4.780*** 3.683***
(0.073) (0.083) (0.107)
Observations 150 150 150
R2 0.619 0.669 0.837
Adjusted R2 0.614 0.663 0.832
Residual Std. Error 0.515 (df = 147) 0.481 (df = 146) 0.339 (df = 145)
F Statistic 119.265*** (df = 2; 147) 98.525*** (df = 3; 146) 185.769*** (df = 4; 145)
Note: p<0.1; p<0.05; p<0.01

We can also write the table directly to a file with the out argument:

stargazer(mod, type = "html", out = "regression.html" ,title = "My iris model")

Resources

There’s a useful cheatsheet for stargazer and its myriad customization options here.

Exercise