Fixed a bug where could not be accessed from a model object, and adjusted one test...
[openmx:openmx.git] / demo / SimpleRegression_MatrixRaw.R
1 #
2 #   Copyright 2007-2015 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 # -----------------------------------------------------------------------------
18 # Program: SimpleRegression_MatrixRaw.R  
19 # Author: Ryne Estabrook
20 # Date: 2009.08.01 
21 #
22 # ModelType: Regression
23 # DataType: Continuous
24 # Field: None
25 #
26 # Purpose:
27 #      Simple Regression model to estimate effect of 
28 #      independent on dependent variables
29 #      Matrix style model input - Raw data input
30 #
31 # RevisionHistory:
32 #      Hermine Maes -- 10 08 2009 updated & reformatted
33 #      Ross Gore -- 06 06 2011  added Model, Data & Field metadata
34 #      Hermine Maes -- 2014.11.02 piecewise specification
35 # -----------------------------------------------------------------------------
36
37 require(OpenMx)
38 # Load Library
39 # -----------------------------------------------------------------------------
40
41 data(myRegDataRaw)
42 SimpleDataRaw <- myRegDataRaw[,c("x","y")]
43 # Prepare Data
44 # -----------------------------------------------------------------------------
45
46 dataRaw      <- mxData( observed=SimpleDataRaw, type="raw" )
47 matrA        <- mxMatrix( type="Full", nrow=2, ncol=2, 
48                           free=c(F,F,T,F), values=c(0,0,1,0), 
49                           labels=c(NA,NA,"beta1",NA), byrow=TRUE, name="A" )
50 matrS        <- mxMatrix( type="Symm", nrow=2, ncol=2, 
51                           free=c(T,F,F,T), values=c(1,0,0,1), 
52                           labels=c("varx",NA,NA,"residual"), byrow=TRUE, name="S" )
53 matrF        <- mxMatrix( type="Iden", nrow=2, ncol=2, name="F" )
54 matrM        <- mxMatrix( type="Full", nrow=1, ncol=2, 
55                           free=c(T,T), values=c(0,0), 
56                           labels=c("meanx","beta0"), name="M")
57 expRAM       <- mxExpectationRAM("A","S","F","M", dimnames=c("x","y"))
58 funML        <- mxFitFunctionML()
59 uniRegModel  <- mxModel("Simple Regression Matrix Specification", 
60                         dataRaw, matrA, matrS, matrF, matrM, expRAM, funML)
61 # Create an MxModel object
62 # -----------------------------------------------------------------------------
63      
64 uniRegFit<-mxRun(uniRegModel)
65 # Fit the model with mxRun
66 # -----------------------------------------------------------------------------
67
68 summary(uniRegFit)
69 # Print a summary of the results
70 # -----------------------------------------------------------------------------
71
72 uniRegFit$output
73 # Print the outputs populated by mxRun
74 # -----------------------------------------------------------------------------
75
76 omxCheckCloseEnough(uniRegFit$output$estimate[["beta0"]], 2.5478, 0.001)
77 omxCheckCloseEnough(uniRegFit$output$estimate[["beta1"]], 0.4831, 0.001)
78 omxCheckCloseEnough(uniRegFit$output$estimate[["residual"]], 0.6652, 0.001)
79 omxCheckCloseEnough(uniRegFit$output$estimate[["meanx"]], 0.0542, 0.001)
80 omxCheckCloseEnough(uniRegFit$output$estimate[["varx"]], 1.1053, 0.001)
81 # Compare OpenMx results to Mx results 
82 # -----------------------------------------------------------------------------