Learn R Programming

CORElearn (version 1.51.2)

cvGen: Cross-validation and stratified cross-validation

Description

Generate indices for cross-validation and stratified cross-validation

Usage

cvGen(n, k) 
cvGenStratified(classVal,k) 
gatherFromList(lst)

Arguments

n

The number of instances in a data set.

k

The number of folds in cross-validation.

classVal

A vector of factors representing class values.

lst

A list of lists from which we collect results of the same components.

Value

The functions cvGen and cvGenStratified return a vector of indices indicating fold membership i.e. from 1:k. The function codegatherFromList returns a list with components containing elements of the same name.

Details

The functions cvGen and cvGenStratified generate indices of instances from a data set which can be used in cross-validation. The function cvGenStratified generates the same distribution of class values in each fold. The function gatherFromList is an auxiliary function helping in collection of results, see the example below.

See Also

CORElearn.

Examples

Run this code
# NOT RUN {
data <- iris
folds <- 10
foldIdx <- cvGen(nrow(data), k=folds)
evalCore<-list()
for (j in 1:folds) {
	dTrain <- data[foldIdx!=j,]
	dTest  <- data[foldIdx==j,]
	modelCore <- CoreModel(Species~., dTrain, model="rf") 
	predCore <- predict(modelCore, dTest)
	evalCore[[j]] <- modelEval(modelCore, correctClass=dTest$Species,
	          predictedClass=predCore$class, predictedProb=predCore$prob ) 
	destroyModels(modelCore)
}
results <- gatherFromList(evalCore)
sapply(results, mean)
# }

Run the code above in your browser using DataLab