Windstorm1981
Windstorm1981

Reputation: 2680

Caret Package: Stratified Cross Validation in Train Function

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.

Upvotes: 13

Views: 10075

Answers (1)

KST
KST

Reputation: 637

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)

Upvotes: 18

Related Questions