Skip to content

Instantly share code, notes, and snippets.

@duttashi
Created July 11, 2017 09:56
Show Gist options
  • Save duttashi/a51c71acb7388c535e30b57854598e77 to your computer and use it in GitHub Desktop.
Save duttashi/a51c71acb7388c535e30b57854598e77 to your computer and use it in GitHub Desktop.
A simple function to perform k-fold cross validation in R
#Randomly shuffle the data
yourdata<-yourdata[sample(nrow(yourdata)),]
#Create 10 equally size folds
folds <- cut(seq(1,nrow(yourdata)),breaks=10,labels=FALSE)
#Perform 10 fold cross validation
for(i in 1:10){
#Segement your data by fold using the which() function
testIndexes <- which(folds==i,arr.ind=TRUE)
testData <- yourdata[testIndexes, ]
trainData <- yourdata[-testIndexes, ]
#Use the test and train data partitions however you desire...
}
# This solution is adapted as it is from https://stats.stackexchange.com/questions/61090/how-to-split-a-data-set-to-do-10-fold-cross-validation
@sasifraza
Copy link

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment