bcallaway11 / did

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

Add call to return object #121

Closed grantmcdermott closed 2 years ago

grantmcdermott commented 2 years ago

To clarify the title of this issue: I know that the call is actually contained within the DIDparams list of MP objects. But exposing it at the top level too would make it possible to invoke standard convenience functions like update. For example:

library(did)

data(mpdta)

mod = att_gt(yname="lemp",
             tname="year",
             idname="countyreal",
             gname="first.treat",
             xformla=NULL,
             data=mpdta)
mod
#> 
#> Call:
#> att_gt(yname = "lemp", tname = "year", idname = "countyreal", 
#>     gname = "first.treat", xformla = NULL, data = mpdta)
#> 
#> 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> 
#> 
#> Group-Time Average Treatment Effects:
#>  Group Time ATT(g,t) Std. Error [95% Simult.  Conf. Band]  
#>   2004 2004  -0.0105     0.0248       -0.0778      0.0568  
#>   2004 2005  -0.0704     0.0328       -0.1596      0.0188  
#>   2004 2006  -0.1373     0.0398       -0.2456     -0.0289 *
#>   2004 2007  -0.1008     0.0354       -0.1971     -0.0046 *
#>   2006 2004   0.0065     0.0241       -0.0591      0.0721  
#>   2006 2005  -0.0028     0.0188       -0.0537      0.0482  
#>   2006 2006  -0.0046     0.0174       -0.0519      0.0427  
#>   2006 2007  -0.0412     0.0207       -0.0975      0.0150  
#>   2007 2004   0.0305     0.0154       -0.0113      0.0724  
#>   2007 2005  -0.0027     0.0167       -0.0483      0.0428  
#>   2007 2006  -0.0311     0.0185       -0.0814      0.0192  
#>   2007 2007  -0.0261     0.0170       -0.0722      0.0201  
#> ---
#> Signif. codes: `*' confidence band does not cover 0
#> 
#> P-value for pre-test of parallel trends assumption:  0.16812
#> Control Group:  Never Treated,  Anticipation Periods:  0
#> Estimation Method:  Doubly Robust

Now, let's try re-run our model without 2007 data using update().

newdat = subset(mpdta, year!=2007)

## Standard update approach doesn't work because call isn't at the top level
update(mod, data = newdat)
#> Error in update.default(mod, data = newdat): need an object with call component

## But it works fine if we add it manually
mod$call = mod$DIDparams$call
update(mod, data = newdat)
#> 
#> Call:
#> att_gt(yname = "lemp", tname = "year", idname = "countyreal", 
#>     gname = "first.treat", xformla = NULL, data = newdat)
#> 
#> 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> 
#> 
#> Group-Time Average Treatment Effects:
#>  Group Time ATT(g,t) Std. Error [95% Simult.  Conf. Band]  
#>   2004 2004  -0.0196     0.0235       -0.0765      0.0373  
#>   2004 2005  -0.0787     0.0297       -0.1505     -0.0069 *
#>   2004 2006  -0.1363     0.0373       -0.2264     -0.0461 *
#>   2006 2004  -0.0026     0.0231       -0.0584      0.0533  
#>   2006 2005  -0.0019     0.0195       -0.0491      0.0452  
#>   2006 2006   0.0047     0.0172       -0.0369      0.0462  
#> ---
#> Signif. codes: `*' confidence band does not cover 0
#> 
#> P-value for pre-test of parallel trends assumption:  0.98941
#> Control Group:  Never Treated,  Anticipation Periods:  0
#> Estimation Method:  Doubly Robust

Created on 2022-03-25 by the reprex package (v2.0.1)

PS. My immediate motivation for this request is that I'd like to add support for MP objects to my ritest package. Being able to pass a quick dataset update to an existing object in a will make it much easier to perform randomization inference. But I can think of several other downstream methods that could benefit too.

PPS. Happy to submit a PR with this simple change if you'd prefer that.

bcallaway11 commented 2 years ago

Hi Grant,

Thanks, yes, I'd be happy to take a pull request on this. This would be a good change, and I think I just didn't know the right place to store the call.

Brant