grantmcdermott / etwfe

Extended two-way fixed effects
https://grantmcdermott.com/etwfe/
Other
50 stars 11 forks source link

Two errors: (1) "Warning message: The demeaning algorithm did not converge, the results are not reliable. (tol: 1e-06, iter: 2036)" and (2) "Error: Class of the `.Dtreat` variable is class is not supported." #25

Closed simonvanteutem closed 1 year ago

simonvanteutem commented 1 year ago

Hi, this is a really cool package, and I'm trying to make it work for my dataset. Please note that I'm a relative beginner in R.

Brief description of my data:

Brief description of my problems

My code:


First, I impute some values for NAs of controls

suppressPackageStartupMessages(library(mice))
missing_vars <- c("age", "p_n_w_al", "p_eenp_hh", "p_ink_hi", "p_00_14_jr", "g_woz")
df_missing <- tk_cbs_test_12_21_80_t12_hosp[, missing_vars]
mids_obj <- mice(df_missing)
df_imputed <- complete(mids_obj)
tk_cbs_test_12_21_80_t12_hosp_imp <- cbind(tk_cbs_test_12_21_80_t12_hosp[, -which(names(tk_cbs_test_12_21_80_t12_hosp) %in% missing_vars)], df_imputed)

Second, create first.treat variable that is the same for all treated neighbourhoods

tk_cbs_test_12_21_80_t12_hosp_imp$first.treat <- ifelse(tk_cbs_test_12_21_80_t12_hosp_imp$evertr == 1, 2015, 0)

Third, convert all controls to numeric variables where this was not yet the case

tk_cbs_test_12_21_80_t12_hosp_imp$p_eenp_hh <- as.numeric(tk_cbs_test_12_21_80_t12_hosp_imp$p_eenp_hh)
tk_cbs_test_12_21_80_t12_hosp_imp$p_00_14_jr <- as.numeric(tk_cbs_test_12_21_80_t12_hosp_imp$p_00_14_jr)

Fourth, specify the model

twoway_FE_12_21_80_co =
  etwfe(
    fml = p_prrp ~ treated + urban + age + p_n_w_al + p_eenp_hh + p_ink_hi + p_00_14_jr + g_woz,
    tvar = jaar.x,
    gvar = first.treat,
    ivar = buurt2021,
    data = tk_cbs_test_12_21_80_t12_hosp_imp
  )

# Have a look at the results
twoway_FE_12_21_80_co

Fifth, recover the Average Treatment Effect (ATE)

emfx(twoway_FE_12_21_80_co)

I hope you can help me out! Regardless of the answer, many thanks for this cool package.

grantmcdermott commented 1 year ago

Hi Simon,

It's tricky without having access to your data---can you at least print out the results of head(tk_cbs_test_12_21_80_t12_hosp_imp)?---but my first guess is that your imputed treatment date is mislaligned. Can you retry from step 2, except this time set the first.treat date to 2021?

grantmcdermott commented 1 year ago

Finally, I have one more question: is it also possible to use the staggered treatment with, in some cases, repeated treatments? To illustrate: suppose I have the relevant data for 2010, 2012, 2017 and 2021, but some neighbourhoods get treated multiple times (e.g. in between 2010 and 2012, but again in 2012 and 2017). Do I then just create a separate first.treat value for those groups, or is there another way around this?

I believe that the ETWFE framework should, conceptually, be able to handle multiple treatments as well as turning treatment off and on again. Unfortunately, I haven't coded that up in this implementation yet. You might consider the did_multiplegt package; though---fair warning---it's quite slow and not particularly well documented. I've written up an overview of it here.

grantmcdermott commented 1 year ago

@simonvanteutem, any updates on this?

grantmcdermott commented 1 year ago

Closing in the absence of further updates. Please let me know if there's still an issue.