RaphaelS1 / survivalmodels

Implementations of survival models in R
https://raphaels1.github.io/survivalmodels/
Other
57 stars 13 forks source link

About save and load mdoels #37

Closed kwkx closed 6 months ago

kwkx commented 2 years ago

I tried to save and then reload models to do prediction using below codes library(mlr3extralearners) library(mlr3pipelines) library(mlr3proba) library(survivalmodels) library(reticulate) library(survival) train <- simsurvdata(100) test <- simsurvdata(50) fit <- deepsurv(Surv(time, status) ~ ., data = train) predict(fit, newdata = test) save(fit,file=".\\model.RData") load(".\\model.RData") pred=predict(fit, newdata=test, type = "risk")

But I got the error shown below:

Error in object$model$compute_baseline_hazards() : attempt to apply non-function

The same situation with coxtime(), deephit(), loghaz() and pchazard().

kwkx commented 2 years ago

R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale: [1] LC_COLLATE=English_Finland.utf8 LC_CTYPE=English_Finland.utf8 LC_MONETARY=English_Finland.utf8 [4] LC_NUMERIC=C LC_TIME=English_Finland.utf8

attached base packages: [1] stats4 grid stats graphics grDevices utils datasets methods base

other attached packages: [1] party_1.3-10 strucchange_1.5-3 sandwich_3.0-2 zoo_1.8-10
[5] modeltools_0.2-23 mvtnorm_1.1-3 ipflasso_1.1 glmnet_4.1-4
[9] Matrix_1.4-1 randomForestSRC_3.1.1 gbm_2.1.8 survivalsvm_0.0.5
[13] keras_2.9.0 mlr3pipelines_0.4.1 mlr3extralearners_0.5.45 mlr3tuning_0.13.1
[17] paradox_0.9.0 mlr3proba_0.4.12 mlr3_0.13.4 reticulate_1.25
[21] survivalmodels_0.1.13 survminer_0.4.9 ggpubr_0.4.0 ggplot2_3.3.6
[25] survivalROC_1.0.3 survival_3.3-1 limma_3.52.2

loaded via a namespace (and not attached): [1] TH.data_1.1-1 colorspace_2.0-3 ggsignif_0.6.3 deldir_1.0-6
[5] rprojroot_2.0.3 htmlTable_2.4.1 set6_0.2.4 base64enc_0.1-3
[9] rstudioapi_0.13 distr6_1.6.9 listenv_0.8.0 fansi_1.0.3
[13] coin_1.4-2 codetools_0.2-18 splines_4.2.1 libcoin_1.0-9
[17] knitr_1.39 zeallot_0.1.0 Formula_1.2-4 jsonlite_1.8.0
[21] broom_1.0.0 km.ci_0.5-6 cluster_2.1.3 png_0.1-7
[25] tfruns_1.5.0 data.tree_1.0.0 DiagrammeR_1.0.9 compiler_4.2.1
[29] backports_1.4.1 fastmap_1.1.0 cli_3.3.0 visNetwork_2.1.0
[33] htmltools_0.5.3 tools_4.2.1 ooplah_0.2.0 gtable_0.3.0
[37] glue_1.6.2 dplyr_1.0.9 rappdirs_0.3.3 Rcpp_1.0.9
[41] carData_3.0-5 vctrs_0.4.1 iterators_1.0.14 xfun_0.31
[45] stringr_1.4.0 globals_0.15.1 lifecycle_1.0.1 rstatix_0.7.0
[49] future_1.27.0 MASS_7.3-58 scales_1.2.0 lgr_0.4.3
[53] parallel_4.2.1 RColorBrewer_1.1-3 gridExtra_2.3 KMsurv_0.1-5
[57] rpart_4.1.16 latticeExtra_0.6-30 stringi_1.7.8 tensorflow_2.9.0
[61] foreach_1.5.2 checkmate_2.1.0 dictionar6_0.1.3 palmerpenguins_0.1.0 [65] shape_1.4.6 matrixStats_0.62.0 rlang_1.0.4 pkgconfig_2.0.3
[69] lattice_0.20-45 purrr_0.3.4 htmlwidgets_1.5.4 tidyselect_1.1.2
[73] here_1.0.1 parallelly_1.32.1 magrittr_2.0.3 R6_2.5.1
[77] generics_0.1.3 Hmisc_4.7-0 multcomp_1.4-19 pillar_1.8.0
[81] whisker_0.4 param6_0.2.4 foreign_0.8-82 withr_2.5.0
[85] abind_1.4-5 nnet_7.3-17 tibble_3.1.8 crayon_1.5.1
[89] car_3.1-0 survMisc_0.5.6 uuid_1.1-0 interp_1.1-3
[93] utf8_1.2.2 jpeg_0.1-9 data.table_1.14.2 mlr3misc_0.10.0
[97] bbotk_0.5.3 digest_0.6.29 xtable_1.8-4 tidyr_1.2.0
[101] munsell_0.5.0

kwkx commented 2 years ago

Thanks for this useful package @RaphaelS1. I am not very familiar with Python, so i just save the variable directly. What should I do, if I want to save the models in this package?

RaphaelS1 commented 2 years ago

Hey have you tried using the save and load functions in reticulate? https://rstudio.github.io/reticulate/reference/py_save_object.html