# A simple Example: 1 factor Confirmatory Factor Analysis
library(OpenMx)
data(demoOneFactor)
manifests <- names(demoOneFactor)
latents <- c("G")
factorModel <- mxModel(model="One Factor", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from=latents, to=manifests),
mxPath(from=manifests, arrows=2),
mxPath(from=latents, arrows=2,free=FALSE, values=1.0),
mxData(cov(demoOneFactor), type="cov",numObs=500)
)
factorFit <-mxRun(factorModel)
summary(factorFit)
# A more complex example using features of R to compress
# what would otherwise be a long and error-prone script
# list of 100 variable names: "01" "02" "03"...
myManifest <- sprintf("%02d", c(1:100))
# the latent variables for the model
myLatent <- c("G1", "G2", "G3", "G4", "G5")
# Start building the model:
# Define its type, and add the manifest and latent variable name lists
testModel <- mxModel(model="testModel6", type = "RAM",
manifestVars = myManifest, latentVars = myLatent)
# Create covariances between the latent variables and add to the model
# Here we use combn to create the covariances
# nb: To create the variances and covariances in one operation you could use
# expand.grid(myLatent,myLatent) to specify from and to
uniquePairs <- combn(myLatent,2)
covariances <- mxPath(from = uniquePairs[1,],
to=uniquePairs[2,], arrows = 2, free = TRUE, values = 1)
testModel <- mxModel(model=testModel, covariances)
# Create variances for the latent variables
variances <- mxPath(from = myLatent,
to=myLatent, arrows = 2, free = TRUE, values = 1)
testModel <- mxModel(model=testModel, variances) # add variances to the model
# Make a list of paths from each packet of 20 manifests
# to one of the 5 latent variables
# nb: The first loading to each latent is fixed to 1 to scale its variance.
singles <- list()
for (i in 1:5) {
j <- i*20
singles <- append(singles, mxPath(
from = myLatent[i], to = myManifest[(j - 19):j],
arrows = 1,
free = c(FALSE, rep(TRUE, 19)),
values = c(1, rep(0.75, 19))))
}
# add single-headed paths to the model
testModel <- mxModel(model=testModel, singles)
## Suppose you want to have a one-headed path AND a two-headed path
## between the same pair of variables in a RAM model?
# One approach: use "dummy" latent variables:
mvars <- c("x1", "x2") #<--Manifest variables.
lvars <- c("dummy1","dummy2") #<--Latent variables.
model <- mxModel(
type = "RAM",
manifestVars=mvars,
latentVars=lvars,
# One-headed path from 'x1' to 'x2', freely estimated:
mxPath(
from = "x1", to = "x2",
arrows = 1,
labels = "b_x2_x1", values=0.1),
# Each latent variable "causes" its corresponding manifest variable:
mxPath(from=lvars,to=mvars,arrows=1,free=FALSE,values=1),
# Each latent variable has its variance fixed to 1.0:
mxPath(from=lvars,arrows=2,free=FALSE,values=1),
# The covariance between the two latent variables is freely estimated;
# it is also the covariance between the two manifest variables:
mxPath(from=lvars[1],to=lvars[2],arrows=2,free=TRUE,values=0.1)
)
if (FALSE) {
# Another approach: directly modify matrices:
# Will raise a warning:
model <- mxModel(
type = "RAM",
manifestVars = c("x1", "x2"),
mxPath(
from = "x1", to = "x2",
arrows = 1,
labels = "b_x2_x1"),
mxPath(
from = "x1",
to = "x2",
arrows = 2,
labels = "v_x2_x1")
)
coef(model) #<--Only the two-headed path is there.
# Directly modify the MxModel's 'A' matrix:
model$A$free["x2","x1"] <- TRUE
model$A$labels["x2","x1"] <- "b_x2_x1"
# Alternately:
# Will raise a warning:
model <- mxModel(
type = "RAM",
manifestVars = c("x1", "x2"),
mxPath(
from = "x1",
to = "x2",
arrows = 2,
labels = "v_x2_x1"),
mxPath(
from = "x1", to = "x2",
arrows = 1,
labels = "b_x2_x1")
)
coef(model) #<--Only the one-headed path is there.
# Directly modify the MxModel's 'S' matrix:
model$S$free["x2","x1"] <- TRUE
model$S$labels["x2","x1"] <- "v_x2_x1"
model$S$free["x1","x2"] <- TRUE
model$S$labels["x1","x2"] <- "v_x2_x1"
#^^^The 'S' matrix is symmetric, so so if you modify an off-diagonal element of 'S',
# you have to likewise modify the corresponding element on the other side of the diagonal.
}
Run the code above in your browser using DataLab