Cross Validation in R using cvTools

library(cvTools)
library(doMC)

registerDoMC(cores=8)

# 10 fold CV
folds <- cvFolds(NROW(iris), K=10)
foreach(i=1:10) %dopar% {
  train <- iris[folds$subsets[folds$which != i], ]
  validation <- iris[folds$subsets[folds$which == i], ]
  # Write modeling and evaluation code here.
  # In this example, I'm just returning training and validation data sets 
  # for illustrative purpose.
  return(list(train=train, validation=validation))
}

Output:

...
[[10]]
[[10]]$train
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
105          6.5         3.0          5.8         2.2  virginica
29           5.2         3.4          1.4         0.2     setosa
18           5.1         3.5          1.4         0.3     setosa
149          6.2         3.4          5.4         2.3  virginica
2            4.9         3.0          1.4         0.2     setosa
43           4.4         3.2          1.3         0.2     setosa
114          5.7         2.5          5.0         2.0  virginica
104          6.3         2.9          5.6         1.8  virginica
...
[[10]]$validation
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
11           5.4         3.7          1.5         0.2     setosa
44           5.0         3.5          1.6         0.6     setosa
67           5.6         3.0          4.5         1.5 versicolor
...

Similar Posts:

Post a Comment

Your email is never published nor shared. Required fields are marked *