Skip to contents

Assigns the population to k stratified folds. See the Details section below for further information.

Usage

stratifiedKFolds(strata = NULL, k)

Arguments

strata

vector of stratification variables. The population size is length(strata)

k

number of folds

Value

A vector of length length(strata) containing the fold ids.

Details

Each element in the population is assigned to one of the k folds so that the percentage of each stratum in the population is preserved in each fold.

Author

Alessandro Barberis

Examples

#Set seed for reproducibility
set.seed(seed = 5381L)

#Define strata
strata = c(1,1,1,2,2,2,2,2,2)

#Check ratio
table(strata)/length(strata)
#> strata
#>         1         2 
#> 0.3333333 0.6666667 

#Assign data to 3 folds
i = stratifiedKFolds(
 strata = strata,
 k = 3
)
#Check folds
i
#> [1] 2 1 3 3 2 1 2 3 1
#Check ratio in the folds
table(strata[i==1])/length(strata[i==1])
#> 
#>         1         2 
#> 0.3333333 0.6666667 
table(strata[i==2])/length(strata[i==2])
#> 
#>         1         2 
#> 0.3333333 0.6666667 
table(strata[i==3])/length(strata[i==3])
#> 
#>         1         2 
#> 0.3333333 0.6666667