Updated copyright to 2013 for R/ demo/ models/passing and src/ folders, and also...
[openmx:openmx.git] / demo / MultipleRegression_PathRaw.R
1 #
2 #   Copyright 2007-2013 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 require(OpenMx)
17
18 # -----------------------------------------------------------------------------
19 # Program: MultipleRegression_PathRaw.R  
20 # Author: Ryne Estabrook
21 # Date: 2009.08.01 
22 #
23 # ModelType: Regression
24 # DataType: Continuous
25 # Field: None
26 #
27 # Purpose:
28 #      Multiple Regression model to estimate effect of independent on dependent variables
29 #      Path style model input - Raw data input
30 #
31 # RevisionHistory:
32 #      Hermine Maes -- 2009.10.08 updated & reformatted
33 #      Ross Gore -- 2011.06.15 added Model, Data & Field metadata
34 # -----------------------------------------------------------------------------
35
36 require(OpenMx)
37 # Load Library
38 # -----------------------------------------------------------------------------
39
40 data(myRegDataRaw)
41
42 myRegDataRaw<-myRegDataRaw[,c("x","y","z")]
43 # Prepare Data
44 # -----------------------------------------------------------------------------
45
46 multiRegModel <- mxModel("Multiple Regression Path Specification", 
47       type="RAM",
48       mxData(
49           observed=myRegDataRaw, 
50           type="raw"
51       ),
52       manifestVars=c("x", "y", "z"),
53       mxPath(
54           from=c("x", "y", "z"), 
55           arrows=2,
56           free=TRUE, 
57           values = c(1, 1, 1),
58           labels=c("varx", "residual", "varz")
59       ),
60       # variance paths
61           # -------------------------------------       
62       mxPath(
63           from="x",
64           to="z",
65           arrows=2,
66           free=TRUE,
67           values=0.5,
68           labels="covxz"
69       ), 
70       # covariance of x and z
71           # -------------------------------------
72       mxPath(
73           from=c("x","z"),
74           to="y",
75           arrows=1,
76           free=TRUE,
77           values=1,
78           labels=c("betax","betaz")
79       ), 
80       # regression weights
81       # -------------------------------------
82       mxPath(
83           from="one",
84           to=c("x", "y", "z"),
85           arrows=1,
86           free=TRUE,
87           values=c(1, 1),
88           labels=c("meanx", "beta0", "meanz")
89       )
90       # means and intercepts
91       # -------------------------------------
92   ) # close model
93 # Create an MxModel object
94 # -----------------------------------------------------------------------------
95       
96 multiRegFit<-mxRun(multiRegModel)
97
98 summary(multiRegFit)
99 multiRegFit@output
100
101
102 omxCheckCloseEnough(multiRegFit@output$estimate[["beta0"]], 1.6332, 0.001)
103 omxCheckCloseEnough(multiRegFit@output$estimate[["betax"]], 0.4246, 0.001)
104 omxCheckCloseEnough(multiRegFit@output$estimate[["betaz"]], 0.2260, 0.001)
105 omxCheckCloseEnough(multiRegFit@output$estimate[["residual"]], 0.6267, 0.001)
106 omxCheckCloseEnough(multiRegFit@output$estimate[["varx"]], 1.1053, 0.001)
107 omxCheckCloseEnough(multiRegFit@output$estimate[["varz"]], 0.8275, 0.001)
108 omxCheckCloseEnough(multiRegFit@output$estimate[["covxz"]], 0.2862, 0.001)
109 omxCheckCloseEnough(multiRegFit@output$estimate[["meanx"]], 0.0542, 0.001)
110 omxCheckCloseEnough(multiRegFit@output$estimate[["meanz"]], 4.0611, 0.001)
111 # Compare OpenMx results to Mx results 
112 # -----------------------------------------------------------------------------