library(simsem)
### NESTED MODEL
# factor loadings
loading <- matrix(0, 6, 2)
loading[1, 1] <- "l1"
loading[2:3, 1] <- c("l2", "l3")
loading[4, 2] <- "l1"
loading[5:6, 2] <- c("l2", "l3")
LY <- bind(loading, "runif(1, 0.5, 0.8)")
LY
# factor variances
VE <- bind(c(1, 0, 1))
VE
# factor correlations
facCor <- matrix(NA, 3, 3)
diag(facCor) <- 1
facCorVal <- diag(3)
facCorVal[1, 2] <- facCorVal[2, 1] <- "runif(1, 0.7, 0.95)" # pre~post
facCorVal[2, 3] <- facCorVal[3, 2] <- "runif(1, 0.4, 0.7)" # lc~post
facCorVal[1, 3] <- facCorVal[3, 1] <- "runif(1, -0.2, 0.2)" # LC~~Pre
RPS <- binds(facCor, facCorVal)
RPS
# mean structure
ALnested <- bind(c(0, 0, 0))
ALnested
# equal intercepts
inter <- rep(NA, 6)
inter[1] <- inter[4] <- "int1"
inter[2] <- inter[5] <- "int2"
inter[3] <- inter[6] <- "int3"
TY <- bind(inter, 1)
TY
# residual variances across time
resvar <- rep(NA, 6)
resvar[1] <- resvar[4] <- "res1"
resvar[2] <- resvar[5] <- "res2"
resvar[3] <- resvar[6] <- "res3"
VTE <- bind(resvar, "runif(1, 0.4, 0.6)")
VTE
# residual covariances across time
error <- diag(6)
error[1, 4] <- error[4, 1] <- "res1"
error[2, 5] <- error[5, 2] <- "res2"
error[3, 6] <- error[6, 3] <- "res3"
errorVal <- diag(6)
errorVal[1, 4] <- errorVal[4, 1] <- "runif(1, 0.4, 0.6)"
errorVal[2, 5] <- errorVal[5, 2] <- "runif(1, 0.4, 0.6)"
errorVal[3, 6] <- errorVal[6, 3] <- "runif(1, 0.4, 0.6)"
RTE <- binds(error, errorVal)
RTE
# generate nested model
lcsmNested <- model(LY = LY,
RPS = RPS,
VE = VE,
AL = ALnested,
TY = TY,
VTE = VTE,
RTE = RTE,
modelType = "CFA")
### PARENT MODEL
# mean structure
ALparent <- bind(c(0, 0, "runif(1, 0.8, 1.2)"))
ALparent
# generate parent model
lcsmParent <- model(LY = LY,
RPS = RPS,
VE = VE,
AL = ALparent,
TY = TY,
VTE = VTE,
RTE = RTE,
modelType = "CFA")
# fit nested and parent model on data from nested model
outDatNestedModNested <- sim(1000, n = 200, lcsmNested, generate = lcsmNested)
outDatNestedModParent <- sim(1000, n = 200, lcsmParent, generate = lcsmNested)
anova(outDatNestedModNested, outDatNestedModParent)