DeclareDesign / estimatr

estimatr: Fast Estimators for Design-Based Inference
https://declaredesign.org/r/estimatr
Other
131 stars 20 forks source link

Add support for right-sided formulas as arguments `clusters` and `weights` #381

Closed rossellhayes closed 3 years ago

rossellhayes commented 3 years ago

This PR addresses #380 by adding support for right-sided formulas as arguments clusters and weights in model functions. With this change, clusters and weights can more easily be set by other functions calling estimatr models, solving the motivating problem in #380:

purrr::map(
  list(~ am, ~ cyl),
  ~ estimatr::lm_robust(mpg ~ hp, data = mtcars, clusters = .)
)
#> [[1]]
#>                Estimate  Std. Error   t value   Pr(>|t|)   CI Lower    CI Upper
#> (Intercept) 30.09886054 2.450272545  12.28388 0.05171030 -1.0340203 61.23174140
#> hp          -0.06822828 0.004484306 -15.21490 0.03879625 -0.1215026 -0.01495395
#>                   DF
#> (Intercept) 1.000011
#> hp          1.029391
#> 
#> [[2]]
#>                Estimate Std. Error   t value   Pr(>|t|)   CI Lower  CI Upper
#> (Intercept) 30.09886054 5.04648516  5.964322 0.05856662 -3.7089449 63.906666
#> hp          -0.06822828 0.02652462 -2.572262 0.17539476 -0.2460305  0.109574
#>                   DF
#> (Intercept) 1.394247
#> hp          1.393706

Created on 2021-04-09 by the reprex package (v1.0.0)

nfultz commented 3 years ago

See also my comment on the ticket - if the workaround is sufficient, no need to merge - https://github.com/DeclareDesign/estimatr/issues/380#issuecomment-817151396

rossellhayes commented 3 years ago

Agreed, using quos was able to handle my use case, so I think that should be sufficient.