MatthieuStigler / multiDiff

Multi-period diff and diff
Other
3 stars 0 forks source link

better error for `mdd_event_study()` when `trim_low=-1` #26

Closed MatthieuStigler closed 1 month ago

MatthieuStigler commented 1 year ago

Should do some simple argument checking!

library(multiDiff)
DID_dat <- sim_dat_common(timing_treatment = 5:10, as_mdd = TRUE)

mdd_event_study(DID_dat, trim_low=-1)
#> OLS estimation, Dep. Var.: y_var
#> Observations: 10,000 
#> Fixed-effects: unit.index: 1,000,  time.index: 10
#> Standard-errors: Clustered (unit.index) 
#>                  Estimate Std. Error t value  Pr(>|t|)    
#> timing_to_treat0 1.075190   0.082824 12.9817 < 2.2e-16 ***
#> timing_to_treat1 1.055766   0.078156 13.5084 < 2.2e-16 ***
#> timing_to_treat2 1.055685   0.081123 13.0133 < 2.2e-16 ***
#> timing_to_treat3 0.981621   0.085285 11.5099 < 2.2e-16 ***
#> timing_to_treat4 1.084160   0.079797 13.5866 < 2.2e-16 ***
#> timing_to_treat5 0.913641   0.084137 10.8590 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.953208     Adj. R2: 0.798443
#>                  Within R2: 0.050119
mdd_event_study(DID_dat, trim_low=0)
#> Error in `mutate()`:
#> ℹ In argument: `timing_to_treat = relevel(factor(.data$timing_to_treat),
#>   as.character(time.omit))`.
#> Caused by error in `relevel.factor()`:
#> ! 'ref' must be an existing level
#> Backtrace:
#>      ▆
#>   1. ├─multiDiff::mdd_event_study(DID_dat, trim_low = 0)
#>   2. │ └─... %>% ...
#>   3. ├─dplyr::mutate(...)
#>   4. ├─dplyr:::mutate.data.frame(...)
#>   5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   6. │   ├─base::withCallingHandlers(...)
#>   7. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   8. │     └─mask$eval_all_mutate(quo)
#>   9. │       └─dplyr (local) eval()
#>  10. ├─stats::relevel(factor(.data$timing_to_treat), as.character(time.omit))
#>  11. ├─stats:::relevel.factor(factor(.data$timing_to_treat), as.character(time.omit))
#>  12. │ └─base::stop("'ref' must be an existing level")
#>  13. └─base::.handleSimpleError(...)
#>  14.   └─dplyr (local) h(simpleError(msg, call))
#>  15.     └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

Created on 2023-11-09 with reprex v2.0.2