Save/load a M5 RWeka caret model fails

2019-05-20 22:00发布

问题:

I'm coming up with an error after loading a saved M5 implementation of the RWeka package via Caret.

Error in .jcall(o, "Ljava/lang/Class;", "getClass") :

RcallMethod: attempt to call a method of a NULL object.

To reproduce the error:

library(caret); library(RWeka)
data(GermanCredit)

myModel <- train(Duration~Amount, data=GermanCredit, method="M5")
predict(myModel, GermanCredit[1,]) # Works.

save(myModel, file="myModel.rda")
load("myModel.rda")
predict(myModel, GermanCredit[1,]) # Produces the RcallMethod error.

This post indicates that RWeka objects are references to a Java object, and that object must be serialized in order to save/load properly.

However, I cannot find the right commands to extend this to the Caret package implementation.

回答1:

http://one-line-it.blogspot.tw/2013/03/r-store-rweka-model-to-file.html

library(RWeka)
j48.model <- J48(formula=class ~ ., data=data)
library(rJava)
.jcache(j48.model$classifier)
save(j48.model, file="j48.model.rda")

This may help you.

btw Are there methods that can make a J48 Rweka classifier into rules?



标签: r r-caret rweka