gen_coda_with_zeros_and_missings: Generate compositional data with zeros and missing values

View source: R/utils.R

gen_coda_with_zeros_and_missingsR Documentation

Generate compositional data with zeros and missing values

Description

Simulate compositional data and optionally introduce structural zeros (interpreted as values below a detection limit) and missing values.

The function first generates a compositional data set 'X0', then creates a modified version 'X' by:

  • replacing values below 'dl_par' by zero, if 'zeros = TRUE',

  • introducing missing values at random, if 'missings = TRUE'.

A matrix of detection limits 'DL' is also returned. It contains 'dl_par' in the positions that were censored to zero, and '0' elsewhere.

Usage

gen_coda_with_zeros_and_missings(
  n,
  d,
  missings = TRUE,
  zeros = TRUE,
  dl_par = 0.05,
  na_p = 0.15
)

Arguments

n

Number of observations.

d

Dimension of the latent coordinate space used to generate the compositions.

missings

Logical; if 'TRUE', introduce missing values at random.

zeros

Logical; if 'TRUE', replace values below 'dl_par' by zero.

dl_par

Detection-limit threshold used to generate zeros.

na_p

Probability that any entry is replaced by 'NA' when 'missings = TRUE'.

Details

Compositions are generated from multivariate normal coordinates and mapped to the simplex through 'composition()'. The eigenvector rotation is included to induce a non-trivial covariance structure in the generated coordinates.

Missing values are introduced completely at random, independently for each cell, with probability 'na_p'.

Value

A list with three components:

X

The generated compositional data set with simulated zeros and/or missing values.

DL

A matrix of detection limits, with 'dl_par' in censored positions and '0' elsewhere.

X0

The original simulated compositional data set before introducing zeros or missing values.

Examples

set.seed(123)
sim <- gen_coda_with_zeros_and_missings(100, 4)

str(sim)
summary(sim$X0)
summary(sim$X)
table(sim$X == 0, useNA = "ifany")


coda.base documentation built on March 4, 2026, 9:06 a.m.