Skip to contents

Handler function providing easy access and uniform syntax to the various implemented repeated sampling techniques. See the Details section below for further information.

Usage

resample(
  x,
  n,
  k,
  method = c("rswor", "srswor", "stratified_rswor", "balanced_rswor", "permutation",
    "kfolds", "stratified_kfolds", "balanced_kfolds", "leave_p_out", "leave_one_out",
    "rswr", "srswr", "stratified_rswr", "balanced_rswr", "bootstrap"),
  prob = NULL,
  undersample = FALSE
)

Arguments

x

either an integer representing the population size, or a vector of stratification variables

n

integer, either the sample size or the number of elements to holdout

k

integer, the number of repeated samples to generate. Used as the number of folds in k-fold sampling

method

character string, one of the supported sampling techniques

prob

(optional) vector of positive numeric values, the probability weights for obtaining the elements from the population. If provided, its length must match the population size

undersample

logical, whether to remove elements from the population in order to try to obtain balanced data

Value

An object of class resampling.

Details

This function internally calls one of the implemented functions for repeated sampling based on the value of the method argument:

"rswor"

repeatedSampleWithoutReplacement

"srswor"

repeatedSimpleRandomSampleWithoutReplacement

"stratified_rswor"

repeatedStratifiedSampleWithoutReplacement

"balanced_rswor"

repeatedBalancedSampleWithoutReplacement

"permutation"

repeatedPermutationSample

"kfolds"

repeatedRandomKm1Folds

"stratified_kfolds"

repeatedStratifiedKm1Folds

"balanced_kfolds"

repeatedBalancedKm1Folds

"leave_p_out"

repeatedLeavePOut

"leave_one_out"

repeatedLeaveOneOut

"rswr"

repeatedSampleWithReplacement

"srswr"

repeatedSimpleRandomSampleWithReplacement

"stratified_rswr"

repeatedStratifiedSampleWithReplacement

"balanced_rswr"

repeatedBalancedSampleWithReplacement

"bootstrap"

repeatedBootstrapSample

See the specific function help page for further details on the implemented methodology.

Author

Alessandro Barberis

Examples

#Set seed for reproducibility
set.seed(seed = 5381L)

#Random sampling without replacement
resample(
 x = 10,
 n = 5,
 k = 2,
 method = "rswor"
)
#> 
#> 2 samples taken from a population of 10 elements by using random
#> sampling without replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 9, 7, 2, ...          5           5
#> 2            2 3, 4, 1, 8, ...          5           5
#> 

#Simple random sampling without replacement
resample(
 x = 10,
 n = 5,
 k = 2,
 method = "srswor"
)
#> 
#> 2 samples taken from a population of 10 elements by using simple random
#> sampling without replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 8, 1, 7, 4, ...          5           5
#> 2            2 5, 2, 8, 6, ...          5           5
#> 

#Permutation
resample(
 x = 10,
 k = 2,
 method = "permutation"
)
#> 
#> 2 samples taken from a population of 10 elements by using permutation
#> sampling.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 5, 4, 8, 1, ...         10           0
#> 2            2 7, 10, 5, , ...         10           0
#> 

#Stratified random sampling without replacement
resample(
 x = c(rep("a", 3),rep("b", 6)),
 n = 6,
 k = 2,
 method = "stratified_rswor"
)
#> 
#> 2 samples taken from a population of 9 elements by using stratified
#> random sampling without replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 7, 3, 4, 1, ...          6           3
#> 2            2 3, 1, 4, 9, ...          6           3
#> 


#Balanced random sampling without replacement
resample(
 x = c(rep("a", 3),rep("b", 6)),
 n = 6,
 k = 2,
 method = "balanced_rswor"
)
#> 
#> 2 samples taken from a population of 9 elements by using balanced
#> random sampling without replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 3, 1, 8, 6, ...          6           3
#> 2            2 1, 9, 3, 8, ...          6           3
#> 

#K-folds sampling
resample(
 x = 10,
 k = 3,
 method = "kfolds"
)
#> 
#> 3 samples taken from a population of 10 elements by using random k-fold
#> sampling.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 3, 4, 7, ...          6           4
#> 2            2 1, 2, 5, 6, ...          7           3
#> 3            3 2, 3, 4, 5, ...          7           3
#> 

#Stratified k-folds sampling
resample(
 x = c(rep("a", 3),rep("b", 6)),
 k = 3,
 method = "stratified_kfolds"
)
#> 
#> 3 samples taken from a population of 9 elements by using stratified
#> k-fold sampling.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 3, 5, 7, ...          6           3
#> 2            2 1, 2, 4, 5, ...          6           3
#> 3            3 2, 3, 4, 6, ...          6           3
#> 

#Balanced k-folds sampling (balanced population)
resample(
 x = c(rep(1,6),rep(2,6)),
 k = 3,
 method = "balanced_kfolds"
)
#> 
#> 3 samples taken from a population of 12 elements by using balanced
#> k-fold sampling.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 4, 5, 6, ...          8           4
#> 2            2 2, 3, 4, 5, ...          8           4
#> 3            3 1, 2, 3, 6, ...          8           4
#> 

#Balanced k-folds sampling (unbalanced population)
resample(
 x = c(rep(1,6),rep(2,12)),
 k = 3,
 method = "balanced_kfolds",
 undersample = TRUE
)
#> 
#> 3 samples taken from a population of 18 elements by using balanced
#> k-fold sampling.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 2, 3, 6, ...          8           4
#> 2            2 1, 4, 5, 6, ...          8           4
#> 3            3 2, 3, 4, 5, ...          8           4
#> 

#Leave-p-out sampling
resample(
 x = 5,
 n = 2,
 method = "leave_p_out"
)
#> 
#> 10 samples taken from a population of 5 elements by using leave-p-out
#> sampling.
#> 
#>   sampleNumber  sample sampleSize holdoutSize
#> 1            1 3, 4, 5          3           2
#> 2            2 2, 4, 5          3           2
#> 3            3 2, 3, 5          3           2
#> 4            4 2, 3, 4          3           2
#> 5            5 1, 4, 5          3           2
#> ...
#> 

#Leave-one-out sampling
resample(
 x = 5,
 method = "leave_one_out"
)
#> 
#> 5 samples taken from a population of 5 elements by using leave-one-out
#> sampling.
#> 
#>   sampleNumber     sample sampleSize holdoutSize
#> 1            1 1, 2, 3, 5          4           1
#> 2            2 2, 3, 4, 5          4           1
#> 3            3 1, 2, 3, 4          4           1
#> 4            4 1, 2, 4, 5          4           1
#> 5            5 1, 3, 4, 5          4           1
#> 

#Random sampling with replacement
resample(
 x = 10,
 n = 5,
 k = 2,
 method = "rswr"
)
#> 
#> 2 samples taken from a population of 10 elements by using random
#> sampling with replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 10, 6, 6, , ...          5           8
#> 2            2 8, 1, 9, 2, ...          5           5
#> 

#Simple random sampling with replacement
resample(
 x = 10,
 n = 5,
 k = 2,
 method = "srswr"
)
#> 
#> 2 samples taken from a population of 10 elements by using simple random
#> sampling with replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 9, 7, 1, ...          5           6
#> 2            2 2, 4, 10, , ...          5           6
#> 

#Stratified random sampling with replacement
resample(
 x = c(rep("a", 3),rep("b", 6)),
 n = 6,
 k = 2,
 method = "stratified_rswr"
)
#> 
#> 2 samples taken from a population of 9 elements by using stratified
#> random sampling with replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 1, 8, 8, 8, ...          6           5
#> 2            2 5, 6, 6, 3, ...          6           4
#> 


#Balanced random sampling with replacement
resample(
 x = c(rep("a", 3),rep("b", 6)),
 n = 6,
 k = 2,
 method = "balanced_rswr"
)
#> 
#> 2 samples taken from a population of 9 elements by using balanced
#> random sampling with replacement.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 4, 9, 2, 1, ...          6           5
#> 2            2 2, 1, 7, 5, ...          6           4
#> 

#Bootstrap sampling
resample(
 x = 10,
 k = 2,
 method = "bootstrap"
)
#> 
#> 2 samples taken from a population of 10 elements by using ordinary
#> bootstrap sampling.
#> 
#>   sampleNumber          sample sampleSize holdoutSize
#> 1            1 7, 5, 3, 6, ...         10           2
#> 2            2 4, 6, 1, 7, ...         10           4
#>