bcallaway11 / did

Difference in Differences with Multiple Periods, website: https://bcallaway11.github.io/did
287 stars 91 forks source link

Confidence intervals for aggregated ATT(g,t)'s for each group based on calendar time #168

Closed npalardy322 closed 1 year ago

npalardy322 commented 1 year ago

Thank you for the fantastic package.

I am using "did" to estimate the group-specific effects of a policy with staggered implementation across different states using monthly data.

The effect appears to be seasonal across all groups. I am able to aggregate the ATT's into group-specific seasonal effects (the average of the ATT's in each three month window) without any issue. However, I am unsure how to compute an estimate of the asymptotic variance for the group-specific seasonal effects. I suspect I can use an approach similar to the answer you provide in #107, but I am a bit stuck.

Any help would be appreciated.

bcallaway11 commented 1 year ago

Yes, I think you are right. I would need a little more details from you (and probably more time too) to specifically answer this, but I think you are on the right track. Basically, if you can recover the aggregated parameter that you are targeting, you should be able to pick up the corresponding columns from the influence function (as in the linked example) and use those to come up with the asymptotic variance, standard errors, etc.

Brant

npalardy322 commented 1 year ago

Thank you for the quick reply. A little more context: I have monthly observations from a balanced panel containing 38 time periods. I have four groups, three treated and one control. The treated groups received treatment in periods 22, 25, and 28. There are reasons to think the treatment will have a greater effect in the summer, so I would like to aggregate the post-treatment ATT's to find group-specific seasonal effects. To accomplish this, I split the post-treatment period into three month windows representing each season and find the average of the ATT's for each group within each window (e.g. the average of the ATT's for group 22 across periods 30-32). I am currently stuck on how to compute an estimate of the asymptotic variance for these group-specific seasonal effects.

npalardy322 commented 1 year ago

I found a solution. Here is some example code that borrows heavily from Brant's reply to #107. `

compute average effect over custom range (in this case 2005-2007) for group treated in 2004

library(did) library(BMisc) library(tidyverse) data(mpdta)

out <- att_gt(yname = "lemp", gname = "first.treat", idname = "countyreal", tname = "year", xformla = ~lpop, data = mpdta, control_group="notyettreated" ) grp <- aggte(out, type="group")

which lengths of exposure to keep for group 2004

e_keep <- tibble(out$t, out$group) %>% rename(t = 'out$t', group = 'out$group') %>% mutate(keep = if_else(t %in% c(2005:2007) & group %in% 2004, T, F)) e_keep <- e_keep$keep

compute the averages

avg_att <- mean(out$att[e_keep])

get the influence function

keep_if <- out$inffunc[,e_keep] n <- nrow(keep_if)

average influence function

avg_if <- apply(keep_if, 1, mean)

estimate asymptotic variance

V <- mean(avg_if^2)

get standard errors

se <- sqrt(V)/sqrt(n)

get critical value

crit <- grp$crit.val.egt

results

data.frame(avg_att=avg_att, se=se, conf_low = avg_att - secrit, conf_high = avg_att + secrit) `