simulate_ordinal: Simulate correlated ordinal variables

View source: R/correlation.R

simulate_ordinalR Documentation

Simulate correlated ordinal variables

Description

Simulate correlated ordinal variables

Usage

simulate_ordinal(
  n,
  probabilities,
  n_variables = 1L,
  rho = 0,
  structure = c("independent", "exchangeable", "ar1", "custom"),
  correlation = NULL,
  labels = NULL,
  variable_names = NULL,
  seed = NULL,
  batch = NULL
)

Arguments

n

Number of observations.

probabilities

A numeric vector used for every variable or a matrix with one row per variable and one column per category.

n_variables

Number of variables when probabilities is a vector.

rho, structure, correlation

Correlation arguments for the latent normal variables. structure follows the same resolution rule as simulate_correlated(): leaving it unset selects "exchangeable" when a non-zero rho is given. Passed to the latent Gaussian generator.

labels

Optional category labels.

variable_names

Optional variable names.

seed

Optional random seed.

batch

Optional single positive whole number. When given, the simulator runs batch times and returns a plain list of batch results, each exactly what the same call without batch returns. With a seed, every dataset gets its own seed drawn from seed, so the whole batch is reproducible; without one the datasets are consecutive draws from the session's random-number stream. The default NULL returns a single result.

Value

A simulab_sim base data.frame with one row per observation and columns id and one column per variable, each holding a category label. Two tidy tables come from as.data.frame(): what = "probabilities" has one row per variable and category with columns variable, category and probability; what = "correlation" has one row per latent correlation-matrix cell with columns row, column and correlation. That correlation is the latent Gaussian one, not the correlation of the realized categories, which is attenuated by the thresholding.

Examples

result <- simulate_ordinal(
  n = 200, probabilities = c(0.2, 0.5, 0.3), n_variables = 2, rho = 0.4, seed = 1
)
head(result)
as.data.frame(result, what = "probabilities")

simulab documentation built on Oct. 15, 2026, 5:08 p.m.