Skip to contents

simulate data for a moderated nonlinear factor analysis.

Usage

simulate_moderated_nonlinear_factor_analysis(N)

Arguments

N

sample size

Value

data set with variables x1-x3 and y1-y3 representing repeated measurements of an affect measure. It is assumed that the autoregressive effect is different depending on covariate k

Examples

library(mxsem)
set.seed(123)
dataset <- simulate_moderated_nonlinear_factor_analysis(N = 2000)

model <- "
xi =~ x1 + x2 + x3
eta =~ y1 + y2 + y3
eta ~ a*xi

# we need two new parameters: a0 and a1. These are created as follows:
!a0
!a1
# Now, we redefine a to be a0 + k*a1, where k is found in the data
a := a0 + data.k*a1
"

mod <- mxsem(model = model,
             data = dataset) |>
  mxTryHard()
#> Running untitled32 with 20 parameters
#> 
#> Beginning initial fit attempt
#> Running untitled32 with 20 parameters
#> 
#>  Lowest minimum so far:  9354.67567511442
#> 
#> Solution found
#> 


#> 
#>  Solution found!  Final fit=9354.6757 (started at 63250.836)  (1 attempt(s): 1 valid, 0 errors)
#>  Start values from best fit:
#> 0.798644439069186,0.907904041125705,0.803838161335694,0.90039637675525,0.0423203089534444,0.0419257504471055,0.0370993727609707,0.0410548523456912,0.0419748898902563,0.0377187198122035,0.983487233943373,0.248565811440238,0.0132158323116108,0.00300763175648384,0.0101763456547476,-0.0043236584301499,-0.000604331921656022,-0.00278126962209998,0.679954707463475,-0.17245961832583

omxGetParameters(mod)
#>         xi→x2         xi→x3        eta→y2        eta→y3         x1↔x1 
#>  0.7986444391  0.9079040411  0.8038381613  0.9003963768  0.0423203090 
#>         x2↔x2         x3↔x3         y1↔y1         y2↔y2         y3↔y3 
#>  0.0419257504  0.0370993728  0.0410548523  0.0419748899  0.0377187198 
#>         xi↔xi       eta↔eta        one→x1        one→x2        one→x3 
#>  0.9834872339  0.2485658114  0.0132158323  0.0030076318  0.0101763457 
#>        one→y1        one→y2        one→y3            a0            a1 
#> -0.0043236584 -0.0006043319 -0.0027812696  0.6799547075 -0.1724596183