Skip to contents

Assigns observations to v cross-validation folds. When strata is supplied, folds are allocated within each stratum so that binary outcomes or important groups are spread more evenly across folds.

Usage

ci_cv_folds(n, v = 5L, strata = NULL, seed = NULL)

Arguments

n

Number of observations.

v

Number of folds.

strata

Optional vector of stratum labels with length n. Missing stratum labels are treated as their own stratum rather than being left unassigned.

seed

Optional random seed for reproducible fold allocation.

Value

An integer vector of fold ids from 1 to v. Every row receives a fold id, and each fold is used at least once whenever n >= v.

Details

Rows are shuffled by position inside each stratum, so single-row strata are never resampled as integers. Fold labels rotate continuously across strata instead of restarting inside each one, which keeps small strata from over-filling the first folds.

Examples

ci_cv_folds(10, v = 3, seed = 1)
#>  [1] 1 2 1 2 3 3 3 1 1 2