simulateData: Simulate Data From a Lavaan Model Syntax

Description Usage Arguments Details Value Examples

View source: R/lav_simulate_old.R

Description

Simulate data starting from a lavaan model syntax.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
simulateData(model = NULL, model.type = "sem", meanstructure = FALSE, 
    int.ov.free = TRUE, int.lv.free = FALSE, conditional.x = FALSE,
    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 = paste("G", 1:ngroups, sep = ""), skewness = NULL, 
    kurtosis = NULL, seed = NULL, empirical = FALSE, 
    return.type = "data.frame", return.fit = FALSE,
    debug = FALSE, standardized = FALSE)

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 (eg. the output of the lavaanify() 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.

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 refect both sets of variables. If "default", the value is set depending on the estimator, and whether or not the model involves categorical endogenous variables.

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 and the variances of exogenous latent variables are included in the model and 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.

seed

Set random seed.

empirical

Logical. If TRUE, the implied moments (Mu and Sigma) specify the empirical not population mean and covariance matrix.

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 such 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 unity by default. 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.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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 <- simulateData(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)

Example output

This is lavaan 0.5-23.1097
lavaan is BETA software! Please report any bugs.
$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

$mean
x1 x2 x3 x4 x5 x6 x7 x8 x9 
 0  0  0  0  0  0  0  0  0 

       x1     x2     x3    x4     x5     x6    x7     x8    x9
x1  1.923  0.864  0.893 0.023 -0.180 -0.220 0.341  0.084 0.543
x2  0.864  1.646  1.074 0.058 -0.179 -0.245 0.500  0.179 0.877
x3  0.893  1.074  2.246 0.008 -0.233 -0.192 0.470  0.219 0.668
x4  0.023  0.058  0.008 1.827  0.571  1.493 0.550  0.197 0.530
x5 -0.180 -0.179 -0.233 0.571  1.396  0.653 0.243 -0.101 0.052
x6 -0.220 -0.245 -0.192 1.493  0.653  3.614 0.900  0.367 0.941
x7  0.341  0.500  0.470 0.550  0.243  0.900 2.624  0.313 1.649
x8  0.084  0.179  0.219 0.197 -0.101  0.367 0.313  0.959 0.290
x9  0.543  0.877  0.668 0.530  0.052  0.941 1.649  0.290 2.315
    x1     x2     x3     x4     x5     x6     x7     x8     x9 
-0.105 -0.107 -0.120 -0.144 -0.167 -0.357  0.000 -0.110 -0.106 
lavaan (0.5-23.1097) converged normally after  38 iterations

  Number of observations                           100

  Estimator                                         ML
  Minimum Function Test Statistic               19.564
  Degrees of freedom                                24
  P-value (Chi-square)                           0.721

Parameter Estimates:

  Information                                 Expected
  Standard Errors                             Standard

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  f1 =~                                               
    x1                1.000                           
    x2                1.362    0.270    5.050    0.000
    x3                1.226    0.254    4.830    0.000
  f2 =~                                               
    x4                1.000                           
    x5                0.484    0.147    3.286    0.001
    x6                1.746    0.374    4.666    0.000
  f3 =~                                               
    x7                1.000                           
    x8                0.194    0.090    2.160    0.031
    x9                1.245    0.208    5.991    0.000

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)
  f3 ~                                                
    f1                0.809    0.222    3.644    0.000
    f2                0.577    0.165    3.491    0.000

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)
  f1 ~~                                               
    f2               -0.079    0.095   -0.831    0.406

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .x1                1.262    0.206    6.129    0.000
   .x2                0.438    0.174    2.512    0.012
   .x3                1.258    0.228    5.528    0.000
   .x4                0.959    0.210    4.562    0.000
   .x5                1.183    0.175    6.742    0.000
   .x6                0.985    0.501    1.964    0.049
   .x7                1.290    0.253    5.094    0.000
   .x8                0.900    0.128    7.025    0.000
   .x9                0.266    0.273    0.975    0.330
    f1                0.642    0.226    2.838    0.005
    f2                0.850    0.270    3.151    0.002
   .f3                0.678    0.201    3.377    0.001

lavaan documentation built on March 10, 2021, 5:05 p.m.

Related to simulateData in lavaan...