Skip to contents

Simulates data based on a specified slca model. If the model parameters are not already estimated, they can either be provided by the user or generated randomly.

Usage

# S3 method for class 'slca'
simulate(object, nsim = 500, seed = NULL, parm, nlevel, ...)

Arguments

object

an slca object representing the model from which data will be simulated.

nsim

a positive number specifying the number of response observations to simulate. Non-integer values are rounded up. The default is 500.

seed

an integer specifying the random seed for reproducibility. If not provided, results will vary across runs.

parm

a user-specified set of parameters to guide the simulation. This is required if the model has not been previously estimated. If the supplied length does not match the model, random parameters are used with a warning.

nlevel

an integer or integer vector specifying the number of levels for each manifest item in the model. If a single integer is provided, all manifest items use that number of levels. Named vectors may specify a subset of manifest items, with unspecified items using the default of 2.

...

Additional arguments passed to other methods.

Value

A list with the following components:

class

A data.frame containing the assigned latent class for each individual across all latent class variables.

response

A data.frame containing the simulated manifest item responses as factors. Internal response encodings used for model calculation are not stored on this object.

Examples

m1 <- slca(lc1[3] ~ x1 + x2 + x3 + x4 + x5,
              lc2[4] ~ y1 + y2 + y3 + y4 + y5)
sim <- simulate(m1, 1000)
sapply(sim$class, table)
#> $lc1
#> 
#>   1   2   3 
#> 287  83 630 
#> 
#> $lc2
#> 
#>   1   2   3   4 
#> 149 248 305 298 
#> 

# simulate data with defined number of levels of manifest items
m2 <- slca(lc1[3] ~ x1 + x2 + x3 + x4)
sim <- simulate(m2, nlevel = c(3, 3, 3, 3))
d <- sim$response
sapply(d, table)
#>    x1  x2  x3  x4
#> 1  93 160 232 202
#> 2 210 175 188 102
#> 3 197 165  80 196

sim <- simulate(m2, nlevel = c(x1 = 2, x3 = 3, x4 = 4))
d <- sim$response
sapply(d, table)
#> $x1
#> 
#>   1   2 
#> 330 170 
#> 
#> $x2
#> 
#>   1   2 
#> 127 373 
#> 
#> $x3
#> 
#>   1   2   3 
#> 126 132 242 
#> 
#> $x4
#> 
#>   1   2   3   4 
#> 191 143 131  35 
#> 

# simulate data with user-defined parameters
pi <- rep(1 / 3, 3)
rho <- c(.9, .1, .9, .1, .9, .1, .9, .1,
         .9, .1, .9, .1, .1, .9, .1, .9,
         .1, .9, .1, .9, .1, .9, .1, .9)
par <- c(pi, rho)
m3 <- slca(lc[3] ~ y1 + y2 + y3 + y4)
sim <- simulate(m3, parm = par)
mf <- estimate(m3, sim$response)
param(mf)
#> PI :
#> (lc)
#>   class
#>          1       2       3
#>     0.3432  0.4175  0.2393
#> 
#> RHO :
#> (a)
#>         class
#> response       1       2       3
#>    1(V1)  0.8687  0.1978  1.0000
#>    2      0.1313  0.8022  0.0000
#>    1(V2)  0.8885  0.1959  1.0000
#>    2      0.1115  0.8041  0.0000
#>    1(V3)  0.9479  0.0783  0.0000
#>    2      0.0521  0.9217  1.0000
#>    1(V4)  0.8877  0.1033  0.1597
#>    2      0.1123  0.8967  0.8403
#> 
#>    V1 V2 V3 V4
#> lc y1 y2 y3 y4