Learn R Programming

lavaan (version 0.7-2)

lavSimulateData: Simulate Data From a Lavaan Model Syntax

Description

Simulate 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)

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").

Arguments

model

A description of the user-specified model. Typically, the model is described using the lavaan model syntax. See model.syntax for more information. Alternatively, a parameter table (e.g., the output of the lavParTable() 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 implies meanstructure = TRUE and std_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. If FALSE, 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. If FALSE, 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. If FALSE, 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 lavaan function.

sample_nobs

Number of observations. If a vector, multiple datasets are created. If return_type = "matrix" or return_type = "cov", a list of length(sample_nobs) is returned, with either the data or covariance matrices, each one based on the number of observations as specified in sample_nobs. If return_type = "data.frame", all datasets are merged and a group variable 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() and simulateData()) 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 on sample_nobs. Providing this argument (or a multilevel model syntax with level: 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. If FALSE, 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).

Examples

Run this code
# 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))

# sample moments
round(cov(myData), 3)
round(colMeans(myData), 3)

# 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)

Run the code above in your browser using DataLab