Hi I know someone asked similar issues before but no clear answer yet (or I tried their solution without success: Caret error using GBM, but not without caret Caret train method
Instead of passing the formula in the train function, pass values for parameters x, y, method etc
the old way:
modFit = train(data.df$Label ~ .,
data = data.df,
method = "rpart",
trControl= cntr,
tuneLength = 7)
new way:
modFit = train(x = data.df.cols,
y = data.df$Label,
method = "rpart",
trControl = cntrl,
tuneLength = 7)
Note: x = data.df.cols has all columns except the label, data.df.cols = data.df[,2:ncol(data.df)]
You need to convert the newly created Class_new
to a factor, as follows:
Sonar$Class_new<-ifelse(Sonar$Class=="R","R",ifelse(Sonar$rand>0,"M","H"))
Sonar$Class_new <- factor(Sonar$Class_new)
Also, you may want to remove the variables Class
and rand
from your training and testing data sets. You can do somthing like:
training <- Sonar[ inTraining, !(names(Sonar) %in% c("Class", "rand"))]
testing <- Sonar[-inTraining, !(names(Sonar) %in% c("Class", "rand"))]
I had allowParallel = TRUE in the train function and the machine I was working on did not have multiple cores. After I commented that statement, I did not get the error.
Thank howaj for your post. That did work for the data I posted but somehow did not work for another dataset, where everything seems to be the same. But I figured out finally:
Could be a syntax issue here. Instead of using train(y~., data=training, ...), I changed to the train(train$y,train$x, ...) without specifying data=.. explicitly:
train(training[,!names(training)%in%response], training$response ...)
This worked.