Update demos directory to reflect new style header and new commenting style.
[openmx:openmx.git] / demo / TwoFactorModel_PathRaw.R
1 #\r
2 #   Copyright 2007-2010 The OpenMx Project\r
3 #\r
4 #   Licensed under the Apache License, Version 2.0 (the "License");\r
5 #   you may not use this file except in compliance with the License.\r
6 #   You may obtain a copy of the License at\r
7\r
8 #        http://www.apache.org/licenses/LICENSE-2.0\r
9\r
10 #   Unless required by applicable law or agreed to in writing, software\r
11 #   distributed under the License is distributed on an "AS IS" BASIS,\r
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 #   See the License for the specific language governing permissions and\r
14 #   limitations under the License.\r
15 \r
16 \r
17 # -----------------------------------------------------------------------------\r
18 # Program: TwoFactorModel_PathRaw.R  \r
19 # Author: Ryne Estabrook\r
20 # Date: 2009.08.01 \r
21 #\r
22 # ModelType: Factor\r
23 # DataType: Continuous\r
24 # Field: None\r
25 #\r
26 # Purpose: \r
27 #       Two Factor model to estimate factor loadings, residual variances and means\r
28 #       Path style model input - Raw data input\r
29 #\r
30 # RevisionHistory:\r
31 #       Hermine Maes -- 2009.10.08 updated & reformatted\r
32 #       Ross Gore -- 2011.06.06 added Model, Data & Field metadata\r
33 # -----------------------------------------------------------------------------\r
34 \r
35 require(OpenMx)\r
36 # Load Library\r
37 # -----------------------------------------------------------------------------\r
38 \r
39 data(myFADataRaw)\r
40 # Prepare Data\r
41 # -----------------------------------------------------------------------------\r
42 \r
43 twoFactorRaw <- myFADataRaw[,c("x1","x2","x3","y1","y2","y3")]\r
44 \r
45 twoFactorModel <- mxModel("Two Factor Model Path Specification", \r
46         type="RAM",\r
47     mxData(\r
48         observed=twoFactorRaw, \r
49         type="raw"\r
50     ),\r
51     manifestVars=c("x1", "x2", "x3", "y1", "y2", "y3"),\r
52     latentVars=c("F1","F2"),\r
53     mxPath(\r
54         from=c("x1", "x2", "x3", "y1", "y2", "y3"),\r
55         arrows=2,\r
56         free=TRUE,\r
57         values=c(1,1,1,1,1,1),\r
58         labels=c("e1","e2","e3","e4","e5","e6")\r
59     ),\r
60     # residual variances\r
61         # -------------------------------------\r
62     mxPath(\r
63         from=c("F1","F2"),\r
64         arrows=2,\r
65         all=TRUE,\r
66         free=TRUE,\r
67         values=c(1, .5,\r
68                 .5, 1),\r
69         labels=c("varF1", "cov", "cov", "varF2")\r
70     ), \r
71     # latent variances and covaraince\r
72         # -------------------------------------\r
73     mxPath(\r
74         from="F1",\r
75         to=c("x1","x2","x3"),\r
76         arrows=1,\r
77         free=c(FALSE,TRUE,TRUE),\r
78         values=c(1,1,1),\r
79         labels=c("l1","l2","l3")\r
80     ),\r
81     # factor loadings for x variables\r
82         # -------------------------------------   \r
83     mxPath(\r
84         from="F2",\r
85         to=c("y1","y2","y3"),\r
86         arrows=1,\r
87         free=c(FALSE,TRUE,TRUE),\r
88         values=c(1,1,1),\r
89         labels=c("l4","l5","l6")\r
90     ),\r
91         # factor loadings for y variables\r
92         # ------------------------------------- \r
93     mxPath(\r
94         from="one",\r
95         to=c("x1","x2","x3","y1","y2","y3","F1","F2"),\r
96         arrows=1,\r
97         free=c(T ,T, T, T, T, T, F, F),\r
98         values=c(1,1,1,1,1,1,0,0),\r
99         labels=c("meanx1","meanx2","meanx3",\r
100                  "meany1","meany2","meany3",\r
101                   NA,NA)\r
102     )\r
103         # means\r
104         # -------------------------------------\r
105 ) # close model\r
106 # Create an MxModel object\r
107 # -----------------------------------------------------------------------------\r
108       \r
109 twoFactorFit <- mxRun(twoFactorModel)\r
110 \r
111 summary(twoFactorFit)\r
112 twoFactorFit@output$estimate\r
113 \r
114 omxCheckCloseEnough(twoFactorFit@output$estimate[["l2"]], 0.9723, 0.01)\r
115 omxCheckCloseEnough(twoFactorFit@output$estimate[["l3"]], 0.9313, 0.01)\r
116 omxCheckCloseEnough(twoFactorFit@output$estimate[["l5"]], 1.0498, 0.01)\r
117 omxCheckCloseEnough(twoFactorFit@output$estimate[["l6"]], 1.0531, 0.01)\r
118 omxCheckCloseEnough(twoFactorFit@output$estimate[["varF1"]], 0.6604, 0.01)\r
119 omxCheckCloseEnough(twoFactorFit@output$estimate[["varF2"]], 0.4505, 0.01)\r
120 omxCheckCloseEnough(twoFactorFit@output$estimate[["cov"]], 0.2952, 0.01)\r
121 omxCheckCloseEnough(twoFactorFit@output$estimate[["e1"]], 0.3349, 0.01)\r
122 omxCheckCloseEnough(twoFactorFit@output$estimate[["e2"]], 0.3985, 0.01)\r
123 omxCheckCloseEnough(twoFactorFit@output$estimate[["e3"]], 0.4091, 0.01)\r
124 omxCheckCloseEnough(twoFactorFit@output$estimate[["e4"]], 0.5404, 0.01)\r
125 omxCheckCloseEnough(twoFactorFit@output$estimate[["e5"]], 0.4809, 0.01)\r
126 omxCheckCloseEnough(twoFactorFit@output$estimate[["e6"]], 0.5571, 0.01)\r
127 omxCheckCloseEnough(twoFactorFit@output$estimate[["meanx1"]], 2.988, 0.01)\r
128 omxCheckCloseEnough(twoFactorFit@output$estimate[["meanx2"]], 3.0113, 0.01)\r
129 omxCheckCloseEnough(twoFactorFit@output$estimate[["meanx3"]], 2.9861, 0.01)\r
130 omxCheckCloseEnough(twoFactorFit@output$estimate[["meany1"]], 2.9554, 0.01)\r
131 omxCheckCloseEnough(twoFactorFit@output$estimate[["meany2"]], 2.9562, 0.01)\r
132 omxCheckCloseEnough(twoFactorFit@output$estimate[["meany3"]], 2.9673, 0.01)\r
133 # Compare OpenMx results to Mx results \r
134 # -----------------------------------------------------------------------------