sampleControlElements: Sample control elements that match a target distribution.

View source: R/sampleControlElements.R

sampleControlElementsR Documentation

Sample control elements that match a target distribution.

Description

Randomly sample from a set of control (background) elements, such that the selected elements are similarly distributed as a given set of target (foreground) elements.

Usage

sampleControlElements(
  x,
  idxTarget,
  idxControl = NULL,
  nbins = 50,
  oversample = 1
)

Arguments

x

numeric vector (or list of numeric vectors). idxTarget and idxControl refer to the elements of x. If x is a list, all elements must have the same length.

idxTarget

numeric or logical vector specifying the elements in x that define the target distribution to be matched by the control elements.

idxControl

numeric or logical vector specifying the complete set of possible control elements in x (default: all that are not in idxTarget), from which a subset is to be sampled.

nbins

numeric(1) or numeric(length(x)) if x is a list, specifying the number of bins to group the values of x into. Higher numbers of bins will increase the match to the target distribution(s), but may fail if there are few elements to sample from (will throw a warning).

oversample

The number of control elements to sample for each target element.

Value

numeric vector with round(length(idxTarget) * oversample) elements, specifying the index (positions) of the sampled control elements.

Author(s)

Michael Stadler

Examples

x <- c(runif(1000, min = 0, max = 10),
       rnorm(200, mean = 5, sd = 1))
s <- sampleControlElements(x, idxTarget = 1001:1200, idxControl = 1:1000)
par(mfrow=c(2,2))
h <- hist(x, breaks = 20, main = "all")
hist(x[1:1000], breaks = h$breaks, main = "all control")
hist(x[1001:1200], breaks = h$breaks, main = "target")
hist(x[s], breaks = h$breaks, main = "sampled control")


fmicompbio/swissknife documentation built on June 11, 2025, 4:17 p.m.