Move all fitfunction args to expectation
[openmx:openmx.git] / models / nightly / RAM-3Factor-96Indicators-covdata-a.R
1 # ---------------------------------------------------------------------
2 # Program: RAM-3Factor-12Indicators.R
3 #  Author: Steven M. Boker
4 #    Date: Fri Jul 30 13:45:12 EDT 2010
5 #
6 # This program is a factor model using standard RAM.
7 #
8 # ---------------------------------------------------------------------
9 # Revision History
10 #    -- Fri Jul 30 13:45:12 EDT 2010
11 #      Created RAM-3Factor-12Indicators.R.
12 #
13 # ---------------------------------------------------------------------
14
15 # ----------------------------------
16 # Read libraries and set options.
17
18 library(OpenMx)
19
20 options(width=100)
21 set.seed(10)
22
23 # ---------------------------------------------------------------------
24 # Data for factor model.
25
26 numberSubjects <- 1000
27 numberFactors <- 3
28 numberIndPerFactor <- 32
29 numberIndicators <- numberIndPerFactor*numberFactors # must be a multiple of numberFactors
30
31 XMatrix <- matrix(rnorm(numberSubjects*numberFactors, mean=0, sd=1), numberSubjects, numberFactors)
32
33 tLoadings <- c(1, seq(.5, .9, length.out=(numberIndPerFactor-1)), rep(0, numberIndPerFactor*2),
34   rep(0, numberIndPerFactor*1), 1, seq(.5, .9, length.out=(numberIndPerFactor-1)), rep(0, numberIndPerFactor*1),
35   rep(0, numberIndPerFactor*2), 1, seq(.5, .9, length.out=(numberIndPerFactor-1)))
36 BMatrix <- matrix(tLoadings, numberFactors, numberIndicators, byrow=TRUE)
37 UMatrix <- matrix(rnorm(numberSubjects*numberIndicators, mean=0, sd=1), numberSubjects, numberIndicators)
38 YMatrix <- XMatrix %*% BMatrix + UMatrix
39
40 cor(XMatrix)
41
42 dimnames(YMatrix) <- list(NULL, paste("X", 1:numberIndicators, sep=""))
43
44 round(cor(YMatrix), 3)
45 round(cov(YMatrix), 3)
46
47 indicators <- paste("X", 1:numberIndicators, sep="")
48 totalVars <- numberIndicators + numberFactors
49
50 # ----------------------------------
51 # Build an orthogonal simple structure factor model
52
53 latents <- paste("F", 1:numberFactors, sep="")
54
55 uniqueLabels <- paste("U_", indicators, sep="")
56 meanLabels <- paste("M_", latents, sep="")
57 factorVarLabels <- paste("Var_", latents, sep="")
58
59 latents1 <- latents[1]
60 indicators1 <- indicators[1:numberIndPerFactor]
61 loadingLabels1 <- paste("b_F1", indicators[1:numberIndPerFactor], sep="") 
62 latents2 <- latents[2]
63 indicators2 <- indicators[numberIndPerFactor+(1:numberIndPerFactor)]
64 loadingLabels2 <- paste("b_F2", indicators[numberIndPerFactor+(1:numberIndPerFactor)], sep="") 
65 latents3 <- latents[3]
66 indicators3 <- indicators[(2*numberIndPerFactor)+(1:numberIndPerFactor)]
67 loadingLabels3 <- paste("b_F3", indicators[(2*numberIndPerFactor)+(1:numberIndPerFactor)], sep="") 
68
69 threeFactorOrthogonal <- mxModel("threeFactorOrthogonal",
70     type="RAM",
71     manifestVars=c(indicators),
72     latentVars=c(latents,"dummy1"),
73     mxPath(from=latents1, to=indicators1, 
74            arrows=1, connect="all.pairs",
75            free=TRUE, values=.2, 
76            labels=loadingLabels1),
77     mxPath(from=latents2, to=indicators2, 
78            arrows=1, connect="all.pairs",
79            free=TRUE, values=.2, 
80            labels=loadingLabels2),
81     mxPath(from=latents3, to=indicators3, 
82            arrows=1, connect="all.pairs",
83            free=TRUE, values=.2, 
84            labels=loadingLabels3),
85     mxPath(from=latents1, to=indicators1[1], 
86            arrows=1, 
87            free=FALSE, values=1),
88     mxPath(from=latents2, to=indicators2[1], 
89            arrows=1, 
90            free=FALSE, values=1),
91     mxPath(from=latents3, to=indicators3[1], 
92            arrows=1, 
93            free=FALSE, values=1),
94     mxPath(from=indicators, 
95            arrows=2, 
96            free=TRUE, values=.2, 
97            labels=uniqueLabels),
98     mxPath(from=latents,
99            arrows=2, 
100            free=TRUE, values=.8, 
101            labels=factorVarLabels),
102     mxPath(from="one", to=indicators, 
103            arrows=1, free=FALSE, values=0),
104     mxPath(from="one", to=c(latents), 
105            arrows=1, free=TRUE, values=.1, 
106            labels=meanLabels),
107     mxData(observed=cov(YMatrix), means=apply(YMatrix, 2, mean), 
108         numObs=nrow(YMatrix), type="cov")
109     )
110
111 threeFactorOrthogonalOut <- mxRun(threeFactorOrthogonal)
112 summary(threeFactorOrthogonalOut)
113