1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#
# Copyright 2007-2019 by the individuals mentioned in the source code history
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
# Program: LatentGrowthModel_PathRaw_ModelRec.R
# Author: Michael Hunter
# Date: 2011.07.22
#
# ModelType: Growth Curve
# DataType: Longitudinal
# Field: None
#
# Purpose:
# Latent Growth model to estimate means and
# (co)variances of slope and intercept
# Path style model input - Raw data input
# Recursive
#
# RevisionHistory:
# Michael Hunter -- 2011.07.22 Took template from Ryne Estabrook's LatentGrowthModel_PathRaw.R
#
# -----------------------------------------------------------------------------
require(OpenMx)
# Load Library
# -----------------------------------------------------------------------------
data(myLongitudinalData)
# Prepare Data
# -----------------------------------------------------------------------------
growthCurveModel <- mxModel(
name="Linear Growth Curve Model Path Specification",
type="RAM",
mxData(
observed=myLongitudinalData,
type="raw"
),
manifestVars=c("x1","x2","x3","x4","x5"),
latentVars=c("intercept","slope")
)
growthCurveModel <- mxModel(
model = growthCurveModel,
# residual variances
mxPath(
from=c("x1","x2","x3","x4","x5"),
arrows=2,
free=TRUE,
values = c(1, 1, 1, 1, 1),
labels=c("residual","residual","residual","residual","residual")
)
)
growthCurveModel <- mxModel(
model = growthCurveModel,
# latent variances and covariance
mxPath(
from=c("intercept","slope"),
arrows=2,
connect="unique.pairs",
free=TRUE,
values=c(1, 1, 1),
labels=c("vari", "cov", "vars")
)
)
growthCurveModel <- mxModel(
model = growthCurveModel,
# intercept loadings
mxPath(
from="intercept",
to=c("x1","x2","x3","x4","x5"),
arrows=1,
free=FALSE,
values=c(1, 1, 1, 1, 1)
)
)
growthCurveModel <- mxModel(
model= growthCurveModel,
# slope loadings
mxPath(
from="slope",
to=c("x1","x2","x3","x4","x5"),
arrows=1,
free=FALSE,
values=c(0, 1, 2, 3, 4)
)
)
growthCurveModel <- mxModel(
model = growthCurveModel,
# manifest means
mxPath(from="one",
to=c("x1", "x2", "x3", "x4", "x5"),
arrows=1,
free=FALSE,
values=c(0, 0, 0, 0, 0)
)
)
growthCurveModel <- mxModel(
model = growthCurveModel,
# latent means
mxPath(from="one",
to=c("intercept", "slope"),
arrows=1,
free=TRUE,
values=c(1, 1),
labels=c("meani", "means")
)
)
# -----------------------------------------------------------------------------
growthCurveFit <- mxRun(growthCurveModel, suppressWarnings=TRUE)
summary(growthCurveFit)
coef(growthCurveFit)
omxCheckCloseEnough(coef(growthCurveFit)[["meani"]], 9.930, 0.01)
omxCheckCloseEnough(coef(growthCurveFit)[["means"]], 1.813, 0.01)
omxCheckCloseEnough(coef(growthCurveFit)[["vari"]], 3.886, 0.01)
omxCheckCloseEnough(coef(growthCurveFit)[["vars"]], 0.258, 0.01)
omxCheckCloseEnough(coef(growthCurveFit)[["cov"]], 0.460, 0.01)
omxCheckCloseEnough(coef(growthCurveFit)[["residual"]], 2.316, 0.01)
# Compare OpenMx results to Mx results
# -----------------------------------------------------------------------------
|