| train_test_split | R Documentation |
Creates indices for training and testing sets. Can use simple random sampling or stratified sampling based on missing values.
train_test_split(n_obs = NULL, prop_test = 0.2, stratify = FALSE,
is_NA = NULL, n_rep = NULL)
n_obs |
Integer. Total number of observations. Required if |
prop_test |
Numeric between 0 and 1. Proportion of data to use for testing. Default is 0.2. |
stratify |
Logical. If |
is_NA |
Optional logical vector. |
n_rep |
Optional integer. Number of folds for creating a
stratified or simple random |
The function creates indices for training and testing sets using either simple random sampling or stratified sampling based on missing values.
Stratified sampling:
When stratify = TRUE, the sampling preserves the proportion of rows with
missing values in both training and testing sets. In this case:
is_NA must be provided
n_obs is ignored (determined from is_NA)
Missing values statistics:
If is_NA is provided but stratify = FALSE, the function will still
compute missing values statistics for the resulting splits. The length of
is_NA is checked against n_obs to ensure consistency. These statistics will be
available in the summary output, with a note indicating that they were
not used for stratification.
Reproducibility: To ensure reproducible splits, call set.seed before using this function.
An object of class idx.split.
If n_rep = NULL (default), the returned object contains:
idx_train: Integer vector of training indices.
idx_test: Integer vector of testing indices (or NULL if
prop_test = 0).
n_train: Integer. Number of training observations.
n_test: Integer. Number of testing observations.
stratified: Logical indicating whether stratified sampling was
used.
missing_stats: List with missing-value counts (if is_NA is
provided), containing:
train_na: Number of rows with missing values in the training set.
test_na: Number of rows with missing values in the testing set.
total_na: Total number of rows with missing values.
If n_rep is specified, the returned object contains:
fold_idx: Integer vector indicating the fold assignment of each
observation.
n_rep: Integer. Number of folds.
stratified: Logical indicating whether stratified sampling was
used.
fold_stats: Data frame summarizing each fold, including the number
of observations, the number of rows with missing values, and the
corresponding proportion of missing values.
# Simple random split
set.seed(123)
split <- train_test_split(n_obs = 100, prop_test = 0.3)
str(split)
# Stratified split by missing values
missing <- sample(c(TRUE, FALSE), 100, replace = TRUE, prob = c(0.2, 0.8))
split <- train_test_split(
prop_test = 0.3,
stratify = TRUE,
is_NA = missing
)
summary(split)
# Compute missing stats without stratification
missing2 <- sample(c(TRUE, FALSE), 100, replace = TRUE, prob = c(0.2, 0.8))
split <- train_test_split(
n_obs = 100,
prop_test = 0.3,
stratify = FALSE,
is_NA = missing2
)
summary(split) # Note about missing stats will appear
# Stratified 5-fold partition
folds <- train_test_split(
n_obs = 1000,
is_NA = sample(c(TRUE, FALSE), prob = c(0.1, 0.9), replace = TRUE, size = 1000),
stratify = TRUE,
n_rep = 5
)
summary(folds)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.