I would like to export a Caret random forest model using the pmml library so I can use it for predictions in Java. Here is a reproduction of the error I am getting.
You cannot invoke the pmml
method with train
or train.formula
types (ie. this is the type of your model.Test
object).
Caret documentation for the train
method says that you can access the best model as the finalModel
field. You can invoke the pmml
method on that object then.
rf = model.Test$finalModel
pmml(rf)
Unfortunately, it turns out that Caret specifies the RF model using the "matrix interface" (ie. by setting the x
and y
fields), not using the more common "formula interface" (ie. by setting the formula
field). AFAIK, the "pmml" package does not support the export of such RF models.
So, looks like your best option is to use a two-level approach. First, use the Caret package to find the most appropriate RF parametrization for your dataset. Second, train the final RF model manually using the "formula interface" with this parametrization.
You can use the r2pmml package to do the job:
library("caret")
library("r2pmml")
data(iris)
train.rf = train(Species ~ ., data = iris, method = "rf")
print(train.rf)
r2pmml(train.rf, "/tmp/train-rf.pmml")