margins icon indicating copy to clipboard operation
margins copied to clipboard

dydx() with change = "iqr" not working as expected

Open lang-benjamin opened this issue 4 years ago • 1 comments

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

I think option change = "iqr" in function dydx() does not work properly. For example:

library(margins)
fm <- lm(mpg ~ hp, data = mtcars)
dydx(mtcars, fm, "hp", change = "iqr")
#>      dydx_hp
#> 1  -5.731175
#> 2  -5.731175
#> 3  -5.731175
#> 4  -5.731175
#> 5  -5.731175
#> 6  -5.731175
#> 7  -5.731175
#> 8  -5.731175
#> 9  -5.731175
#> 10 -5.731175
#> 11 -5.731175
#> 12 -5.731175
#> 13 -5.731175
#> 14 -5.731175
#> 15 -5.731175
#> 16 -5.731175
#> 17 -5.731175
#> 18 -5.731175
#> 19 -5.731175
#> 20 -5.731175
#> 21 -5.731175
#> 22 -5.731175
#> 23 -5.731175
#> 24 -5.731175
#> 25 -5.731175
#> 26 -5.731175
#> 27 -5.731175
#> 28 -5.731175
#> 29 -5.731175
#> 30 -5.731175
#> 31 -5.731175
#> 32 -5.731175
# I would have expected the same result as in
dydx(mtcars, fm, "hp", change = c(quantile(mtcars$hp, 1/4), quantile(mtcars$hp, 3/4)))
#>      dydx_hp
#> 1  -5.697061
#> 2  -5.697061
#> 3  -5.697061
#> 4  -5.697061
#> 5  -5.697061
#> 6  -5.697061
#> 7  -5.697061
#> 8  -5.697061
#> 9  -5.697061
#> 10 -5.697061
#> 11 -5.697061
#> 12 -5.697061
#> 13 -5.697061
#> 14 -5.697061
#> 15 -5.697061
#> 16 -5.697061
#> 17 -5.697061
#> 18 -5.697061
#> 19 -5.697061
#> 20 -5.697061
#> 21 -5.697061
#> 22 -5.697061
#> 23 -5.697061
#> 24 -5.697061
#> 25 -5.697061
#> 26 -5.697061
#> 27 -5.697061
#> 28 -5.697061
#> 29 -5.697061
#> 30 -5.697061
#> 31 -5.697061
#> 32 -5.697061

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

lang-benjamin avatar Jun 21 '20 12:06 lang-benjamin

Thanks. This looks like it is due to the internal use of fivenum() returning integers rather than numeric quantiles. I'll switch out the implementation as that's not correct.

leeper avatar Jun 22 '20 13:06 leeper