Skip to content

Instantly share code, notes, and snippets.

@scunning1975
Created February 20, 2022 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scunning1975/0951967f75b83cff594d3d0c05636f6b to your computer and use it in GitHub Desktop.
Save scunning1975/0951967f75b83cff594d3d0c05636f6b to your computer and use it in GitHub Desktop.
R code to estimate TWFE event study
# ------------------------------------------------------------------------------
# name: baker_sa.R
# author: scott cunningham (with massive help from grant mcdermott who basically
# fixed all of it, so I think he's the author tbh)
# description: implement SA on the baker dataset
# last updated: february 20, 2022
# ------------------------------------------------------------------------------
# load libraries
library(haven) # Read Stata .dta files
library(fixest) # Sun & Abraham (and regular TWFE and high-dimensional FEs, etc., etc.)
# load data
baker = read_dta('https://github.com/scunning1975/mixtape/raw/master/baker.dta')
baker$treated = baker$treat_date!=0 # create a treatment variable
# NB: All units in this dataset are treated so above is kind of pointless
# Naive TWFE Event Study (SEs clustered by state)
res_naive = feols(y ~ i(time_til, treated, ref = -1) |
id + year,
baker, vcov = ~state)
summary(res_naive)
iplot(res_naive, main = "Naive TWFE")
# Again, because all units are treated you could just as well have run the
# following:
feols(y ~ i(time_til, ref = -1) |
id + year,
baker, vcov = ~state) |>
iplot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment