12

Is there a way to perform stratified cross validation when using the train function to fit a model to a large imbalanced data set? I know straight forward k fold cross validation is possible but my categories are highly unbalanced. I've seen discussion about this topic but no real definitive answer.

Thanks in advance.

Windstorm1981
  • 2,564
  • 7
  • 29
  • 57
  • I'm also looking for the answer... By default, function createFolds() creates stratified folds. But I'm not sure about the train function when using method = "cv" in trainControl. – jbrettas Apr 21 '16 at 20:38

1 Answers1

17

There is a parameter called 'index' which can let user specified the index to do cross validation.

folds <- 4
cvIndex <- createFolds(factor(training$Y), folds, returnTrain = T)
tc <- trainControl(index = cvIndex,
               method = 'cv', 
               number = folds)

rfFit <- train(Y ~ ., data = training, 
            method = "rf", 
            trControl = tc,
            maximize = TRUE,
            verbose = FALSE, ntree = 1000)
KST
  • 589
  • 9
  • 15