margins icon indicating copy to clipboard operation
margins copied to clipboard

margins plot

Open jonlatner opened this issue 7 years ago • 5 comments

Please specify whether your issue is about:

there appears to be an issue with the plot command in margins, which mislabels the coefficients and standard errors produced from the margins command.

library(margins)
x <- lm(mpg ~ factor(cyl) * am + hp + wt, data = mtcars)
mfx <- margins(x)
summary(mfx)
plot(mfx)

jonlatner avatar Oct 15 '18 10:10 jonlatner

Yes, definite bug. Thanks for reporting!

leeper avatar Feb 16 '19 17:02 leeper

This would be fixed by https://github.com/leeper/margins/pull/114

vincentarelbundock avatar Jul 13 '19 23:07 vincentarelbundock

Since the exact behavior of this bug isn't mentioned above, the bug is that labels are printed in the same order as when you directly print the margins object directly to console, but the coefficients are plotted in alphabetical order. The same reordering happens when calling summary() on a margins object, except that the names are appropriately reordered as well.

The example in the documentation has this exact problem happen to it too. Might be worthwhile quickly patching, unless the ggplot2 backend will be swapped in soon (which would be a nice improvement).

In the meantime, anyone wanting to use plot() can just use plot(m, which=summary(m)$factor) as a short workaround, where m is a margins object.

A quick fix following the form of the codebase would be to replace line 37 of plot.R with:

which = colnames(marginal_effects(x, with_at = FALSE))[order(colnames(marginal_effects(x, with_at = FALSE)))],

rmcrowley2000 avatar Sep 08 '19 08:09 rmcrowley2000

Still in 0.3.26

Screen Shot 2021-05-24 at 9 46 19 AM

grasshoppermouse avatar May 24 '21 16:05 grasshoppermouse

For what it’s worth, I have completely re-written the plotting functions in margins to produce ggplot2 objects. This re-write fixes the problem in this Issue.

I eventually closed the pull request because it sat there without comments for over a year. (Not blaming the maintainer; this is volunteer work and I know he’s super busy.) If you want, you can still install my re-write from this repository:

https://github.com/vincentarelbundock/marginsplot

Make sure you load the marginsplot package after margins to override the default methods:

library(margins)
library(marginsplot)

x <- lm(mpg ~ wt * hp + cyl, mtcars)
mar <- margins(x)
plot(mar)

Session info
sessioninfo::session_info()
#> Warning in system("timedatectl", intern = TRUE): running command 'timedatectl'
#> had status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       Ubuntu 20.04.2 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  C.UTF-8                     
#>  ctype    C.UTF-8                     
#>  tz       America/Toronto             
#>  date     2021-05-24                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib
#>  assertthat    0.2.1      2019-03-21 [1]
#>  backports     1.2.1      2020-12-09 [1]
#>  cli           2.5.0      2021-04-26 [1]
#>  colorspace    2.0-1      2021-05-04 [1]
#>  crayon        1.4.1      2021-02-08 [1]
#>  curl          4.3.1      2021-04-30 [1]
#>  data.table    1.14.0     2021-02-21 [1]
#>  DBI           1.1.1      2021-01-15 [1]
#>  digest        0.6.27     2020-10-24 [1]
#>  dplyr         1.0.6      2021-05-05 [1]
#>  ellipsis      0.3.2      2021-04-29 [1]
#>  evaluate      0.14       2019-05-28 [1]
#>  fansi         0.4.2      2021-01-15 [1]
#>  farver        2.1.0      2021-02-28 [1]
#>  fs            1.5.0      2020-07-31 [1]
#>  generics      0.1.0      2020-10-31 [1]
#>  ggplot2       3.3.3      2020-12-30 [1]
#>  glue          1.4.2      2020-08-27 [1]
#>  gtable        0.3.0      2019-03-25 [1]
#>  highr         0.9        2021-04-16 [1]
#>  htmltools     0.5.1.1    2021-01-22 [1]
#>  httr          1.4.2      2020-07-20 [1]
#>  knitr         1.33       2021-04-24 [1]
#>  labeling      0.4.2      2020-10-20 [1]
#>  lifecycle     1.0.0      2021-02-15 [1]
#>  magrittr      2.0.1      2020-11-17 [1]
#>  margins     * 0.3.26     2021-01-22 [1]
#>  marginsplot * 0.0.0.9000 2021-05-24 [1]
#>  MASS          7.3-53.1   2021-02-12 [1]
#>  mime          0.10       2021-02-13 [1]
#>  munsell       0.5.0      2018-06-12 [1]
#>  pillar        1.6.1      2021-05-16 [1]
#>  pkgconfig     2.0.3      2019-09-22 [1]
#>  prediction    0.3.14     2019-06-17 [1]
#>  purrr         0.3.4      2020-04-17 [1]
#>  R.cache       0.14.0     2019-12-06 [1]
#>  R.methodsS3   1.8.1      2020-08-26 [1]
#>  R.oo          1.24.0     2020-08-26 [1]
#>  R.utils       2.10.1     2020-08-26 [1]
#>  R6            2.5.0      2020-10-28 [1]
#>  reprex        2.0.0      2021-04-02 [1]
#>  rlang         0.4.11     2021-04-30 [1]
#>  rmarkdown     2.8        2021-05-07 [1]
#>  scales        1.1.1      2020-05-11 [1]
#>  sessioninfo   1.1.1      2018-11-05 [1]
#>  stringi       1.6.2      2021-05-17 [1]
#>  stringr       1.4.0      2019-02-10 [1]
#>  styler        1.3.2      2020-02-23 [1]
#>  tibble        3.1.2      2021-05-16 [1]
#>  tidyselect    1.1.1      2021-04-30 [1]
#>  utf8          1.2.1      2021-03-12 [1]
#>  vctrs         0.3.8      2021-04-29 [1]
#>  withr         2.4.1      2021-01-26 [1]
#>  xfun          0.23       2021-05-15 [1]
#>  xml2          1.3.2      2020-04-23 [1]
#>  yaml          2.2.1      2020-02-01 [1]
#>  source                                         
#>  standard (@0.2.1)                              
#>  standard (@1.2.1)                              
#>  standard (@2.5.0)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@1.4.1)                              
#>  CRAN (R 4.1.0)                                 
#>  CRAN (R 4.1.0)                                 
#>  standard (@1.1.1)                              
#>  standard (@0.6.27)                             
#>  standard (@1.0.6)                              
#>  standard (@0.3.2)                              
#>  standard (@0.14)                               
#>  standard (@0.4.2)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@1.5.0)                              
#>  standard (@0.1.0)                              
#>  standard (@3.3.3)                              
#>  standard (@1.4.2)                              
#>  standard (@0.3.0)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@0.5.1.1)                            
#>  standard (@1.4.2)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@0.4.2)                              
#>  standard (@1.0.0)                              
#>  standard (@2.0.1)                              
#>  CRAN (R 4.1.0)                                 
#>  Github (vincentarelbundock/marginsplot@124ab5f)
#>  standard (@7.3-53.)                            
#>  standard (@0.10)                               
#>  standard (@0.5.0)                              
#>  standard (@1.6.1)                              
#>  standard (@2.0.3)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@0.3.4)                              
#>  standard (@0.14.0)                             
#>  standard (@1.8.1)                              
#>  standard (@1.24.0)                             
#>  standard (@2.10.1)                             
#>  standard (@2.5.0)                              
#>  standard (@2.0.0)                              
#>  CRAN (R 4.1.0)                                 
#>  CRAN (R 4.1.0)                                 
#>  standard (@1.1.1)                              
#>  standard (@1.1.1)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@1.4.0)                              
#>  standard (@1.3.2)                              
#>  standard (@3.1.2)                              
#>  CRAN (R 4.1.0)                                 
#>  CRAN (R 4.1.0)                                 
#>  standard (@0.3.8)                              
#>  standard (@2.4.1)                              
#>  CRAN (R 4.1.0)                                 
#>  standard (@1.3.2)                              
#>  standard (@2.2.1)                              
#> 
#> [1] /home/vincent/R/x86_64-pc-linux-gnu-library/4.1
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

vincentarelbundock avatar May 24 '21 17:05 vincentarelbundock