cvGen: Cross-validation and stratified cross-validation

View source: R/util.R

cvGenR Documentation

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.

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.

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.

Author(s)

Marko Robnik-Sikonja

See Also

CORElearn.

Examples

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)

CORElearn documentation built on Nov. 18, 2022, 5:08 p.m.