knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(balancedSampling)

balancedSampling helps to perform constrained randomization of a population so that the modalities of the qualitative variables are equally distributed across samples.

Lets suppose you have two qualitative variables 'A' and 'B', with four and two modalities respectively. The population is a cartesian product of these two variables (or a multiple of this cartesian product), i.e. n=8 or a multipe of 8:

data <- expand.grid(A=LETTERS[1:4], B=letters[1:2])
data

The frequency of the modalities are 2 (8/4) for the variable A, 4 (8/2) for variable B. There is only one partition possible of this population in two subset balanced as to the number of modalities for both variable, viz a partition in 2 sets (since the frequency of both variables must be a multiple of the number of sample).

This two sets can be randomly drawn using:

balancedSampling(data, c("A", "B"), 2)

The sample can be further constrained by avoiding two consecutive occurrences of a modality:

balancedSampling(data, c("A", "B"), 2, c(1,1))

Such partition is also available with only one variable, or more than two variables.

data <- data.frame(A=rep(LETTERS[1:3], 6))
data
balancedSampling(data, "A", 3)


sylvainloiseau/balancedSampling documentation built on July 5, 2020, 6:52 p.m.