Learn R Programming

daltoolbox (version 1.3.767)

sample_stratified: Stratified Sampling

Description

Train/test split and k-fold partitioning that preserve the target class proportions.

Usage

sample_stratified(attribute)

Value

An object of class sample_stratified.

Arguments

attribute

Name of the target attribute whose class proportions should be preserved.

Details

Use this sampler when the response distribution matters, especially in imbalanced classification problems. Compared with simple random sampling, it reduces the chance that one split will overrepresent or underrepresent a class by accident.

Examples

Run this code
# using stratified sampling
sample <- sample_stratified("Species")
tt <- train_test(sample, iris)

# distribution of train
table(tt$train$Species)

# preparing dataset into four folds
folds <- k_fold(sample, iris, 4)

# distribution of folds
tbl <- NULL
for (f in folds) {
  tbl <- rbind(tbl, table(f$Species))
}
head(tbl)

Run the code above in your browser using DataLab