simulate_lca: Simulate a latent class model

View source: R/latent.R

simulate_lcaR Documentation

Simulate a latent class model

Description

Draws a latent class for each observation from proportions, then draws every indicator independently from that class's category probabilities (the usual local-independence assumption of latent class analysis).

Usage

simulate_lca(
  n,
  probabilities,
  proportions = NULL,
  class_labels = NULL,
  category_labels = NULL,
  seed = NULL,
  batch = NULL
)

Arguments

n

Number of observations. A single whole number of at least 2.

probabilities

Item probabilities as an array with dimensions class, indicator and category, or as a tidy data frame with columns class, indicator, category and probability. Category probabilities must sum to one within each class and indicator. The dimnames (the class, indicator and category values of a tidy table) name the classes, the indicator columns and the categories.

proportions

Latent-class proportions, one positive value per class; they are rescaled to sum to one. NULL (the default) makes every class equally likely.

class_labels, category_labels

Optional labels, one unique value per class and per category, overriding the names given in probabilities. Without names there, class_labels defaults to "Class 1", "Class 2" and so on, and category_labels to the integers 1:categories. The category labels are the values written into the indicator columns.

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, latent_class (the true class label) and one column per indicator holding the sampled value of category_labels. A parameters component holds one row per class-by-indicator-by-category combination with columns latent_class, indicator, category, probability and proportion.

Examples

# `probabilities` is a class x indicator x category array. Values fill the
# class dimension fastest, so each pair below is one category across the two
# classes: P(indicator = 1) then P(indicator = 2).
probabilities <- array(
  c(0.8, 0.2,   0.7, 0.3,     # category 1, for each class and indicator
    0.2, 0.8,   0.3, 0.7),    # category 2, for each class and indicator
  dim = c(2, 2, 2),
  dimnames = list(
    class = c("Class 1", "Class 2"),
    indicator = c("item_1", "item_2"),
    category = c("no", "yes")
  )
)

# Only the `indicator` dimnames name the output columns, so the class and
# category labels are passed explicitly.
result <- simulate_lca(
  n = 200,
  probabilities = probabilities,
  proportions = c(0.6, 0.4),
  category_labels = c("no", "yes"),
  seed = 1
)
head(result)
as.data.frame(result, what = "parameters")

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