title: Stratified k-fold Cross Validation in R
body: <p>Suppose I have a multiclass dataset (iris for example). I want to perform a stratified 10 fold CV to test model performance. I found a function in the package <code>splitstackchange</code> called <code>stratified</code> that gives me a stratified fold based on the proportion of the data I want. So if I want a testing fold it would be 0.1 of the data rows.</p>
  
  <pre><code>#One Fold
  library(splitstackchange)
  stratified(iris,c("Species"),0.1)
  </code></pre>
  
  <p>I want to know how to implement this function or any other form of stratified cv in a 10-fold loop. I couldn't crack the logic behind it. Here I include a reproducible example.</p>
  
  <pre><code>    library(splitstackshape)
      data=iris
      names(data)[ncol(data)]=c("Y")
      nFolds=10
  
      for (i in 1:nFolds){
        testing=stratified(data,c("Y"),0.1,keep.rownames=TRUE)
        rn=testing$rn
        testing=testing[,-"rn"]
        row.names(testing)=rn
        trainingRows=setdiff(1:nrow(data),as.numeric(row.names(testing)))
        training=data[trainingRows,]
        names(training)[ncol(training)]="Y"
      }
  </code></pre>