lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
378 stars 61 forks source link

Calculating ATT for a given range of post-periods #403

Open kennchua opened 1 year ago

kennchua commented 1 year ago

In the package example for two-way fixed effects and sunab(), we can get the ATT as follows:

data(base_stagg)
res_twfe = feols(y ~ x1 + i(time_to_treatment, treated,
                            ref = c(-1, -1000)) | id + year, base_stagg)
aggregate(res_twfe, c("ATT" = "treatment::[^-]"))

res_sunab = feols(y ~ x1 + sunab(year_treated, year) | id + year, base_stagg)
aggregate(res_sunab, c("ATT" = "year::[^-]"))

How would one approach getting the ATT for a subset of the periods (e.g. 0-3)? I tried the following for TWFE regressions:

aggregate(res_twfe, c("ATT" = "time_to_treatment::(0|1|2|3):treated$"))

But it just returns the estimates for each period rather than an aggregate.

I think it's possible to use something like the marginaleffects::hypotheses() command.

marginaleffects::hypotheses(res_twfe, 
                            paste("(",paste(paste("`", paste("time_to_treatment::", 0:3, ":treated", sep = ""), "`", sep = ""), collapse = "+"), ")/4=0", sep = "")) |>
    broom::tidy()

But in order to do this for output of sunab(), I would need the aggregated variance-covariance matrix.

Would appreciate any leads on how to aggregate effects for a range of post-treatment periods particularly for sunab() regressions.

grantmcdermott commented 1 year ago

Possible duplicate of #295.

tsquall121 commented 6 days ago

Hey, @kennchua @grantmcdermott @lrberge @vincentarelbundock I am facing the same issue here. I used the function as suggested in #295 to extract the variance-covariance matrix, but I wasn't sure how to aggregate it and incorporate it into the marginaleffects::hypotheses function.

marginaleffects::hypotheses(res_sunab, hypothesis = "mean(c(year::0, year::1,year::2,year::3)) = 0")

After running the code above, the coefficient is -2.91 and everything else is NAs, along with 19 warnings saying In names(model[["coefficients"]]) == names(coefs) : longer object length is not a multiple of shorter object length. It seems to suggest that the coefficients included in the test is shorter than the original model.

Therefore, I tried to do what was suggested in the previous post - including the variance-covariance matrix to match the length of the coefficients. sunab_beta_vcv(res_sunab)$sigma[9:12, 9:12]

However, I don't know how to include this into the function. When I did the following, I got errors:

marginaleffects::hypotheses(res_sunab, hypothesis = "mean(c(year::0, year::1,year::2,year::3)) = 0", vcov = sunab_beta_vcv(res_sunab)$sigma[9:12, 9:12])

Error: The jacobian does not match the variance-covariance matrix. In addition: There were 19 warnings (use warnings() to see them)