2 Comments
Jun 23, 2022·edited Jun 23, 2022Liked by scott cunningham

Dear Scott,

this blogpost is incredibly helpful, I'm using it for my teaching. Muggles like me only really understand stuff when we code it ourselves and since I’m not fluent in Stata code, I didn’t completely understand the DGP at first glance. I quickly translated that Stata code into R so you don’t need to load a dataset from Stata into R. Of course, the RNG is a bit different even with the same seed but the general picture is the same. Maybe this is useful for your blog for others trying to get their head around this. With this, also R users can directly play around with the different group effects that are coded into this data.

Best,

Jakob

p.s. Here the code:

# we want 1000 firms in 40 states, information from 1980 till 2009

library("data.table")

set.seed(20200403)

Data <- data.table(state = rep(1:40, each = 750),

firms = rep(runif(1000, min = 0, max = 5), each = 30),

year = rep(1980:2009, 1000),

n = rep(1:1000, each = 30),

id = rep(1:1000, each = 30),

group = rep(1:4, each = 7500),

treat_date = rep(c(1986, 1992, 1998, 2004), each = 7500),

e = rnorm(30000,0,(0.5)^2),

te = c(rnorm(7500, 10, (0.2)^2), rnorm(7500, 8, (0.2)^2),

rnorm(7500, 6, (0.2)^2), rnorm(7500, 4, (0.2)^2)))

Data[, treat := ifelse(year >= treat_date, 1, 0)]

Data[, time_til := year - treat_date]

Data[, treated := 1] # I agree that this is pretty pointless, could simply throw

# a constant into the feols regression and interact with that one

# DGP:

Data[, y := firms + n + treat*te*(year - treat_date + 1) + e]

# Naive TWFE Event Study (SEs clustered by state)

res_naive = feols(y ~ i(time_til, treated, ref = -1) |

id + year,

Data, vcov = ~state)

res_cohort = feols(y ~ sunab(treat_date, year) | id + year,

Data, subset = ~year<2004, ## NB: subset!

vcov = ~state)

# Can also use iplot to plot them together

iplot(list(res_naive, res_cohort), ref.line = -1,

main = "Treatment's effect on y")

legend("topright", col = c(1, 2), pch = c(20, 17),

legend = c("TWFE", "Sun & Abraham"))

Expand full comment