Simulate Data From a Lavaan Model Syntax
simulateData.RdSimulate data starting from a lavaan model syntax.
Usage
lavSimulateData(model = NULL, model_type = "sem", meanstructure = FALSE,
int_ov_free = TRUE, int_lv_free = FALSE,
marker_int_zero = FALSE, conditional_x = FALSE,
composites = TRUE, fixed_x = FALSE,
orthogonal = FALSE, std_lv = TRUE, auto_fix_first = FALSE,
auto_fix_single = FALSE, auto_var = TRUE, auto_cov_lv_x = TRUE,
auto_cov_y = TRUE, ..., sample_nobs = 500L, ov_var = NULL,
group_label = NULL, skewness = NULL,
kurtosis = NULL, cluster_idx = NULL, seed = NULL, empirical = FALSE,
mass = TRUE, ordered_center = TRUE, return_type = "data.frame",
return_fit = FALSE, debug = FALSE, standardized = FALSE)
simulateData(model = NULL, model_type = "sem", meanstructure = FALSE,
int_ov_free = TRUE, int_lv_free = FALSE,
marker_int_zero = FALSE, conditional_x = FALSE,
composites = TRUE, fixed_x = FALSE,
orthogonal = FALSE, std_lv = TRUE, auto_fix_first = FALSE,
auto_fix_single = FALSE, auto_var = TRUE, auto_cov_lv_x = TRUE,
auto_cov_y = TRUE, ..., sample_nobs = 500L, ov_var = NULL,
group_label = NULL, skewness = NULL,
kurtosis = NULL, cluster_idx = NULL, seed = NULL, empirical = FALSE,
mass = FALSE, ordered_center = TRUE, return_type = "data.frame",
return_fit = FALSE, debug = FALSE, standardized = FALSE)
lav_data_simulate_old(..., ordered_center = FALSE)Arguments
- model
A description of the user-specified model. Typically, the model is described using the lavaan model syntax. See
model.syntaxfor more information. Alternatively, a parameter table (e.g., the output of thelavParTable()function) is also accepted.- model_type
Set the model type: possible values are
"cfa","sem"or"growth". This may affect how starting values are computed, and may be used to alter the terminology used in the summary output, or the layout of path diagrams that are based on a fitted lavaan object.- meanstructure
If
TRUE, the means of the observed variables enter the model. If"default", the value is set based on the user-specified model, and/or the values of other arguments.- int_ov_free
If
FALSE, the intercepts of the observed variables are fixed to zero.- int_lv_free
If
FALSE, the intercepts of the latent variables are fixed to zero.- marker_int_zero
Logical. Only relevant if the metric of each latent variable is set by fixing the first factor loading to unity. If
TRUE, it impliesmeanstructure = TRUEandstd_lv = FALSE, and it fixes the intercepts of the marker indicators to zero, while freeing the means/intercepts of the latent variables. Only works correctly for single group, single level models.- conditional_x
If
TRUE, we set up the model conditional on the exogenous `x' covariates; the model-implied sample statistics only include the non-x variables. IfFALSE, the exogenous `x' variables are modeled jointly with the other variables, and the model-implied statistics reflect both sets of variables. If"default", the value is set depending on the estimator, and whether or not the model involves categorical endogenous variables.- composites
If
TRUE, use the new (0.6-20) approach for handling composites.- fixed_x
If
TRUE, the exogenous `x' covariates are considered fixed variables and the means, variances and covariances of these variables are fixed to their sample values. IfFALSE, they are considered random, and the means, variances and covariances are free parameters. If"default", the value is set depending on the mimic option.- orthogonal
If
TRUE, the exogenous latent variables are assumed to be uncorrelated.- std_lv
If
TRUE, the metric of each latent variable is determined by fixing their variances to 1.0. IfFALSE, the metric of each latent variable is determined by fixing the factor loading of the first indicator to 1.0.- auto_fix_first
If
TRUE, the factor loading of the first indicator is set to 1.0 for every latent variable.- auto_fix_single
If
TRUE, the residual variance (if included) of an observed indicator is set to zero if it is the only indicator of a latent variable.- auto_var
If
TRUE, the (residual) variances of both observed and latent variables are set free.- auto_cov_lv_x
If
TRUE, the covariances of exogenous latent variables are included in the model and set free.- auto_cov_y
If
TRUE, the covariances of dependent variables (both observed and latent) are included in the model and set free.- ...
additional arguments passed to the
lavaanfunction.- sample_nobs
Number of observations. If a vector, multiple datasets are created. If
return_type = "matrix"orreturn_type = "cov", a list oflength(sample_nobs)is returned, with either the data or covariance matrices, each one based on the number of observations as specified insample_nobs. Ifreturn_type = "data.frame", all datasets are merged and agroupvariable is added to mimic a multiple group dataset.- ov_var
The user-specified variances of the observed variables.
- group_label
The group labels that should be used if multiple groups are created.
- skewness
Numeric vector. The skewness values for the observed variables. Defaults to zero.
- kurtosis
Numeric vector. The kurtosis values for the observed variables. Defaults to zero.
- cluster_idx
Optional. Only used (and only available via
lavSimulateData()andsimulateData()) for multilevel models. An integer vector (or a list of integer vectors, one per group) giving the cluster membership of each level-1 unit. If supplied, it determines both the number of level-1 units and the number of clusters. If not supplied for a multilevel model, balanced clusters are created based onsample_nobs. Providing this argument (or a multilevel model syntax withlevel:blocks) routes the request to the multilevel data-generation engine.- seed
Set random seed.
- empirical
Logical. If
TRUE, the implied moments (Mu and Sigma) specify the empirical not population mean and covariance matrix.- mass
Logical. If
TRUE, use the mvrnorm() function from the MASS package (the default before 0.7-1) instead of the internal lav_mvrnorm() function.- ordered_center
Logical. Only relevant for categorical data, which is generated by the (new) multilevel-aware engine. If
TRUE(the default), each ordered variable is centered (its sample mean is subtracted) before it is cut at the model-implied thresholds; this also works when the model-implied mean is nonzero. IfFALSE, the variable is cut at the thresholds directly (the behaviour of the older engine).- return_type
If
"data.frame", a data.frame is returned. If"matrix", a numeric matrix is returned (without any variable names). If"cov", a covariance matrix is returned (without any variable names).- return_fit
If
TRUE, return the fitted model that has been used to generate the data as an attribute (called"fit"); this may be useful for inspection.- debug
If
TRUE, debugging information is displayed.- standardized
If
TRUE, the residual variances of the observed variables are set in such a way that the model implied variances are unity. This allows regression coefficients and factor loadings (involving observed variables) to be specified in a standardized metric.
Details
Model parameters can be specified by fixed values in the lavaan model syntax. If no fixed values are specified, the value zero will be assumed, except for factor loadings and variances, which are set to 0.7 and 1.0 respectively. By default, multivariate normal data are generated. However, by providing skewness and/or kurtosis values, nonnormal multivariate data can be generated, using the Vale & Maurelli (1983) method.
There is a single data-simulation engine. Multilevel data (model syntax
containing level: blocks, or when the cluster_idx argument is
provided) are generated by an internal multilevel worker; all single-level data
(continuous and categorical, including the ov_var, skewness,
kurtosis, standardized and mass options) are generated by
the historical single-level worker, so that the continuous, single-level output
remains byte-identical to previous versions. For multilevel data, the
ov_var, skewness, kurtosis, standardized and
mass arguments are not (yet) supported and are ignored (with a warning).
lav_data_simulate_old() is a deprecated wrapper kept for backward
compatibility; it forwards to the unified engine (with ordered_center =
FALSE by default, reproducing the historical threshold cut).
Value
The generated data. Either as a data.frame
(if return_type="data.frame"),
a numeric matrix (if return_type="matrix"),
or a covariance matrix (if return_type="cov").
Examples
# specify population model
population.model <- ' f1 =~ x1 + 0.8*x2 + 1.2*x3
f2 =~ x4 + 0.5*x5 + 1.5*x6
f3 =~ x7 + 0.1*x8 + 0.9*x9
f3 ~ 0.5*f1 + 0.6*f2
'
# generate data
set.seed(1234)
myData <- lavSimulateData(population.model, sample_nobs = 100L)
# population moments
fitted(sem(population.model))
#> $cov
#> x1 x2 x3 x4 x5 x6 x7 x8 x9
#> x1 2.000
#> x2 0.800 1.640
#> x3 1.200 0.960 2.440
#> x4 0.000 0.000 0.000 2.000
#> x5 0.000 0.000 0.000 0.500 1.250
#> x6 0.000 0.000 0.000 1.500 0.750 3.250
#> x7 0.500 0.400 0.600 0.600 0.300 0.900 2.610
#> x8 0.050 0.040 0.060 0.060 0.030 0.090 0.161 1.016
#> x9 0.450 0.360 0.540 0.540 0.270 0.810 1.449 0.145 2.304
#>
# sample moments
round(cov(myData), 3)
#> x1 x2 x3 x4 x5 x6 x7 x8 x9
#> x1 2.109 0.974 1.287 -0.100 0.148 -0.107 0.447 0.070 0.243
#> x2 0.974 1.387 1.014 0.000 0.055 -0.032 0.544 0.150 0.590
#> x3 1.287 1.014 2.774 -0.298 0.160 -0.164 0.551 0.117 0.574
#> x4 -0.100 0.000 -0.298 1.817 0.282 1.114 0.342 0.004 0.604
#> x5 0.148 0.055 0.160 0.282 0.960 0.446 0.338 0.023 0.331
#> x6 -0.107 -0.032 -0.164 1.114 0.446 3.614 0.987 0.118 1.210
#> x7 0.447 0.544 0.551 0.342 0.338 0.987 2.605 0.228 1.600
#> x8 0.070 0.150 0.117 0.004 0.023 0.118 0.228 0.881 0.077
#> x9 0.243 0.590 0.574 0.604 0.331 1.210 1.600 0.077 2.404
round(colMeans(myData), 3)
#> x1 x2 x3 x4 x5 x6 x7 x8 x9
#> -0.196 -0.161 -0.206 -0.103 -0.146 -0.265 0.004 -0.094 -0.138
# fit model
myModel <- ' f1 =~ x1 + x2 + x3
f2 =~ x4 + x5 + x6
f3 =~ x7 + x8 + x9
f3 ~ f1 + f2 '
fit <- sem(myModel, data=myData)
summary(fit)
#> lavaan 0.7-1.3097 ended normally after 41 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 21
#>
#> Number of observations 100
#>
#> Model Test User Model:
#>
#> Test statistic 18.021
#> Degrees of freedom 24
#> P-value (Chi-square) 0.802
#>
#> Parameter Estimates:
#>
#> Standard errors Standard
#> Information Expected
#> Information saturated (h1) model Structured
#>
#> Latent Variables:
#> Estimate Std.Err z-value P(>|z|)
#> f1 =~
#> x1 1.000
#> x2 0.883 0.151 5.851 0.000
#> x3 1.108 0.195 5.671 0.000
#> f2 =~
#> x4 1.000
#> x5 0.435 0.168 2.589 0.010
#> x6 1.945 0.522 3.723 0.000
#> f3 =~
#> x7 1.000
#> x8 0.089 0.087 1.027 0.304
#> x9 1.119 0.198 5.644 0.000
#>
#> Regressions:
#> Estimate Std.Err z-value P(>|z|)
#> f3 ~
#> f1 0.471 0.149 3.170 0.002
#> f2 0.980 0.284 3.449 0.001
#>
#> Covariances:
#> Estimate Std.Err z-value P(>|z|)
#> f1 ~~
#> f2 -0.038 0.108 -0.352 0.725
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|)
#> .x1 1.006 0.207 4.849 0.000
#> .x2 0.528 0.138 3.842 0.000
#> .x3 1.417 0.274 5.166 0.000
#> .x4 1.236 0.223 5.546 0.000
#> .x5 0.844 0.126 6.704 0.000
#> .x6 1.449 0.537 2.700 0.007
#> .x7 1.165 0.269 4.333 0.000
#> .x8 0.861 0.122 7.051 0.000
#> .x9 0.610 0.279 2.185 0.029
#> f1 1.082 0.300 3.613 0.000
#> f2 0.563 0.231 2.437 0.015
#> .f3 0.669 0.239 2.804 0.005
#>