Created
July 11, 2017 09:56
-
-
Save duttashi/a51c71acb7388c535e30b57854598e77 to your computer and use it in GitHub Desktop.
A simple function to perform k-fold cross validation in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much!