topepo / caret

caret (Classification And Regression Training) R package that contains misc functions for training and plotting classification and regression models
http://topepo.github.io/caret/index.html
1.62k stars 632 forks source link

Failed to load a saved model #1013

Closed aswansyahputra closed 5 years ago

aswansyahputra commented 5 years ago

Hi!

I have this error message when loading a saved model (RJip model from RWeka package implemented via caret):

Error in .jcall(o, "Ljava/lang/Class;", "getClass"): RcallMethod: attempt to call a method of a NULL object.

Similar questions and answer are mentioned here and here. However, how can I address this issue by still using caret::train() instead of RWeka::JRip()?

Regards,

Aswan

Minimal reproducible example

library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2

my_model <- train(Species ~ . , data = iris, method = "JRip")
predict(my_model, iris[6,])
#> [1] setosa
#> Levels: setosa versicolor virginica

save(my_model, file = "my_model.rda")
load("my_model.rda")
predict(my_model, iris[6,])
#> Error in .jcall(o, "Ljava/lang/Class;", "getClass"): RcallMethod: attempt to call a method of a NULL object.

Created on 2019-03-04 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 Ubuntu 18.04.2 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_US:en #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Asia/Jakarta #> date 2019-03-04 #> #> ─ 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.1) #> callr 3.1.1 2018-12-21 [1] CRAN (R 3.5.1) #> caret * 6.0-81 2018-11-20 [3] CRAN (R 3.5.1) #> class 7.3-15 2019-01-01 [4] CRAN (R 3.5.2) #> cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.1) #> codetools 0.2-16 2018-12-24 [4] CRAN (R 3.5.2) #> colorspace 1.4-0 2019-01-13 [1] CRAN (R 3.5.2) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.1) #> data.table 1.12.0 2019-01-13 [1] CRAN (R 3.5.2) #> 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) #> dplyr 0.8.0.1 2019-02-15 [1] CRAN (R 3.5.2) #> e1071 1.7-0.1 2019-01-21 [3] CRAN (R 3.5.2) #> evaluate 0.13 2019-02-12 [1] CRAN (R 3.5.2) #> foreach 1.4.4 2017-12-12 [1] CRAN (R 3.5.2) #> fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.1) #> generics 0.0.2 2018-11-29 [1] CRAN (R 3.5.1) #> ggplot2 * 3.1.0 2018-10-25 [1] CRAN (R 3.5.1) #> glue 1.3.0.9000 2019-02-01 [1] Github (tidyverse/glue@8188cea) #> gower 0.1.2 2017-02-23 [3] CRAN (R 3.5.0) #> gtable 0.2.0 2016-02-26 [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) #> ipred 0.9-8 2018-11-05 [3] CRAN (R 3.5.1) #> iterators 1.0.10 2018-07-13 [1] CRAN (R 3.5.2) #> knitr 1.21 2018-12-10 [1] CRAN (R 3.5.1) #> lattice * 0.20-38 2018-11-04 [4] CRAN (R 3.5.1) #> lava 1.6.3 2018-08-10 [3] CRAN (R 3.5.1) #> lazyeval 0.2.1 2017-10-29 [1] CRAN (R 3.5.1) #> lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.5.1) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.1) #> MASS 7.3-51.1 2018-11-01 [4] CRAN (R 3.5.1) #> Matrix 1.2-15 2018-11-01 [4] CRAN (R 3.5.1) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.1) #> ModelMetrics 1.2.2 2018-11-03 [3] CRAN (R 3.5.1) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.5.1) #> nlme 3.1-137 2018-04-07 [4] CRAN (R 3.5.0) #> nnet 7.3-12 2016-02-02 [4] CRAN (R 3.5.0) #> pillar 1.3.1 2018-12-15 [1] CRAN (R 3.5.1) #> pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.1) #> pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.5.1) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.1) #> plyr 1.8.4 2016-06-08 [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.1) #> prodlim 2018.04.18 2018-04-18 [3] CRAN (R 3.5.0) #> ps 1.3.0 2018-12-21 [1] CRAN (R 3.5.1) #> purrr 0.3.0 2019-01-27 [1] CRAN (R 3.5.2) #> R6 2.4.0 2019-02-14 [1] CRAN (R 3.5.2) #> Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.1) #> recipes 0.1.4 2018-11-19 [3] CRAN (R 3.5.1) #> remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.2) #> reshape2 1.4.3 2017-12-11 [1] CRAN (R 3.5.1) #> rJava 0.9-10 2018-05-29 [3] CRAN (R 3.5.0) #> rlang 0.3.1 2019-01-08 [3] CRAN (R 3.5.2) #> rmarkdown 1.11 2018-12-08 [1] CRAN (R 3.5.1) #> rpart 4.1-13 2018-02-23 [4] CRAN (R 3.5.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.1) #> RWeka 0.4-40 2019-02-26 [1] CRAN (R 3.5.2) #> RWekajars 3.9.3-1 2018-09-10 [1] CRAN (R 3.5.1) #> scales 1.0.0 2018-08-09 [1] CRAN (R 3.5.1) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.1) #> stringi 1.3.1 2019-02-13 [1] CRAN (R 3.5.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.5.2) #> survival 2.43-3 2018-11-26 [4] CRAN (R 3.5.1) #> testthat 2.0.1 2018-10-13 [1] CRAN (R 3.5.1) #> tibble 2.0.1 2019-01-12 [1] CRAN (R 3.5.2) #> tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.5.1) #> timeDate 3043.102 2018-02-21 [1] CRAN (R 3.5.2) #> usethis 1.4.0 2018-08-14 [1] CRAN (R 3.5.2) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.1) #> xfun 0.5 2019-02-20 [1] CRAN (R 3.5.2) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.5.1) #> #> [1] /home/aswansyahputra/R/x86_64-pc-linux-gnu-library/3.5 #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library ```
topepo commented 5 years ago

See the RWeka vignette:

From the R side, the generated classifier is a reference to an external Java object. As such objects do not persist across sessions, they will be restored as ‘null’ references. Fortunately, rJava has added a .jcache() mechanism providing an R-side cache of such objects in serialized form, which is attached to the object and hence saved when the Java object is saved, and can be restored via rJava mechanisms for unserializing Java references if they are ‘null’ references and have a cache attached. One most be cautious when creating such persistent references, though; see ?.jcache for more information.

As those SO links indicated, you would run .jcache() on my_model$finalModel before saving.