require("OpenMx")
## Loading required package: OpenMx
require(semPlot)
## Loading required package: semPlot This is semPlot 0.3.3 semPlot is BETA
## software! Please report any bugs.
cormat = matrix(c(1.00, 0.60, 0.30, 0.20, 0.20,
0.60, 1.00, 0.20, 0.30, 0.10,
0.30, 0.20, 1.00, 0.70, 0.60,
0.20, 0.30, 0.70, 1.00, 0.50,
0.20, 0.10, 0.60, 0.50, 1.00), ncol=5, byrow=TRUE)
# where
obs.var = c('Ach1', 'Ach2', 'Amb1', 'Amb2', 'Amb3')
dimnames(cormat) <- list(obs.var, obs.var)
#Create an MxModel object
modelAA <- mxModel("Model",
type="RAM",
mxData(observed=cormat,type="cor", numObs=500),
manifestVars=obs.var,
latentVars=c("Achievement", "Ambition"),
# residual variances
mxPath(from=obs.var,
arrows=2,
free=TRUE,
values=c(1,1,1,1,1),
labels=c("e1","e2","e3","e4","e5")
),
# latent variance
mxPath(from="Achievement",
arrows=2,
free=FALSE,
values=1,
labels ="varAch"
),
# latent variance
mxPath(from="Ambition",
arrows=2,
free=FALSE,
values=1,
labels ="varAmb"
),
# latent covariance
mxPath(from="Achievement",
to="Ambition",
arrows=2,
free=TRUE,
values=1,
labels ="covAA"
),
# factor loadings
mxPath(from="Achievement",
to=obs.var[1:2],
arrows=1,
free=c(TRUE,TRUE),
values=c(1,1),
labels =c("l1","l2")
),
# factor loadings
mxPath(from="Ambition",
to=obs.var[3:5],
arrows=1,
free=c(TRUE,TRUE,TRUE),
values=c(1,1,1),
labels =c("l3","l4","l5")
)
) # close model
fit <- mxRun(modelAA)
## Running Model
fit@output$estimate
## l1 l2 l3 l4 l5 e1 e2 e3 e4 e5
## 0.8788 0.6827 0.9204 0.7613 0.6519 0.2277 0.5339 0.1529 0.4204 0.5750
## covAA
## 0.3558
semPaths(fit)
I modeled them at the same time:
#Create an MxModel object
modelAA2 <- mxModel("Model",
type="RAM",
mxData(observed=cormat,type="cor", numObs=500),
manifestVars=obs.var,
latentVars=c("Achievement", "Ambition"),
# residual variances
mxPath(from=obs.var,
arrows=2,
free=TRUE,
values=c(1,1,1,1,1),
labels=c("e1","e2","e3","e4","e5")
),
# latent variance
mxPath(from="Achievement",
arrows=2,
free=FALSE,
values=1,
labels ="varAch"
),
# latent variance
mxPath(from="Ambition",
arrows=2,
free=FALSE,
values=1,
labels ="varAmb"
),
# factor loadings
mxPath(from="Achievement",
to=obs.var[1:2],
arrows=1,
free=c(TRUE,TRUE),
values=c(1,1),
labels =c("l1","l2")
),
# factor loadings
mxPath(from="Ambition",
to=obs.var[3:5],
arrows=1,
free=c(TRUE,TRUE,TRUE),
values=c(1,1,1),
labels =c("l3","l4","l5")
)
) # close model
fit2 <- mxRun(modelAA2)
## Running Model
fit2@output$estimate
## l1 l2 l3 l4 l5 e1 e2 e3 e4 e5
## 0.7746 0.7746 0.9165 0.7638 0.6547 0.4000 0.4000 0.1600 0.4167 0.5714
semPaths(fit2)
We modify the data, and model both parts at once, but are only interested in Ambition:
cormat = matrix(c(1.00, 0.60, 0.30, 0.20, 0.20,
0.60, 1.00, 0.20, 0.30, 0.10,
0.30, 0.20, 1.00, 0.00, 0.60,
0.20, 0.30, 0.00, 1.00, 0.50,
0.20, 0.10, 0.60, 0.50, 1.00), ncol=5, byrow=TRUE)
# where
obs.var = c('Ach1', 'Ach2', 'Amb1', 'Amb2', 'Amb3')
dimnames(cormat) <- list(obs.var, obs.var)
#Create an MxModel object
modelAA <- mxModel("Model",
type="RAM",
mxData(observed=cormat,type="cor", numObs=500),
manifestVars=obs.var,
latentVars=c("Achievement", "Ambition"),
# residual variances
mxPath(from=obs.var,
arrows=2,
free=TRUE,
values=c(1,1,1,1,1),
labels=c("e1","e2","e3","e4","e5")
),
# latent variance
mxPath(from="Achievement",
arrows=2,
free=FALSE,
values=1,
labels ="varAch"
),
# latent variance
mxPath(from="Ambition",
arrows=2,
free=FALSE,
values=1,
labels ="varAmb"
),
# latent covariance
mxPath(from="Achievement",
to="Ambition",
arrows=2,
free=TRUE,
values=1,
labels ="covAA"
),
# factor loadings
mxPath(from="Achievement",
to=obs.var[1:2],
arrows=1,
free=c(TRUE,TRUE),
values=c(1,1),
labels =c("l1","l2")
),
# factor loadings
mxPath(from="Ambition",
to=obs.var[3:5],
arrows=1,
free=c(TRUE,TRUE,TRUE),
values=c(1,1,1),
labels =c("l3","l4","l5")
)
) # close model
fit3 <- mxRun(modelAA)
## Running Model
## Warning: In model 'Model' NPSOL returned a non-zero status code 4. The
## major iteration limit was reached (Mx status BLUE).