Skip to contents

Generates simulated longitudinal data from a growth mixture model with complex survey design features (stratification, clustering, weights). Useful for testing, validation, and teaching.

Usage

simulate_gmm_survey(
  n_individuals = 1000,
  n_times = 5,
  n_classes = 3,
  time_scores = NULL,
  class_proportions = NULL,
  growth_parameters = NULL,
  residual_sds = NULL,
  design = "stratified_cluster",
  n_strata = 4,
  n_clusters = 100,
  cluster_size = 10,
  weight_type = "inverse_prob",
  missing_rate = 0.1,
  missing_mechanism = "MCAR",
  covariates = TRUE,
  seed = NULL
)

Arguments

n_individuals

Number of individuals to generate (default: 1000)

n_times

Number of time points (default: 5)

n_classes

Number of latent classes (default: 3)

time_scores

Numeric vector of time scores (default: 0:(n_times-1))

class_proportions

Numeric vector of class proportions (must sum to 1)

growth_parameters

List of lists, each containing intercept and slope for each class. If NULL, reasonable defaults are generated.

residual_sds

Numeric vector of residual standard deviations by class

design

Character string: "srs" (simple random sample), "stratified", "cluster", or "stratified_cluster" (default)

n_strata

Number of strata if stratified design (default: 4)

n_clusters

Number of clusters (PSUs) if cluster design (default: 100)

cluster_size

Average cluster size (default: 10)

weight_type

Character: "none", "inverse_prob", or "calibrated"

missing_rate

Proportion of missing data (default: 0.1)

missing_mechanism

Character: "MCAR" (default), "MAR", or "MNAR"

covariates

Logical, include time-invariant covariates? (default: TRUE)

seed

Random seed for reproducibility (default: NULL)

Value

Data frame in long format with variables:

id

Individual ID

time

Time point

outcome

Observed outcome value

true_class

True latent class (for validation)

stratum

Stratum indicator (if stratified)

psu

Primary sampling unit (if clustered)

weight

Sampling weight (if weighted)

covariates

Additional covariates (if requested)

Details

This function generates data following a growth mixture model:

For individual i in class k: $$y_{it} = (\beta_{0k} + u_{0i}) + (\beta_{1k} + u_{1i}) * t + \epsilon_{it}$$

where:

  • \(\beta_{0k}\), \(\beta_{1k}\) are class-specific intercept and slope

  • \(u_{0i}\), \(u_{1i}\) are random effects (optional, not yet implemented)

  • \(\epsilon_{it}\) ~ N(0, \(\sigma_k^2\)) are residuals

Survey design features:

  • Stratification: Population divided into strata, sampling within strata

  • Clustering: Individuals nested within PSUs (e.g., schools, neighborhoods)

  • Weights: Inverse probability weights to account for unequal selection probabilities

The function can generate various missing data patterns to test robustness of analyses.

References

Sterba, S. K. (2014). Fitting nonlinear latent growth curve models with individually varying time points. Structural Equation Modeling, 21(4), 630-647.

Examples

if (FALSE) { # \dontrun{
# Simple 3-class example
sim_data <- simulate_gmm_survey(
  n_individuals = 1000,
  n_times = 5,
  n_classes = 3,
  design = "stratified_cluster",
  seed = 123
)

head(sim_data)
table(sim_data$true_class)

# Fit model to simulated data
fit <- gmm_survey(
  data = sim_data,
  id = "id",
  time = "time",
  outcome = "outcome",
  n_classes = 3,
  strata = "stratum",
  cluster = "psu",
  weights = "weight",
  starts = 100
)

# Compare estimated vs true parameters
summary(fit)
} # }