Muthen and Muthen (2002) CFA example (original) (raw)
#-------------------------------------------------------------------
library(SimDesign)
Design <- createDesign(N=150, fcor=.25, loadings=.8, residuals=.36)
#-------------------------------------------------------------------
Generate <- function(condition, fixed_objects = NULL) {
Attach(condition)
population.model <-
sprintf('
f1 =~ %s
f2 =~ %s
f1 ~~ 1*f1
f2 ~~ 1*f2
f1 ~~ %f*f2
%s', paste0(loadings, "*x", 1:5, collapse=' + '),
paste0(loadings, "*x", 6:10, collapse=' + '),
fcor,
paste0(paste0('x', 1:10, " ~~ "),
paste0(residuals, "*x", 1:10, '\n'), collapse=''))
# cat(population.model)
dat <- simulateData(population.model, sample.nobs=N)
dat
}
Analyse <- function(condition, dat, fixed_objects = NULL) {
myModel <- '
f1 =~ NA*x1 + x2 + x3 + x4 + x5
f2 =~ NA*x6 + x7 + x8 + x9 + x10
f1 ~~ 1*f1
f2 ~~ 1*f2'
mod <- sem(myModel, data=dat)
ests <- parameterEstimates(mod)
ret <- with(ests, pvalue[paste0(lhs, op, rhs) == "f1~~f2"])
ret
}
Summarise <- function(condition, results, fixed_objects = NULL) {
EDR(results, alpha = .05)
}
#-------------------------------------------------------------------
# original result from Muthen and Muthen (not run, but gives power ~= .81)
# res <- runSimulation(design=Design, replications=50000, generate=Generate,
# analyse=Analyse, summarise=Summarise, packages='lavaan', parallel=TRUE)
# res
#-------------------------------------------------------------------
Design <- createDesign(N=NA, fcor=.25, loadings=.8, residuals=.36)
solved <- SimSolve(design=Design, b=.8, interval=c(100,300), generate=Generate,
analyse=Analyse, summarise=Summarise, packages='lavaan',
parallel=TRUE, predCI.tol=.01, verbose=FALSE)
This is lavaan 0.6-19
lavaan is FREE software! Please report any bugs.
# A tibble: 1 × 4
N fcor loadings residuals
<dbl> <dbl> <dbl> <dbl>
1 143.66 0.25 0.8 0.36
$root
[1] 143.66
$predCI.root
CI_2.5 CI_97.5
142.2862 145.0107
$b
[1] 0.8
$predCI.b
[1] 0.7951075 0.8048044
$terminated_early
[1] TRUE
$time
[1] 02m 40.32s
$iterations
[1] 94
$total.replications
[1] 29900
$tab
y x reps
9 0.7849206 139 1260
10 0.8036765 140 1360
11 0.7966667 141 1200
12 0.7808696 142 1150
13 0.7997519 143 4030
14 0.7965194 144 7470
15 0.8017825 145 5610
16 0.8151210 146 4960
17 0.8222222 148 1080
19 0.8222222 150 540
plot(solved, type = 'history')