train_test_split: Split data into training and testing sets

View source: R/prtree_split.R

train_test_splitR Documentation

Split data into training and testing sets

Description

Creates indices for training and testing sets. Can use simple random sampling or stratified sampling based on missing values.

Usage

train_test_split(n_obs = NULL, prop_test = 0.2, stratify = FALSE,
  is_NA = NULL, n_rep = NULL)

Arguments

n_obs

Integer. Total number of observations. Required if stratify = FALSE. Ignored when stratify = TRUE (in which case n_obs is determined from is_NA).

prop_test

Numeric between 0 and 1. Proportion of data to use for testing. Default is 0.2.

stratify

Logical. If TRUE, performs stratified sampling based on missing values. In this case, is_NA must be provided. Default is FALSE.

is_NA

Optional logical vector. TRUE indicates rows with missing values. Required if stratify = TRUE. If provided when stratify = FALSE, its length must equal n_obs and missing statistics will be computed (see details).

n_rep

Optional integer. Number of folds for creating a stratified or simple random k-fold partition. If NULL (default), the function generates a single training/test split according to prop_test. When specified, prop_test is ignored and the function returns a vector of fold assignments (fold_idx) instead of training and testing indices.

Details

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.

Value

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.

Examples


# 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)

PRTree documentation built on Nov. 23, 2026, 9:08 a.m.