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