| split_cv | R Documentation |
split_cv creates (optionally repeated and stratified) v-fold
cross-validation splits for every dataset in a list. It is implemented
with base R and data.table only and returns, per dataset, a
data.table of fold identifiers, row indices and (optionally) the
training / validation subsets.
split_cv(
data,
v = 10L,
repeats = 1L,
strata = NULL,
breaks = 4L,
pool = 0.1,
seed = NULL,
materialize = TRUE,
out_type = "dt"
)
data |
A |
v |
Number of folds. A single integer >= 2. Default |
repeats |
Number of repeats. A single integer >= 1. Default |
strata |
|
breaks |
Number of quantile bins used when |
pool |
Strata holding less than this proportion of the rows are
merged with the next-smallest stratum. A number in |
seed |
|
materialize |
Logical. If |
out_type |
Class of the |
Fold sizes differ by at most one row. With stratification, rows are
shuffled within strata, the strata are laid out one after another and fold
labels are dealt out cyclically, so each stratum is spread evenly across
folds. The stratification rules (breaks, pool) follow the same idea
as rsample::vfold_cv(), but the random fold assignments are not
identical to rsample's.
Training and validation subsets can always be rebuilt from the indices:
data[[1]][res[[1]]$train_idx[[k]], ].
For mixed-model cross-validation (e.g. genomic prediction with
'ASReml-R') a common alternative to subsetting is masking: keep all
records, set the phenotypes of validate_idx to NA, fit the model on
the full data and correlate the predicted breeding values of the masked
individuals with their observations. materialize = FALSE is enough for
this, since only the indices are needed.
A list of data.table objects (one per input dataset, names of
data preserved), each with one row per fold and the columns:
id — fold label (Fold1, ...), or repeat label
(Repeat1, ...) when repeats > 1.
id2 — fold label, only when repeats > 1.
train_idx — list-column of training row indices.
validate_idx — list-column of validation row indices.
train, validate — list-columns of subsets, data.table or
data.frame according to out_type (only when
materialize = TRUE).
nest_cv() for the nested data.table variant.
# Prepare example data: Convert first 3 columns of iris dataset to long format and split
dt_split <- w2l_split(data = iris, cols = 1:3)
# dt_split is now a list containing 3 data tables for Sepal.Length, Sepal.Width, and Petal.Length
# Example 1: Single cross-validation (no repeats)
split_cv(
data = dt_split, # Input list of split data
v = 3, # Set 3-fold cross-validation
repeats = 1, # Perform cross-validation once (no repeats)
seed = 123 # Reproducible folds
)
# Returns a list where each element contains:
# - id: fold labels (Fold1, Fold2, Fold3)
# - train_idx / validate_idx: row indices of each fold
# - train / validate: training and validation subsets
# Example 2: Repeated cross-validation
split_cv(
data = dt_split, # Input list of split data
v = 3, # Set 3-fold cross-validation
repeats = 2, # Perform cross-validation twice
seed = 123
)
# Returns a list where each element contains:
# - id: repeat labels (Repeat1, Repeat2)
# - id2: fold labels (Fold1, Fold2, Fold3)
# - train_idx / validate_idx, train / validate
# Example 3: Stratified CV, indices only (memory friendly)
res <- split_cv(dt_split, v = 5, strata = "Species", seed = 1,
materialize = FALSE)
# Rebuild the training set of fold 1 of the first dataset when needed
head(dt_split[[1]][res[[1]]$train_idx[[1]], ])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.