leeper / margins

An R Port of Stata's 'margins' Command
https://cloud.r-project.org/package=margins
Other
260 stars 40 forks source link

Suppress printing of prediction data frame #110

Open bensoltoff opened 5 years ago

bensoltoff commented 5 years ago

I regularly call cplot() with plot = FALSE to generate predicted values for plotting using ggplot2. When I do this, the first 20 rows of observations with predicted values are always printed to the console in addition to returning the data frame. When using this code in an R Markdown document, I would rather not show this output in the document. But since it is generated using the print() function, there is no easy way to suppress the output using code chunk options such as message = FALSE. I have to entirely suppress the output of the code chunk with include = FALSE, which is problematic if I want to print the code in the document. Can this printing behavior be adjusted so it is generated by message() instead?

It only appears using cplot(what = "prediction"), not with cplot(what = "effect"). My guess then is that the print behavior actually comes from prediction() in the prediction package, but I wasn't entirely sure.

library(tidyverse)
library(margins)

x1 <- lm(mpg ~ drat * wt * am, data = mtcars)

# predicted values
cplot(x1, "wt", what = "prediction", draw = FALSE) %>%
  ggplot(aes(x = xvals)) + 
  geom_line(aes(y = yvals)) +
  geom_line(aes(y = upper), linetype = 2) +
  geom_line(aes(y = lower), linetype = 2) +
  geom_hline(yintercept = 0) +
  ggtitle("Predicted Fuel Economy (mpg) by Weight") +
  xlab("Weight (1000 lbs)") + ylab("Predicted Value")
#>       xvals    yvals    upper     lower
#> 1  1.513000 28.66765 33.04903 24.286273
#> 2  1.675958 27.71126 31.71243 23.710103
#> 3  1.838917 26.75488 30.38319 23.126563
#> 4  2.001875 25.79849 29.06385 22.533126
#> 5  2.164833 24.84210 27.75810 21.926095
#> 6  2.327792 23.88571 26.47147 21.299955
#> 7  2.490750 22.92932 25.21225 20.646397
#> 8  2.653708 21.97293 23.99282 19.953051
#> 9  2.816667 21.01655 22.83057 19.202525
#> 10 2.979625 20.06016 21.74656 18.373748
#> 11 3.142583 19.10377 20.75901 17.448522
#> 12 3.305542 18.14738 19.87315 16.421613
#> 13 3.468500 17.19099 19.07759 15.304391
#> 14 3.631458 16.23460 18.35187 14.117337
#> 15 3.794417 15.27821 17.67591 12.880521
#> 16 3.957375 14.32183 17.03432 11.609334
#> 17 4.120333 13.36544 16.41648 10.314396
#> 18 4.283292 12.40905 15.81532  9.002781
#> 19 4.446250 11.45266 15.22612  7.679197
#> 20 4.609208 10.49627 14.64572  6.346819


# marginal effects
cplot(x1, "wt", what = "effect", draw = FALSE) %>%
  ggplot(aes(x = xvals)) + 
  geom_line(aes(y = yvals)) +
  geom_line(aes(y = upper), linetype = 2) +
  geom_line(aes(y = lower), linetype = 2) +
  geom_hline(yintercept = 0) +
  ggtitle("AME of Axle Ratio on Fuel Economy (mpg) by Weight") +
  xlab("Weight (1000 lbs)") + ylab("AME of Axle Ratio")

Created on 2019-02-15 by the reprex package (v0.2.1)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.5.2 (2018-12-20) #> os macOS Mojave 10.14.2 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2019-02-15 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.0 2017-04-11 [2] CRAN (R 3.5.0) #> backports 1.1.3 2018-12-14 [2] CRAN (R 3.5.0) #> bindr 0.1.1 2018-03-13 [2] CRAN (R 3.5.0) #> bindrcpp 0.2.2 2018-03-29 [1] CRAN (R 3.5.0) #> broom 0.5.1 2018-12-05 [2] CRAN (R 3.5.0) #> callr 3.1.1 2018-12-21 [2] CRAN (R 3.5.0) #> cellranger 1.1.0 2016-07-27 [2] CRAN (R 3.5.0) #> cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.0) #> colorspace 1.4-0 2019-01-13 [2] CRAN (R 3.5.2) #> crayon 1.3.4 2017-09-16 [2] CRAN (R 3.5.0) #> curl 3.3 2019-01-10 [2] CRAN (R 3.5.2) #> data.table 1.12.0 2019-01-13 [2] CRAN (R 3.5.2) #> desc 1.2.0 2018-05-01 [2] CRAN (R 3.5.0) #> devtools 2.0.1 2018-10-26 [1] CRAN (R 3.5.1) #> digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.0) #> dplyr * 0.7.8 2018-11-10 [1] CRAN (R 3.5.0) #> evaluate 0.12 2018-10-09 [2] CRAN (R 3.5.0) #> forcats * 0.3.0 2018-02-19 [2] CRAN (R 3.5.0) #> fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.0) #> generics 0.0.2 2018-11-29 [1] CRAN (R 3.5.0) #> ggplot2 * 3.1.0 2018-10-25 [1] CRAN (R 3.5.0) #> glue 1.3.0 2018-07-17 [2] CRAN (R 3.5.0) #> gtable 0.2.0 2016-02-26 [2] CRAN (R 3.5.0) #> haven 2.0.0 2018-11-22 [2] CRAN (R 3.5.0) #> highr 0.7 2018-06-09 [2] CRAN (R 3.5.0) #> hms 0.4.2 2018-03-10 [2] CRAN (R 3.5.0) #> htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.5.0) #> httr 1.4.0 2018-12-11 [2] CRAN (R 3.5.0) #> jsonlite 1.6 2018-12-07 [2] CRAN (R 3.5.0) #> knitr 1.21 2018-12-10 [2] CRAN (R 3.5.1) #> labeling 0.3 2014-08-23 [2] CRAN (R 3.5.0) #> lattice 0.20-38 2018-11-04 [2] CRAN (R 3.5.2) #> lazyeval 0.2.1 2017-10-29 [2] CRAN (R 3.5.0) #> lubridate 1.7.4 2018-04-11 [2] CRAN (R 3.5.0) #> magrittr 1.5 2014-11-22 [2] CRAN (R 3.5.0) #> margins * 0.3.23 2018-05-22 [2] CRAN (R 3.5.0) #> MASS 7.3-51.1 2018-11-01 [2] CRAN (R 3.5.2) #> memoise 1.1.0 2017-04-21 [2] CRAN (R 3.5.0) #> mime 0.6 2018-10-05 [1] CRAN (R 3.5.0) #> modelr 0.1.2 2018-05-11 [2] CRAN (R 3.5.0) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 3.5.0) #> nlme 3.1-137 2018-04-07 [2] CRAN (R 3.5.2) #> pillar 1.3.1 2018-12-15 [2] CRAN (R 3.5.0) #> pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.0) #> pkgconfig 2.0.2 2018-08-16 [2] CRAN (R 3.5.1) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.0) #> plyr 1.8.4 2016-06-08 [2] CRAN (R 3.5.0) #> prediction 0.3.6.1 2018-12-04 [2] CRAN (R 3.5.0) #> prettyunits 1.0.2 2015-07-13 [2] CRAN (R 3.5.0) #> processx 3.2.1 2018-12-05 [2] CRAN (R 3.5.0) #> ps 1.3.0 2018-12-21 [2] CRAN (R 3.5.0) #> purrr * 0.3.0 2019-01-27 [2] CRAN (R 3.5.2) #> R6 2.3.0 2018-10-04 [1] CRAN (R 3.5.0) #> Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.0) #> readr * 1.3.1 2018-12-21 [2] CRAN (R 3.5.0) #> readxl 1.2.0 2018-12-19 [2] CRAN (R 3.5.0) #> remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.0) #> rlang 0.3.1 2019-01-08 [1] CRAN (R 3.5.2) #> rmarkdown 1.11 2018-12-08 [2] CRAN (R 3.5.0) #> rprojroot 1.3-2 2018-01-03 [2] CRAN (R 3.5.0) #> rvest 0.3.2 2016-06-17 [2] CRAN (R 3.5.0) #> scales 1.0.0 2018-08-09 [1] CRAN (R 3.5.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.0) #> stringi 1.2.4 2018-07-20 [2] CRAN (R 3.5.0) #> stringr * 1.3.1 2018-05-10 [2] CRAN (R 3.5.0) #> testthat 2.0.1 2018-10-13 [2] CRAN (R 3.5.0) #> tibble * 2.0.1 2019-01-12 [2] CRAN (R 3.5.2) #> tidyr * 0.8.2.9000 2019-02-11 [1] Github (tidyverse/tidyr@0b27690) #> tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.5.0) #> tidyverse * 1.2.1 2017-11-14 [2] CRAN (R 3.5.0) #> usethis 1.4.0 2018-08-14 [1] CRAN (R 3.5.0) #> withr 2.1.2 2018-03-15 [2] CRAN (R 3.5.0) #> xfun 0.4 2018-10-23 [1] CRAN (R 3.5.0) #> xml2 1.2.0 2018-01-24 [2] CRAN (R 3.5.0) #> yaml 2.2.0 2018-07-25 [2] CRAN (R 3.5.0) #> #> [1] /Users/soltoffbc/Library/R/3.5/library #> [2] /Library/Frameworks/R.framework/Versions/3.5/Resources/library ```
leeper commented 5 years ago

Thanks. Seems like a bug.

davidhen commented 5 years ago

I've had similar problems with older version of R, but when I run this reprex the problem seems to be gone.

library(tidyverse)
library(margins)

#Model

x1 <- lm(mpg ~ drat * wt * am, data = mtcars)

cplot(x1, "wt", what = "prediction", draw = FALSE) %>%
  ggplot(aes(x = xvals)) + 
  geom_line(aes(y = yvals)) +
  geom_line(aes(y = upper), linetype = 2) +
  geom_line(aes(y = lower), linetype = 2) +
  geom_hline(yintercept = 0) +
  ggtitle("Predicted Fuel Economy (mpg) by Weight") +
  xlab("Weight (1000 lbs)") + ylab("Predicted Value")


cplot(x1, "wt", what = "effect", draw = FALSE) %>%
  ggplot(aes(x = xvals)) + 
  geom_line(aes(y = yvals)) +
  geom_line(aes(y = upper), linetype = 2) +
  geom_line(aes(y = lower), linetype = 2) +
  geom_hline(yintercept = 0) +
  ggtitle("AME of Axle Ratio on Fuel Economy (mpg) by Weight") +
  xlab("Weight (1000 lbs)") + ylab("AME of Axle Ratio")

Created on 2019-02-17 by the reprex package (v0.2.1)

devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.2 (2018-12-20)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United Kingdom.1252 
#>  ctype    English_United Kingdom.1252 
#>  tz       Europe/London               
#>  date     2019-02-17                  
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.1)
#>  backports     1.1.3   2018-12-14 [1] CRAN (R 3.5.2)
#>  callr         3.1.1   2018-12-21 [1] CRAN (R 3.5.2)
#>  cli           1.0.1   2018-09-25 [1] CRAN (R 3.5.1)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.1)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.1)
#>  devtools      2.0.1   2018-10-26 [1] CRAN (R 3.5.1)
#>  digest        0.6.18  2018-10-10 [1] CRAN (R 3.5.1)
#>  evaluate      0.13    2019-02-12 [1] CRAN (R 3.5.2)
#>  fs            1.2.6   2018-08-23 [1] CRAN (R 3.5.1)
#>  glue          1.3.0   2018-07-17 [1] CRAN (R 3.5.1)
#>  highr         0.7     2018-06-09 [1] CRAN (R 3.5.1)
#>  htmltools     0.3.6   2017-04-28 [1] CRAN (R 3.5.1)
#>  knitr         1.21    2018-12-10 [1] CRAN (R 3.5.2)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.1)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.1)
#>  pkgbuild      1.0.2   2018-10-16 [1] CRAN (R 3.5.1)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.1)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.1)
#>  processx      3.2.1   2018-12-05 [1] CRAN (R 3.5.2)
#>  ps            1.3.0   2018-12-21 [1] CRAN (R 3.5.2)
#>  R6            2.4.0   2019-02-14 [1] CRAN (R 3.5.1)
#>  Rcpp          1.0.0   2018-11-07 [1] CRAN (R 3.5.1)
#>  remotes       2.0.2   2018-10-30 [1] CRAN (R 3.5.1)
#>  rlang         0.3.1   2019-01-08 [1] CRAN (R 3.5.2)
#>  rmarkdown     1.11    2018-12-08 [1] CRAN (R 3.5.2)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.1)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.1)
#>  stringi       1.2.4   2018-07-20 [1] CRAN (R 3.5.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.5.2)
#>  testthat      2.0.1   2018-10-13 [1] CRAN (R 3.5.1)
#>  usethis       1.4.0   2018-08-14 [1] CRAN (R 3.5.1)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.1)
#>  xfun          0.4     2018-10-23 [1] CRAN (R 3.5.1)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.5.1)
#> 
#> [1] C:/R/R-3.5.2/library

Created on 2019-02-17 by the reprex package (v0.2.1)

vincentarelbundock commented 5 years ago

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