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
margins() ignores use of I() inside formula #150
Comments
This is by design. |
Thanks for the quick reply! Let me ask one more question about the general principle behind this. So, how about other transformations like |
Thanks. I'll look into that more - the intention is for it to report based on original variable. If that's not happening, it's probably a bug. |
Thanks. Sorry again, where is this intention coming from? That essentially means |
Obviously when it comes to non-linear transformation marginal effect at the scale of the original variable is no longer constant across different values of the original predictor. |
Just adding a comment here, because I was confused by this issue. In my testing, all of these transformations are reported on the scale of the original predictor (which is also what I would have expected): library(margins)
margins(lm(mpg ~ wt + I(wt^2), data = mtcars))
#> Average marginal effects
#> lm(formula = mpg ~ wt + I(wt^2), data = mtcars)
#> wt
#> -5.845
margins(lm(mpg ~ stats::poly(wt, 2), data = mtcars))
#> Average marginal effects
#> lm(formula = mpg ~ stats::poly(wt, 2), data = mtcars)
#> wt
#> -5.845
margins(lm(mpg ~ log(wt), data = mtcars))
#> Average marginal effects
#> lm(formula = mpg ~ log(wt), data = mtcars)
#> wt
#> -5.855
margins(lm(mpg ~ splines::ns(wt), data = mtcars))
#> Average marginal effects
#> lm(formula = mpg ~ splines::ns(wt), data = mtcars)
#> wt
#> -5.344
margins(lm(mpg ~ splines::ns(wt, 5), data = mtcars))
#> Average marginal effects
#> lm(formula = mpg ~ splines::ns(wt, 5), data = mtcars)
#> wt
#> -5.931 Created on 2021-04-22 by the reprex package (v1.0.0) |
By the way - however perhaps this is not the best place to talk about it - rather unexpected consequence of library(margins)
# model without within-formula data transformations
m1 <- lm(mpg ~ wt, data = mtcars)
# model with some within-formula data transformation
m2 <- lm(mpg ~ I(wt^2), data = mtcars)
margins(m1)
#> Average marginal effects
#> lm(formula = mpg ~ wt, data = mtcars)
#> wt
#> -5.344
margins(m2)
#> Average marginal effects
#> lm(formula = mpg ~ I(wt^2), data = mtcars)
#> wt
#> -4.542
# now let's change the dataset
mtcars <- subset(mtcars, carb < 4)
# and let's call margins() on the (unaffected!) models once more
# for model with no within-formula data transformations we get the same results
margins(m1)
#> Average marginal effects
#> lm(formula = mpg ~ wt, data = mtcars)
#> wt
#> -5.344
# but for model with some within-formula data transformations results are different!
margins(m2)
#> Average marginal effects
#> lm(formula = mpg ~ I(wt^2), data = mtcars)
#> wt
#> -4.068 Created on 2021-04-22 by the reprex package (v2.0.0) That's because in the latter model there is no way to get the original variable other than turn back to the dataset (there is no untransformed data in the a model object) and consequently |
That's an interesting find, and I think it should be included in the documentation somewhere, if it isn't yet. For the specific case you describe (subsetting), margins could even figure out that the data was changed, because the design matrix has fewer rows than the updated I think it would be worth creating a new issue for this problem. |
It should be possible to detect any changes (that affect results) by comparing model frame from the model object with model frame constructed using data object in its current state and the model call. However detecting such changes perhaps should result in warning instead of error. |
Yes, that would be a pretty expensive operation for large models, though, and you would have to do it for every |
Thanks for the answer! I realize that I misunderstood the initial answer. I thought that the intention was to ignore usage of I() or any other transformation. It is however not ignored, just the final reporting is then in terms of the original variable (which makes sense). |
Using
I()
inside a formula does not work,margins()
ignores it. As an example, let's do a linear regression of mpg vs. hp from the mtcars data and re-scale hp.Created on 2020-06-21 by the reprex package (v0.3.0)
Session info
The text was updated successfully, but these errors were encountered: