Yuvaraj Yuvaraj - 28 days ago 22
R Question

How to tune multiple parameters using Caret package?

I am building a CART model and I am trying to tune 2 parameters of rpart - CP and Maxdepth. While the Caret package is working well for one parameter at a time, when both are used it keeps throwing an error and i am not able to figure out why

library(caret)
data(iris)
tc <- trainControl("cv",10)
rpart.grid <- expand.grid(cp=seq(0,0.1,0.01), minsplit=c(10,20))
train(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, data=iris, method="rpart",
trControl=tc, tuneGrid=rpart.grid)


I am getting the following error:

Error in train.default(x, y, weights = w, ...) :
The tuning parameter grid should have columns cp

Answer

Method "rpart" is only capable of tuning the cp, method "rpart2" is used for maxdepth. There is no tuning for minsplit or any of the other rpart controls. If you want to tune on different options you can write a custom model to take this into account.

Click here for more info on how to do this. Also read this answer about how to use the rpart control within the train function.

Comments