library(data.table)
library(fixest)
This is another post in my series of attempts to learn the data.table
package for R
and to get more comfortable with base R
graphics. Today, I reimplement the data generating process and one of the models in this excellent blog post by Andrew Baker: How to create relative time indicators. I also strongly recommend watching Andrew’s talk on Difference-in-Differences staggered treatment designs.
For this exercise, we will only use two libraries:
As in the original post, the data generating process includes unit and time fixed effects, as well as unit-specific treatment effects.
\[y_{it}=\alpha_i + \alpha_t + \tau_{it} + \varepsilon_{it}\] \[\alpha_i,\alpha_t \sim N(0,1)\] \[\varepsilon_{it} \sim N(0,0.5)\] \[\mu_{it} \sim N(0.3,0.2^2)\]
and \(\tau_{it}\) is equal to the sum of each firm-specific \(\mu_{it}\) since the start of the treatment period.
We have 1000 firms split into 4 cohorts which receive treatment in 1986, 1992, 1998, and 2004.
= function() {
make_data
# unit fixed effects
= data.table(
unit unit = 1:1000,
unit_fe = rnorm(1000, 0, 1), # unit fe
state = sample(1:40, 1000, replace=TRUE), # state id
mu = rnorm(1000, .3, .2) # treatment effect
)
# year fixed effects
= data.table(
year year = 1980:2010,
year_fe = rnorm(31, 0, 1)
)
# treatment groups by state
= data.table(
treat_taus state = sample(1:40, 40),
cohort_year = sort(rep(c(1986, 1992, 1998, 2004), 10))
)
# full interaction of unit X year
= CJ(
out unit = 1:1000,
year = 1980:2010
)= merge(out, unit, by="unit")
out = merge(out, year, by="year")
out = merge(out, treat_taus, by="state")
out
# error term, treatment indicator, and treatment effects
:= rnorm(31000, 0, .5)][
out[, error := fifelse(year >= cohort_year, 1, 0)][
, treat := fifelse(treat == 1, mu, 0)]
, tau
# cumulative treatment effects
setkey(out, unit, year)
:= cumsum(tau), by="unit"]
out[, tau_cum
# dependent variable
:= unit_fe + year_fe + tau_cum + error]
out[, dep_var
return(out)
}
Plot data from a single simulation
= make_data()
dat
# empty plot window
plot(0,
type = "n",
ylim = c(-8, 20),
xlim = c(1980, 2010),
xlab = "",
ylab = "Outcome variable")
# one line per firm
for (i in 1:1000) {
lines(x = dat[unit==i]$year,
y = dat[unit==i]$dep_var,
col = adjustcolor("grey", alpha=.1))
}
# means by treatment cohort
= dat[, .(dep_var = mean(dep_var)), by=.(cohort_year, year)]
cohorts
<- c(
years "#CC6677" = 1986,
"#332288" = 1992,
"#DDCC77" = 1998,
"#117733" = 2004
)
for (y in years) {
abline(v = y,
col = names(years)[years==y],
lwd = 3)
lines(x = cohorts[cohort_year==y]$year,
y = cohorts[cohort_year==y]$dep_var,
col = names(years)[years==y],
lwd = 3)
}
# simulate data
= make_data()
dat
# drop 2004 treatment
= dat[year <= 2003]
dat
# years since/to treatment
:= year - cohort_year]
dat[, rel_year = dat[cohort_year==2004, min(rel_year)]
min_year = dat[cohort_year==2004, max(rel_year)]
max_year
# identification requires dropping two dummies
# dat[, rel_year := fifelse(!rel_year %in% c(min_year, -1), as.character(rel_year), "omitted")][
# , rel_year := factor(rel_year)][
# , rel_year := relevel(rel_year, ref=c("omitted")]
:= factor(rel_year, sort(unique(rel_year)))]
dat[, rel_year
# regression model
= dep_var ~ rel_year | unit + year
f = feols(f, data=dat) mod
The variables 'rel_year13', 'rel_year14', 'rel_year15', 'rel_year16' and 'rel_year17' have been removed because of collinearity (see $collin.var).
# clean results
= data.table("term" = names(coef(mod)), "estimate" = coef(mod))
out = out[ , term := as.numeric(gsub(".*year", "", term))]
out = out[term %in% -5:5] out
Plot results
# event study estimates
plot(x = out$term,
y = out$estimate,
pch=20,
xlim = c(-5, 5),
ylim = c(-.5, 1.5),
xlab = "Relative time",
ylab = "Estimate")
# truth
= data.table(term=-5:5)
truth := fifelse(term >= 0, (term + 1) * .3, 0)]
truth[, estimate lines(x = truth$term,
y = truth$estimate,
col = "#CC6677",
lty = "dashed")