1 # -----------------------------------------------------------------------
2 # Program: BivariateSaturated_MatrixRawCholesky.R
3 #  Author: Hermine Maes
4 #    Date: 08 01 2009
5 #
6 # Bivariate Saturated model to estimate means and (co)variances
7 # using Cholesky Decomposition
8 # Matrix style model input - Raw data input
9 #
10 # Revision History
11 #   Hermine Maes -- 10 08 2009 updated & reformatted
12 # -----------------------------------------------------------------------
14 require(OpenMx)
16 #Simulate Data
17 # -----------------------------------------------------------------------
18 require(MASS)
19 set.seed(200)
20 rs=.5
21 xy <- mvrnorm (1000, c(0,0), matrix(c(1,rs,rs,1),2,2))
22 testData <- xy
23 selVars <- c("X","Y")
24 dimnames(testData) <- list(NULL, selVars)
25 summary(testData)
26 cov(testData)
28 #example 6: Saturated Model with Raw Data and Matrix-Style Input
29 # -----------------------------------------------------------------------
30 bivSatModel6 <- mxModel("bivSat6",
31     mxMatrix(
32                 type="Full",
33                 nrow=2,
34                 ncol=2,
35                 free=c(TRUE,TRUE,FALSE,TRUE),
36                 values=c(1,.2,0,1),
37                 name="Chol"
38         ),
39         mxMatrix(
40                 type="Full",
41                 nrow=1,
42                 ncol=2,
43                 free=TRUE,
44                 values=c(0,0),
45                 name="expMean"
46         ),
47     mxAlgebra(
48                 expression=Chol %*% t(Chol),
49                 name="expCov"
50         ),
51         mxData(
52                 observed=testData,
53                 type="raw"
54         ),
55         mxFIMLObjective(
56                 covariance="expCov",
57                 means="expMean",
58                 dimnames=selVars
59         )
60 )
62 bivSatFit6 <- mxRun(bivSatModel6)
63 EM <- mxEval(expMean, bivSatFit6)
64 EC <- mxEval(expCov, bivSatFit6)
65 LL <- mxEval(objective,bivSatFit6)