Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.frame() gives unexpected results with iv_robust() #397

Open
ngreifer opened this issue Aug 29, 2022 · 4 comments
Open

model.frame() gives unexpected results with iv_robust() #397

ngreifer opened this issue Aug 29, 2022 · 4 comments
Milestone

Comments

@ngreifer
Copy link

Hello,

I have found that model.frame() gives unexpected results when used with iv_robust(), possibly due to it being unable to parse the model formula. With a factor variable in the model, no dataset is produced, and without factor variables, the formula is misinterpreted, with the | interpreted as "or". See reprex below:

#Using lalonde dataset in MatchIt
fit <- estimatr::iv_robust(re78 ~ treat + age + race | educ + age + race,
                           data = MatchIt::lalonde)

# With factors: failure
head(model.frame(fit))
#> Warning in Ops.factor(treat + age, race): '+' not meaningful for factors
#> Warning in Ops.factor(educ + age, race): '+' not meaningful for factors
#> [1] re78                                  
#> [2] treat + age + race | educ + age + race
#> <0 rows> (or 0-length row.names)

fit <- estimatr::iv_robust(re78 ~ treat + age | educ + age,
                           data = MatchIt::lalonde)

# Without factors: misinterpretation
head(model.frame(fit))
#>            re78 treat + age | educ + age
#> NSW1  9930.0460                     TRUE
#> NSW2  3595.8940                     TRUE
#> NSW3 24909.4500                     TRUE
#> NSW4  7506.1460                     TRUE
#> NSW5   289.7899                     TRUE
#> NSW6  4056.4940                     TRUE

Created on 2022-08-29 with reprex v2.0.2

This fix is needed to fully resolve #374, because as of right now there is no clear way to extract the original dataset from the model. Thanks!

@nfultz
Copy link
Contributor

nfultz commented Aug 29, 2022

Apparently the method dispatches differently for iv_robust than lm_robust - please try using the lm method explicitly, like:

stats:::model.frame.lm(fit)

This is another edge case for #123

@ngreifer
Copy link
Author

Thank you, unfortunately, this is for use inside insight::get_data() which I'm aware you have no control over. That function simply calls model.frame() so ideally the method for iv_robust objects would naturally dispatch correctly.

@nfultz
Copy link
Contributor

nfultz commented Aug 31, 2022

It's weird, someone in the other thread said get_data used to work? #123 (comment)

Maybe it changed?

@ngreifer
Copy link
Author

It actually produced incorrect output in that thread, as well (note the second column).

@graemeblair graemeblair added this to the summer 2023 milestone May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants