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

visreg clash with riskRegression for hazard plots #153

Open
sahirbhatnagar opened this issue Oct 14, 2021 · 2 comments
Open

visreg clash with riskRegression for hazard plots #153

sahirbhatnagar opened this issue Oct 14, 2021 · 2 comments
Labels

Comments

@sahirbhatnagar
Copy link
Owner

I have no idea where to even begin with this error. It seems there's a clash somewhere with the visreg and riskRegression packages. It occurs when I use the plot.singleEventCB function:

library(survival)
library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/
library(riskRegression)
#> riskRegression version 2021.10.10

cfit <- fitSmoothHazard(death ~ futime + sex*pspline(age, df=4), data=mgus, time = "futime")
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
plot(cfit, 
     hazard.params = list(xvar = "age",
                          by = "sex",
                          alpha = 1))
#> Error in names(Data) <- gsub("offset\\((.*)\\)", "\\1", names(Data)): names() applied to a non-vector

Created on 2021-10-14 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.4 (2021-02-15)
#>  os       Pop!_OS 21.04               
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_US:en                    
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Toronto             
#>  date     2021-10-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package        * version    date       lib
#>  assertthat       0.2.1      2019-03-21 [1]
#>  backports        1.2.1      2020-12-09 [1]
#>  base64enc        0.1-3      2015-07-28 [1]
#>  blob             1.2.1      2020-01-20 [1]
#>  cachem           1.0.6      2021-08-19 [1]
#>  callr            3.7.0      2021-04-20 [1]
#>  casebase       * 0.9.1.9999 2021-10-13 [1]
#>  checkmate        2.0.0      2020-02-06 [1]
#>  cli              3.0.1      2021-07-17 [1]
#>  cluster          2.1.1      2021-02-14 [4]
#>  cmprsk           2.2-10     2020-06-09 [1]
#>  codetools        0.2-18     2020-11-04 [4]
#>  colorspace       2.0-2      2021-06-24 [1]
#>  conquer          1.0.2      2020-08-27 [1]
#>  crayon           1.4.1      2021-02-08 [1]
#>  data.table       1.14.2     2021-09-27 [1]
#>  DBI              1.1.1      2021-01-15 [1]
#>  desc             1.4.0      2021-09-28 [1]
#>  devtools         2.4.2      2021-06-07 [1]
#>  digest           0.6.28     2021-09-23 [1]
#>  dplyr            1.0.7      2021-06-18 [1]
#>  ellipsis         0.3.2      2021-04-29 [1]
#>  evaluate         0.14       2019-05-28 [1]
#>  fansi            0.5.0      2021-05-25 [1]
#>  fastmap          1.1.0      2021-01-25 [1]
#>  foreach          1.5.1      2020-10-15 [1]
#>  foreign          0.8-81     2020-12-22 [4]
#>  Formula          1.2-3      2018-05-03 [1]
#>  fs               1.5.0      2020-07-31 [1]
#>  generics         0.1.0      2020-10-31 [1]
#>  ggplot2          3.3.5      2021-06-25 [1]
#>  glue             1.4.2      2020-08-27 [1]
#>  gridExtra        2.3        2017-09-09 [1]
#>  gtable           0.3.0      2019-03-25 [1]
#>  highr            0.9        2021-04-16 [1]
#>  Hmisc            4.4-1      2020-08-10 [1]
#>  htmlTable        2.1.0      2020-09-16 [1]
#>  htmltools        0.5.2      2021-08-25 [1]
#>  htmlwidgets      1.5.3      2020-12-10 [1]
#>  iterators        1.0.13     2020-10-15 [1]
#>  jpeg             0.1-8.1    2019-10-24 [1]
#>  knitr            1.36       2021-09-29 [1]
#>  lattice          0.20-41    2020-04-02 [1]
#>  latticeExtra     0.6-29     2019-12-19 [1]
#>  lava             1.6.7      2020-03-05 [1]
#>  lifecycle        1.0.1      2021-09-24 [1]
#>  magrittr         2.0.1      2020-11-17 [1]
#>  MASS             7.3-53.1   2021-02-12 [4]
#>  Matrix           1.3-2      2021-01-06 [4]
#>  MatrixModels     0.4-1      2015-08-22 [1]
#>  matrixStats      0.56.0     2020-03-13 [1]
#>  memoise          2.0.0      2021-01-26 [1]
#>  mets             1.2.9      2021-09-06 [1]
#>  mgcv             1.8-34     2021-02-16 [4]
#>  multcomp         1.4-13     2020-04-08 [1]
#>  munsell          0.5.0      2018-06-12 [1]
#>  mvtnorm          1.1-1      2020-06-09 [1]
#>  nlme             3.1-152    2021-02-04 [4]
#>  nnet             7.3-15     2021-01-24 [4]
#>  numDeriv         2016.8-1.1 2019-06-06 [1]
#>  pillar           1.6.3      2021-09-26 [1]
#>  pkgbuild         1.2.0      2020-12-15 [1]
#>  pkgconfig        2.0.3      2019-09-22 [1]
#>  pkgload          1.2.2      2021-09-11 [1]
#>  png              0.1-7      2013-12-03 [1]
#>  polspline        1.1.19     2020-05-15 [1]
#>  prettyunits      1.1.1      2020-01-24 [1]
#>  processx         3.5.2      2021-04-30 [1]
#>  prodlim          2019.11.13 2019-11-17 [1]
#>  ps               1.6.0      2021-02-28 [1]
#>  purrr            0.3.4      2020-04-17 [1]
#>  quantreg         5.67       2020-09-09 [1]
#>  R6               2.5.1      2021-08-19 [1]
#>  RColorBrewer     1.1-2      2014-12-07 [1]
#>  Rcpp             1.0.7      2021-07-07 [1]
#>  remotes          2.4.0      2021-06-02 [1]
#>  riskRegression * 2021.10.10 2021-10-11 [1]
#>  rlang            0.4.11     2021-04-30 [1]
#>  rmarkdown        2.11.3     2021-10-13 [1]
#>  rms              6.0-1      2020-07-18 [1]
#>  rpart            4.1-15     2019-04-12 [4]
#>  rprojroot        2.0.2      2020-11-15 [1]
#>  rstudioapi       0.13       2020-11-12 [1]
#>  sandwich         2.5-1      2019-04-06 [1]
#>  scales           1.1.1      2020-05-11 [1]
#>  sessioninfo      1.1.1      2018-11-05 [1]
#>  SparseM          1.78       2019-12-13 [1]
#>  stringi          1.7.5      2021-10-04 [1]
#>  stringr          1.4.0      2019-02-10 [1]
#>  survival       * 3.2-13     2021-08-24 [1]
#>  testthat         3.1.0      2021-10-04 [1]
#>  TH.data          1.0-10     2019-01-21 [1]
#>  tibble           3.1.5      2021-09-30 [1]
#>  tidyselect       1.1.1      2021-04-30 [1]
#>  timereg          1.9.7      2020-08-24 [1]
#>  usethis          2.0.1      2021-02-10 [1]
#>  utf8             1.2.2      2021-07-24 [1]
#>  vctrs            0.3.8      2021-04-29 [1]
#>  VGAM             1.1-5      2021-01-14 [1]
#>  visreg           2.7.0      2020-06-04 [1]
#>  withr            2.4.2      2021-04-18 [1]
#>  xfun             0.26       2021-09-14 [1]
#>  yaml             2.2.1      2020-02-01 [1]
#>  zoo              1.8-8      2020-05-02 [1]
#>  source                                  
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  Github (sahirbhatnagar/casebase@2342d19)
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.3)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.3)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.3)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.3)                          
#>  CRAN (R 4.0.3)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  Github (rstudio/rmarkdown@ebf0d09)      
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.0)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#> 
#> [1] /home/sahir/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

This works if I don't load riskRegression:

library(survival)
library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/
# library(riskRegression)

cfit <- fitSmoothHazard(death ~ futime + sex*pspline(age, df=4), data=mgus, time = "futime")
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
plot(cfit, 
     hazard.params = list(xvar = "age",
                          by = "sex",
                          alpha = 1))
#> Conditions used in construction of plot
#> sex: female / male
#> futime: 3329.963
#> offset: 0

Created on 2021-10-14 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.4 (2021-02-15)
#>  os       Pop!_OS 21.04               
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_US:en                    
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Toronto             
#>  date     2021-10-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib
#>  assertthat    0.2.1      2019-03-21 [1]
#>  blob          1.2.1      2020-01-20 [1]
#>  cachem        1.0.6      2021-08-19 [1]
#>  callr         3.7.0      2021-04-20 [1]
#>  casebase    * 0.9.1.9999 2021-10-13 [1]
#>  cli           3.0.1      2021-07-17 [1]
#>  colorspace    2.0-2      2021-06-24 [1]
#>  crayon        1.4.1      2021-02-08 [1]
#>  curl          4.3.2      2021-06-23 [1]
#>  data.table    1.14.2     2021-09-27 [1]
#>  DBI           1.1.1      2021-01-15 [1]
#>  desc          1.4.0      2021-09-28 [1]
#>  devtools      2.4.2      2021-06-07 [1]
#>  digest        0.6.28     2021-09-23 [1]
#>  dplyr         1.0.7      2021-06-18 [1]
#>  ellipsis      0.3.2      2021-04-29 [1]
#>  evaluate      0.14       2019-05-28 [1]
#>  fansi         0.5.0      2021-05-25 [1]
#>  fastmap       1.1.0      2021-01-25 [1]
#>  fs            1.5.0      2020-07-31 [1]
#>  generics      0.1.0      2020-10-31 [1]
#>  ggplot2       3.3.5      2021-06-25 [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.2      2021-08-25 [1]
#>  httr          1.4.2      2020-07-20 [1]
#>  knitr         1.36       2021-09-29 [1]
#>  lattice       0.20-41    2020-04-02 [1]
#>  lifecycle     1.0.1      2021-09-24 [1]
#>  magrittr      2.0.1      2020-11-17 [1]
#>  Matrix        1.3-2      2021-01-06 [4]
#>  memoise       2.0.0      2021-01-26 [1]
#>  mgcv          1.8-34     2021-02-16 [4]
#>  mime          0.11       2021-06-23 [1]
#>  munsell       0.5.0      2018-06-12 [1]
#>  nlme          3.1-152    2021-02-04 [4]
#>  pillar        1.6.3      2021-09-26 [1]
#>  pkgbuild      1.2.0      2020-12-15 [1]
#>  pkgconfig     2.0.3      2019-09-22 [1]
#>  pkgload       1.2.2      2021-09-11 [1]
#>  prettyunits   1.1.1      2020-01-24 [1]
#>  processx      3.5.2      2021-04-30 [1]
#>  ps            1.6.0      2021-02-28 [1]
#>  purrr         0.3.4      2020-04-17 [1]
#>  R6            2.5.1      2021-08-19 [1]
#>  remotes       2.4.0      2021-06-02 [1]
#>  rlang         0.4.11     2021-04-30 [1]
#>  rmarkdown     2.11.3     2021-10-13 [1]
#>  rprojroot     2.0.2      2020-11-15 [1]
#>  rstudioapi    0.13       2020-11-12 [1]
#>  scales        1.1.1      2020-05-11 [1]
#>  sessioninfo   1.1.1      2018-11-05 [1]
#>  stringi       1.7.5      2021-10-04 [1]
#>  stringr       1.4.0      2019-02-10 [1]
#>  survival    * 3.2-13     2021-08-24 [1]
#>  testthat      3.1.0      2021-10-04 [1]
#>  tibble        3.1.5      2021-09-30 [1]
#>  tidyselect    1.1.1      2021-04-30 [1]
#>  usethis       2.0.1      2021-02-10 [1]
#>  utf8          1.2.2      2021-07-24 [1]
#>  vctrs         0.3.8      2021-04-29 [1]
#>  VGAM          1.1-5      2021-01-14 [1]
#>  visreg        2.7.0      2020-06-04 [1]
#>  withr         2.4.2      2021-04-18 [1]
#>  xfun          0.26       2021-09-14 [1]
#>  xml2          1.3.2      2020-04-23 [1]
#>  yaml          2.2.1      2020-02-01 [1]
#>  source                                  
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  Github (sahirbhatnagar/casebase@2342d19)
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.3)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  Github (rstudio/rmarkdown@ebf0d09)      
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.4)                          
#>  CRAN (R 4.0.2)                          
#>  CRAN (R 4.0.2)                          
#> 
#> [1] /home/sahir/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
@turgeonmaxime
Copy link
Collaborator

turgeonmaxime commented Oct 14, 2021

Here is what I was able to find. First, if I use the debugger and look at the content of Data, I get this:

Browse[1]> Data
function (n, outcome = "competing.risks", formula = ~f(X1, 2) + 
    f(X2, -0.033) + f(X3, 0.4) + f(X6, 0.1) + f(X7, -0.1) + f(X8, 
    0.5) + f(X9, -1)) 
{
    # Some code that's irrelevant
}
<bytecode: 0x7f84a6322f10>
<environment: namespace:riskRegression>

As we can see at the end, the argument Data, used in visreg, now points to a function in the package riskRegression. So we get an error when visreg:::setupF tries to change the names of a data frame that is now in fact a function; see here:

https://github.com/pbreheny/visreg/blob/13254c9553960c3885f2c12c7a7f7332bc582fb2/R/setupF.R#L37

A little bit more sleuthing reveals that Data actually maps to riskRegression::sampleData, which is a function creating sample data according to some parameters:

Browse[1]> identical(Data, riskRegression::sampleData)
[1] TRUE

One key observation is that sampleData is also the name we use for the dataset after case-base sampling. It's also the dataset that appears in the original call to glm:

> cfit$call
fitSmoothHazard(formula = death ~ futime + sex * pspline(age, 
    df = 4), data = mgus, time = "futime")
> cfit$lower_call
glm(formula = formula, family = binomial, data = sampleData)

Given all this, and given how visreg:::setupF finds the dataset it needs, my best guess is that at some point visreg:::setupF reads cfit$lower_call instead of cfit$call, and therefore looks for an object called sampleData in the environment and finds the function from riskRegression.

The only problem with this guess is that it doesn't explain why the code works when riskRegression isn't loaded. It seems to me that, following that logic, we would get an error that sampleData doesn't exist.

@turgeonmaxime
Copy link
Collaborator

turgeonmaxime commented Oct 14, 2021

As a proof of concept, you can see that if we define an object sampleData in the global environment, visreg:::setupF finds it:

library(survival)
library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/

sampleData <- 10
class(sampleData) <- "foo"
names.foo <- function(x) message("I feel triggered")

cfit <- fitSmoothHazard(death ~ futime + sex*pspline(age, df=4), data=mgus, time = "futime")
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
plot(cfit, 
     hazard.params = list(xvar = "age",
                          by = "sex",
                          alpha = 1))
#> I feel triggered
#> Error in as.data.frame.default(data): cannot coerce class '"foo"' to a data.frame

Created on 2021-10-14 by the reprex package (v2.0.0)

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

No branches or pull requests

2 participants