Skip to content

Instantly share code, notes, and snippets.

Created Feb 20, 2022
What would you like to do?
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('')
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)
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) |>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment