Assigns whole clusters to cross-validation folds so that no cluster is
split across folds: every observation of a cluster receives the same fold
id, and therefore no training set ever shares a cluster with the fold held
out against it. Folds are built by a deterministic size-balanced greedy
rule: clusters are taken largest first (ties by first appearance in
cluster.id) and each is placed into the currently smallest fold
(ties by lowest fold index). With v equal to the number of
clusters this reduces to leave-cluster-out.
Arguments
- cluster.id
Vector of cluster labels (factor, character, or integer-like), one per observation, no missing values.
- v
Number of folds; an integer between 2 and the number of distinct clusters.
- shuffle.seed
Optional integer seed for a randomized cluster order;
NULL(default) keeps the deterministic order.
Value
An integer fold-id vector of length length(cluster.id) with
values in 1:v; every fold is nonempty and every cluster maps to
exactly one fold.
Details
The assignment is fully deterministic when shuffle.seed is
NULL. Supplying shuffle.seed applies one seeded permutation
to the cluster order before the greedy pass (the seed is consumed
immediately via set.seed), giving a reproducible randomized
variant; callers following the LPS evidence conventions should record the
seed they pass.
See also
lps.nested.cv() for nested cross-validation that can consume
grouped folds at both the outer and inner level.
Examples
cluster <- rep(letters[1:6], each = 2)
lps.grouped.foldid(cluster, v = 3L)
#> [1] 1 1 2 2 3 3 1 1 2 2 3 3