# Create and run the 1-factor CFA on the openmx.ssri.psu.edu front page
# 1. Load OpenMx and the demoOneFactor dataframe
library(OpenMx)
data(demoOneFactor)
# 2. Define the manifests (5 demo variables) and latents for use in the model
manifests <- names(demoOneFactor)
latents <- c("G")
# 3. Build the model, adding paths and data
model <- mxModel(model="One Factor", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from=latents, to=manifests, labels=paste("b", 1:5, sep="")),
mxPath(from=manifests, arrows=2, labels=paste("u", 1:5, sep="")),
mxPath(from=latents , arrows=2, free=FALSE, values=1.0),
mxData(cov(demoOneFactor), type="cov", numObs=500)
)
# 4. Run the model, returning the result into model
model <- mxRun(model)
# 5. Show a summary of the fitted model and parameter values
summary(model)
# 6. Print SE-based CIs for the fitted parameter values
confint(model)
# 7. Add likelihood-based CIs to the model and run these
model = mxModel(model, mxCI(paste0("b", 1:5)))
model <- mxRun(model, intervals = TRUE)
summary(model)$CI
# lbound estimate ubound note
# b1 0.3675940 0.3967545 0.4285895
# b2 0.4690663 0.5031569 0.5405838
# b3 0.5384588 0.5766635 0.6186705
# b4 0.6572678 0.7020702 0.7514609
# b5 0.7457231 0.7954529 0.8503486
# 8. Demonstrate mxTryHard
model <- mxTryHard(model, intervals = TRUE)
Run the code above in your browser using DataLab