old files deleted, new files added
[openmx:openmx.git] / demo / TwoFactorModel_PathRaw.R
1 # -----------------------------------------------------------------------\r
2 # Program: TwoFactorModel_PathRaw.R  \r
3 #  Author: Ryne Estabrook\r
4 #    Date: 08 01 2009 \r
5 #\r
6 # Two Factor model to estimate factor loadings, residual variances and means\r
7 # Path style model input - Raw data input\r
8 #\r
9 # Revision History\r
10 #   Hermine Maes -- 10 08 2009 updated & reformatted\r
11 # -----------------------------------------------------------------------\r
12 \r
13 require(OpenMx)\r
14 \r
15 #Prepare Data\r
16 # -----------------------------------------------------------------------\r
17 data(myFADataRaw)\r
18 \r
19 twoFactorRaw <- myFADataRaw[,c("x1","x2","x3","y1","y2","y3")]\r
20 \r
21 #Create an MxModel object\r
22 # -----------------------------------------------------------------------\r
23 twoFactorModel <- mxModel("Two Factor Model -- Path Specification", \r
24         type="RAM",\r
25     mxData(\r
26         observed=twoFactorRaw, \r
27         type="raw"\r
28     ),\r
29     manifestVars=c("x1", "x2", "x3", "y1", "y2", "y3"),\r
30     latentVars=c("F1","F2"),\r
31     # residual variances\r
32     mxPath(\r
33         from=c("x1", "x2", "x3", "y1", "y2", "y3"),\r
34         arrows=2,\r
35         free=TRUE,\r
36         values=c(1,1,1,1,1,1),\r
37         labels=c("e1","e2","e3","e4","e5","e6")\r
38     ),\r
39     # latent variances and covaraince\r
40     mxPath(\r
41         from=c("F1","F2"),\r
42         arrows=2,\r
43         all=TRUE,\r
44         free=TRUE,\r
45         values=c(1, .5,\r
46                 .5, 1),\r
47         labels=c("varF1", "cov", "cov", "varF2")\r
48     ), \r
49     # factor loadings for x variables\r
50     mxPath(\r
51         from="F1",\r
52         to=c("x1","x2","x3"),\r
53         arrows=1,\r
54         free=c(FALSE,TRUE,TRUE),\r
55         values=c(1,1,1),\r
56         labels=c("l1","l2","l3")\r
57     ),\r
58     # factor loadings for y variables\r
59     mxPath(\r
60         from="F2",\r
61         to=c("y1","y2","y3"),\r
62         arrows=1,\r
63         free=c(FALSE,TRUE,TRUE),\r
64         values=c(1,1,1),\r
65         labels=c("l4","l5","l6")\r
66     ),\r
67     # means\r
68     mxPath(\r
69         from="one",\r
70         to=c("x1","x2","x3","y1","y2","y3","F1","F2"),\r
71         arrows=1,\r
72         free=c(T ,T, T, T, T, T, F, F),\r
73         values=c(1,1,1,1,1,1,0,0),\r
74         labels=c("meanx1","meanx2","meanx3",\r
75                  "meany1","meany2","meany3",\r
76                   NA,NA)\r
77     )\r
78 ) # close model\r
79       \r
80 twoFactorFit <- mxRun(twoFactorModel)\r
81 \r
82 summary(twoFactorFit)\r
83 twoFactorFit@output$estimate\r
84 \r
85 #Compare OpenMx results to Mx results \r
86 # -----------------------------------------------------------------------\r
87 omxCheckCloseEnough(twoFactorFit@output$estimate[["l2"]], 0.9723, 0.01)\r
88 omxCheckCloseEnough(twoFactorFit@output$estimate[["l3"]], 0.9313, 0.01)\r
89 omxCheckCloseEnough(twoFactorFit@output$estimate[["l5"]], 1.0498, 0.01)\r
90 omxCheckCloseEnough(twoFactorFit@output$estimate[["l6"]], 1.0531, 0.01)\r
91 omxCheckCloseEnough(twoFactorFit@output$estimate[["varF1"]], 0.6604, 0.01)\r
92 omxCheckCloseEnough(twoFactorFit@output$estimate[["varF2"]], 0.4505, 0.01)\r
93 omxCheckCloseEnough(twoFactorFit@output$estimate[["cov"]], 0.2952, 0.01)\r
94 omxCheckCloseEnough(twoFactorFit@output$estimate[["e1"]], 0.3349, 0.01)\r
95 omxCheckCloseEnough(twoFactorFit@output$estimate[["e2"]], 0.3985, 0.01)\r
96 omxCheckCloseEnough(twoFactorFit@output$estimate[["e3"]], 0.4091, 0.01)\r
97 omxCheckCloseEnough(twoFactorFit@output$estimate[["e4"]], 0.5404, 0.01)\r
98 omxCheckCloseEnough(twoFactorFit@output$estimate[["e5"]], 0.4809, 0.01)\r
99 omxCheckCloseEnough(twoFactorFit@output$estimate[["e6"]], 0.5571, 0.01)\r
100 omxCheckCloseEnough(twoFactorFit@output$estimate[["meanx1"]], 2.988, 0.01)\r
101 omxCheckCloseEnough(twoFactorFit@output$estimate[["meanx2"]], 3.0113, 0.01)\r
102 omxCheckCloseEnough(twoFactorFit@output$estimate[["meanx3"]], 2.9861, 0.01)\r
103 omxCheckCloseEnough(twoFactorFit@output$estimate[["meany1"]], 2.9554, 0.01)\r
104 omxCheckCloseEnough(twoFactorFit@output$estimate[["meany2"]], 2.9562, 0.01)\r
105 omxCheckCloseEnough(twoFactorFit@output$estimate[["meany3"]], 2.9673, 0.01)\r