I am facing two problems while using
caret package in R. I am reproducing an example below:
library(mlbench) library(caret) set.seed(998) data(Sonar) #Random data, just for illustration purpose Sonar= Sonar[, 1:6] #Selected first 6 columsn only for showing an example. I am assuming V6 to be response. head(Sonar) inTraining <- createDataPartition(Sonar$V6, p = 0.75, list = FALSE) training <- Sonar[inTraining, ] testing <- Sonar[-inTraining, ] modelFit <- train( V6~.,data=training, method="rpart" ) varImp(modelFit)
a. How to extract top three (3) variables from
varImp output? I tried to order the variables but for any reason, its not working for me.
b. Also, why the following code doesn't work for "randomForest"?
modelFit <- train( V6~.,data=training, method="rf" ) varImp(modelFit) > varImp(modelFit) Rerun with Debug Error in varImp[, "%IncMSE"] : subscript out of bounds