Error building partial dependence plots for RF using FinalModel output from caret's train() function

795 views Asked by At

I am using the following code to fit and test a random forest classification model:

> control <- trainControl(method='repeatedcv', 
+                         number=5,repeats = 3, 
+                         search='grid') 
> tunegrid <- expand.grid(.mtry = (1:12)) 
> rf_gridsearch <- train(y = river$stat_bino,
+                        x = river[,colnames(river) != "stat_bino"], 
+                              data = river,
+                              method = 'rf',
+                              metric = 'Accuracy',
+                              ntree = 600,
+                              importance = TRUE,
+                              tuneGrid = tunegrid, trControl = control)  

Note, I am using

train(y = river$stat_bino, x = river[,colnames(river) != "stat_bino"],...
rather than: train(stat_bino ~ .,... 

so that my categorical variables will not be turned into dummy variables. solution here: variable encoding in K-fold validation of random forest using package 'caret')

I would like to extract the FinalModel and use it to make partial dependence plots for my variables (using code below), but I get an error message and don't know how to fix it.

> model1 <- rf_gridsearch$finalModel
> library(pdp)
> partial(model1, pred.var = "MAXCL", type = "classification", which.class = "1", plot =TRUE)
Error in eval(stats::getCall(object)$data) : 
  ..1 used in an incorrect context, no ... to look in

Thanks for any solutions here!

0

There are 0 answers