sim.lavaan: Generates Simulated Data from a lavaan Model Syntax

View source: R/sim.lavaan.R

sim.lavaanR Documentation

Generates Simulated Data from a lavaan Model Syntax

Description

This function simulates data from a lavaan model syntax with unstandardized or standardized parameters. By default, the function simulates observed variables based on the model specified in the argument model in a unstandardized metric.

Usage

sim.lavaan(model, n = 500, std = FALSE, skew = NULL, kurt = NULL,
           observed = TRUE, latent = FALSE, fscores = FALSE, composites = FALSE,
           errors = FALSE, matrices = FALSE, method = c("eigen", "svd", "chol"),
           seed = NULL, max.iter = 100, check = TRUE)

Arguments

model

a character string indicating the lavaan model syntax.

n

a numeric value indicating the number of observations. By default, 500 simulated cases are simulated.

std

logical: if TRUE, lavaan model syntax specified in the argument model is based on standardized parameters.

skew

a numeric vector indicating the skewness values for the observed variables. Note that this argument is only used when std = FALSE.

kurt

a numeric vector indicating the kurtosis values for the observed variables. Note that this argument is only used when std = FALSE. Note that this argument is only used when std = FALSE.

observed

logical: if TRUE (default), observed variables are included. Note that this argument is only used when std = TRUE.

latent

logical: if TRUE, latent variables are included. Note that this argument is only used when std = TRUE.

fscores

logical: if TRUE, factor score are included. Note that this argument is only used when std = TRUE.

composites

logical: if TRUE, composite variables are included. Note that this argument is only used when std = TRUE.

errors

logical: if TRUE, observed error and latent disturbance variables are included. Note that this argument is only used when std = TRUE.

matrices

logical: if TRUE, matrices are included as attributes. Note that this argument is only used when std = TRUE.

method

a character string indicating the matrix decomposition used to determine the matrix root of sigma in the random number generator for the multivariate normal distribution, i.e., "eigen" (default) for eigenvalue decomposition, "svd" for singular value decomposition, and "chol" for Cholesky decomposition. Note that this argument is only used when std = TRUE.

seed

a numeric value specifying the seed of the pseudo-random numbers used when simulating multivariate normal data.

max.iter

a numeric value indicating the maximum number of iterations when solving for error variances and correlation matrix. Note that this argument is only used when std = TRUE.

check

logical: if TRUE (default), argument specification is checked.

Value

Returns a data frame.

Note

This function uses the function simulateData from the R package lavaan by Yves Rosseel (2012) when std = FALSE and is based on modified copies of the function sim_standardized from the simstandard package by W. Joel Schneider (2021) and the function rmvnorm from the package mvtnorm by Alan Genz and Frank Bretz (2026) when std = TRUE.

Author(s)

Takuya Yanagida

References

Genz, A., & Bretz, F. (2026). mvtnorm: Multivariate Normal and t Distributions. R package version 1.3-6. https://doi.org/10.32614/CRAN.package.mvtnorm

Rosseel, Y. (2012). lavaan: An R Package for Structural Equation Modeling. Journal of Statistical Software, 48, 1-36. https://doi.org/10.18637/jss.v048.i02

Schneider, W. J. (2021). simstandard: Generate standardized data. R package version 0.6.3. https://doi.org/10.32614/CRAN.package.simstandard

Examples

## Not run: 

# Model specification
model <- '# Measurement model
          f1 =~ 0.8*x1 + 0.7*x2 + 0.5*x3
          f2 =~ 0.7*x4 + 0.8*x5 + 0.6*x6
          # Factor correlation
          f1 ~~ 0.4*f2'

# Example 1: Unstandardized parameters, simulate 200 cases
simdat1 <- sim.lavaan(model, n = 200)

# Example 2: Standardized parameters, simulate 200 cases
simdat2 <- sim.lavaan(model, std = TRUE, n = 200)

## End(Not run)

misty documentation built on Aug. 2, 2026, 9:06 a.m.

Related to sim.lavaan in misty...