DeclareDesign / estimatr

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

Passing Formulas as Strings Doesn't Work #317

Closed jasonyang5 closed 5 years ago

jasonyang5 commented 5 years ago

When running lm_robust while passing the formula as a string, I get the following warning and error:

Error in formula[[2]] : subscript out of bounds In addition: Warning message: Assigning non-quosure objects to quosure lists is deprecated as of rlang 0.3.0. Please coerce to a bare list beforehand with as.list()

Here is some code that can replicate the problem:

df = tibble(a = c(1,2,3,4,5), b = c(1,2,3,4,6))
lm_robust("a ~ b", df) # does not work
lm_robust(a ~ b, df) # works

I know for a fact that string formulas worked a few months ago, as I used them in functions for a paper.

For the time being, are there other ways you can 'program' lm_robust?

Thank you!

nfultz commented 5 years ago

I would generally recommend working with formula objects directly, instead of editing strings or pasting them together. There are many helper functions in the stats package:

But you should be easily be able to convert from string to formula using as.formula() - the new version of rlang is apparently no longer doing the conversion for you. So maybe try shimming that in:

lm_robust(as.formula("a ~ b"), df) # theoretically works
lukesonnet commented 5 years ago

I'm closing this, as I don't think we want to support strings as the formula.