| nest_cv | R Documentation |
nest_cv creates v-fold cross-validation splits for each nested data frame
of a nested data.table (e.g. the output of w2l_nest()) and returns one
row per fold, with the outer (non-nested) columns broadcast to every fold.
The cross-validation itself is performed by split_cv().
nest_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 |
A data.table with all non-nested columns of data, followed by
the columns described in split_cv() (id, id2, train_idx,
validate_idx and, if materialize = TRUE, train / validate, whose
class follows out_type).
Other nested columns are dropped (a message lists them).
split_cv()
# Example: Cross-validation for nested data.table demonstrations
# Setup test data
dt_nest <- w2l_nest(
data = iris, # Input dataset
cols = 1:2 # Nest first 2 columns
)
# Example 1: Basic 2-fold cross-validation (reproducible)
nest_cv(
data = dt_nest, # Input nested data.table
v = 2, # Number of folds (2-fold CV)
seed = 123 # Reproducible folds
)
# Example 2: Repeated 2-fold CV, keeping only the split objects
nest_cv(
data = dt_nest, # Input nested data.table
v = 2, # Number of folds (2-fold CV)
repeats = 2, # Number of repetitions
seed = 123,
materialize = FALSE # No train/validate copies (saves memory)
)
# Example 3: data.frame subsets, ready for ASReml-R / lm() / glm()
cv_df <- nest_cv(dt_nest, v = 2, seed = 123, out_type = "df")
class(cv_df$train[[1]]) # "data.frame"
# Example 4: masking-style CV (keep all rows, hide validation phenotypes)
cv_idx <- nest_cv(dt_nest, v = 2, seed = 123, materialize = FALSE)
cv_idx[dt_nest, on = "name", full := i.data] # attach the full nested table
masked <- as.data.frame(cv_idx$full[[1]]) # a copy: dt_nest stays intact
masked$value[cv_idx$validate_idx[[1]]] <- NA
sum(is.na(masked$value)) # validation records to be predicted
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.