bcallaway11 / did

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

Error on `conditional_did_pretest` -> "Error in l[[whichone]] : this S4 class is not subsettable" #101

Closed avila-a closed 2 years ago

avila-a commented 2 years ago

Hi,

Thank you for your amazing package and the very clear explanations you post here.

I am running on an error that was not there on the last version I had of did. The error happened on my specific data but it also happens in the reproducible example from conditional_did_pretest():

library(did)
data(mpdta)
pre.test <- conditional_did_pretest(yname="lemp",
                                    tname="year",
                                    idname="countyreal",
                                    gname="first.treat",
                                    xformla=~lpop,
                                    data=mpdta)

When I run it, I get the following error message:

Show in New Window
Step 1 of 2: Computing test statistic....
  |                                                  | 0 % ~calculating 

Error in l[[whichone]] : this S4 class is not subsettable

10. FUN(X[[i]], ...)
9. lapply(listolists, function(l) l[[whichone]])
8. BMisc::getListElement(attgt.list, "group")
7. unlist(BMisc::getListElement(attgt.list, "group"))
6. unique(unlist(BMisc::getListElement(attgt.list, "group")))
5. process_attgt(Jres)
4. FUN(X[[i]], ...)
3. lapply(X[Split[[i]]], FUN, ...)
2. pbapply::pblapply(1:nrow(X), function(i) {
www <- as.numeric(weightfun(X1, X[i, ]))
rightids <- dta[, idname][www == 1]
thisdata <- data ...
1. conditional_did_pretest(yname = "lemp", tname = "year", idname = "countyreal", 
gname = "first.treat", xformla = ~lpop, data = mpdta)

I have just installed the newer versions of R and of all packages and dependencies, including BMisc. Do you know what causes the error?

Thank you and best wishes for the year ahead,

Antonio


> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] did_2.1.0    Matrix_1.3-4

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       pillar_1.6.4     compiler_4.1.2  
 [4] ggpubr_0.4.0     BMisc_1.4.3      tools_4.1.2     
 [7] lifecycle_1.0.1  tibble_3.1.6     gtable_0.3.0    
[10] lattice_0.20-45  pkgconfig_2.0.3  rlang_0.4.12    
[13] parallel_4.1.2   xfun_0.29        dplyr_1.0.7     
[16] knitr_1.37       generics_0.1.1   vctrs_0.3.8     
[19] trust_0.1-8      DRDID_1.0.3      grid_4.1.2      
[22] tidyselect_1.1.1 glue_1.6.0       R6_2.5.1        
[25] rstatix_0.7.0    fansi_0.5.0      pbapply_1.5-0   
[28] carData_3.0-4    ggplot2_3.3.5    purrr_0.3.4     
[31] tidyr_1.1.4      car_3.0-12       magrittr_2.0.1  
[34] scales_1.1.1     backports_1.4.1  ellipsis_0.3.2  
[37] abind_1.4-5      colorspace_2.0-2 ggsignif_0.6.3  
[40] utf8_1.2.2       munsell_0.5.0    broom_0.7.10    
[43] crayon_1.4.2  
bcallaway11 commented 2 years ago

Hi Antonio,

Thanks for the message. It looks like I may have introduced a bug into the conditional_did_pretest code in the new version. For what it’s worth, my sense is that most researchers who use our package do not actually use this function and find the pre-tests that come directly out of att_gt to be sufficient (as well as being much less computationally demanding). That said, I’ll try to take a closer look at this issue this week. I’ll keep you posted.

Brant

avila-a commented 2 years ago

Many thanks Brant!

bcallaway11 commented 2 years ago

Hi Antonio,

Sorry it took me so long to get to this. But I think that I have it working now. I was able to reproduce the same sort of error that you were reporting. But I updated the code, and now it appears to work:

library(did)
# these lines just create simulated data
sp <- reset.sim(time.periods=3, n=1000)
data <- build_sim_dataset(sp)

cdp <- conditional_did_pretest(yname="Y",
                               tname="period",
                               idname="id",
                               gname="G",
                               xformla=~X,
                               data=data)
#> We are no longer updating this function.  It should continue to work, but most users find the pre-tests already reported by the `att_gt` function to be sufficient for most empirical applications.
#> Step 1 of 2: Computing test statistic....
#> Step 2 of 2: Simulating limiting distribution of test statistic....
summary(cdp)
#> Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
#> Cramer von Mises: 
#>   Test Statistic:  4.031258 
#>   Critical Value:  19.9732 
#>   P-value       :  0.543 
#>  
#> Clustering on   :  , 
#> X formula       :  ~ X

Keep me posted on if this works for you now.

Brant