# NOT RUN {
## Specify population values. Note every parameter has a fixed value.
modelP <- '
f1 =~ .7*V1 + .7*V2 + .7*V3 + .7*V4
f2 =~ .7*V5 + .7*V6 + .7*V7 + .7*V8
f1 ~~ .3*f2
f1 ~~ 1*f1
f2 ~~ 1*f2
V1 ~~ .51*V1
V2 ~~ .51*V2
V3 ~~ .51*V3
V4 ~~ .51*V4
V5 ~~ .51*V5
V6 ~~ .51*V6
V7 ~~ .51*V7
V8 ~~ .51*V8
'
## Specify analysis model. Note parameter of interest f1~~f2 is fixed to 0.
modelA <- '
f1 =~ V1 + V2 + V3 + V4
f2 =~ V5 + V6 + V7 + V8
f1 ~~ 0*f2
'
## Calculate power
SSpower(powerModel = modelA, popModel = modelP, n = 150, nparam = 1,
std.lv = TRUE)
## Get power for a range of sample sizes
Ns <- seq(100, 500, 40)
Power <- rep(NA, length(Ns))
for(i in 1:length(Ns)) {
Power[i] <- SSpower(powerModel = modelA, popModel = modelP,
n = Ns[i], nparam = 1, std.lv = TRUE)
}
plot(x = Ns, y = Power, type = "l", xlab = "Sample Size")
## Optionally specify different values for multiple populations
modelP2 <- '
f1 =~ .7*V1 + .7*V2 + .7*V3 + .7*V4
f2 =~ .7*V5 + .7*V6 + .7*V7 + .7*V8
f1 ~~ c(-.3, .3)*f2 # DIFFERENT ACROSS GROUPS
f1 ~~ 1*f1
f2 ~~ 1*f2
V1 ~~ .51*V1
V2 ~~ .51*V2
V3 ~~ .51*V3
V4 ~~ .51*V4
V5 ~~ .51*V5
V6 ~~ .51*V6
V7 ~~ .51*V7
V8 ~~ .51*V8
'
modelA2 <- '
f1 =~ V1 + V2 + V3 + V4
f2 =~ V5 + V6 + V7 + V8
f1 ~~ c(psi21, psi21)*f2 # EQUALITY CONSTRAINT ACROSS GROUPS
'
## Calculate power
SSpower(powerModel = modelA2, popModel = modelP2, n = c(100, 100), nparam = 1,
std.lv = TRUE)
## Get power for a range of sample sizes
Ns2 <- cbind(Group1 = seq(10, 100, 10), Group2 = seq(10, 100, 10))
Power2 <- apply(Ns2, MARGIN = 1, FUN = function(nn) {
SSpower(powerModel = modelA2, popModel = modelP2, n = nn,
nparam = 1, std.lv = TRUE)
})
plot(x = rowSums(Ns2), y = Power2, type = "l", xlab = "Total Sample Size",
ylim = 0:1)
abline(h = c(.8, .9), lty = c("dotted","dashed"))
legend("bottomright", c("80% Power","90% Power"), lty = c("dotted","dashed"))
# }
Run the code above in your browser using DataLab