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.67567512738
#> 
#> 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.798644787140878,0.907904236613599,0.803838142013948,0.900396358534138,0.0423202738274926,0.0419256359832653,0.0370993849926502,0.0410547496618837,0.041974968940025,0.0377186342526869,0.983486182403335,0.248565838931859,0.0132141364668449,0.00300610847335311,0.0101745706855508,-0.00432472158982143,-0.00060509160768298,-0.00278202909173598,0.679955375785379,-0.172459318949147

omxGetParameters(mod)
#>         xi→x2         xi→x3        eta→y2        eta→y3         x1↔x1 
#>  0.7986447871  0.9079042366  0.8038381420  0.9003963585  0.0423202738 
#>         x2↔x2         x3↔x3         y1↔y1         y2↔y2         y3↔y3 
#>  0.0419256360  0.0370993850  0.0410547497  0.0419749689  0.0377186343 
#>         xi↔xi       eta↔eta        one→x1        one→x2        one→x3 
#>  0.9834861824  0.2485658389  0.0132141365  0.0030061085  0.0101745707 
#>        one→y1        one→y2        one→y3            a0            a1 
#> -0.0043247216 -0.0006050916 -0.0027820291  0.6799553758 -0.1724593189