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

margins() ignores use of I() inside formula #150

Open
langbe opened this issue Jun 21, 2020 · 3 comments
Open

margins() ignores use of I() inside formula #150

langbe opened this issue Jun 21, 2020 · 3 comments
Labels

Comments

@langbe
Copy link

@langbe langbe commented Jun 21, 2020

  • a possible bug
  • a question about package functionality
  • a suggested code or documentation change, improvement to the code, or feature request

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.

library(margins)
# re-scale predictor variable hp manually works
mtcars2 <- mtcars
mtcars2$hp <- mtcars2$hp / 50
margins(lm(mpg ~ hp, data = mtcars2))
#> Average marginal effects
#> lm(formula = mpg ~ hp, data = mtcars2)
#>      hp
#>  -3.411

# re-scale within formula does not work:
# margins() ignores re-scaling via I(hp / 50) and produces same result as in
# margins(lm(mpg ~ hp, data = mtcars))
ame_scaled <- margins(lm(mpg ~ I(hp / 50), data = mtcars))
ame_scaled
#> Average marginal effects
#> lm(formula = mpg ~ I(hp/50), data = mtcars)
#>        hp
#>  -0.06823
all.equal(
  summary(ame_scaled)$AME[[1]],
  summary(margins(lm(mpg ~ hp, data = mtcars)))$AME[[1]]
)
#> [1] TRUE

Created on 2020-06-21 by the reprex package (v0.3.0)

Session info
sessionInfo()
#> R version 4.0.0 (2020-04-24)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS High Sierra 10.13.6
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] margins_0.3.23
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.4.6      digest_0.6.25     MASS_7.3-51.6     magrittr_1.5     
#>  [5] evaluate_0.14     highr_0.8         rlang_0.4.6       stringi_1.4.6    
#>  [9] data.table_1.12.8 rmarkdown_2.1     prediction_0.3.14 tools_4.0.0      
#> [13] stringr_1.4.0     xfun_0.14         yaml_2.2.1        compiler_4.0.0   
#> [17] htmltools_0.4.0   knitr_1.28
@leeper
Copy link
Owner

@leeper leeper commented Jun 22, 2020

This is by design. margins() reports effects at the scale of the original variable, not the term generated by I().

@leeper leeper added the question label Jun 22, 2020
@langbe
Copy link
Author

@langbe langbe commented Jun 22, 2020

Thanks for the quick reply! Let me ask one more question about the general principle behind this. So, how about other transformations like log(hp), stats::poly(hp, 2) or splines::ns(hp)? The first two are not ignored by margins() whereas the last one will again be ignored.

@leeper
Copy link
Owner

@leeper leeper commented Jun 24, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.