45
45
# ' # = Does Rain cause Mud? =
46
46
# ' # ========================
47
47
# '
48
+ # ' # =======================================
49
+ # ' # = 2. Define manifests for var 1 and 2 =
50
+ # ' # =======================================
51
+ # ' var1 = paste0("varA", 1:3)
52
+ # ' var2 = paste0("varB", 1:3)
53
+ # '
48
54
# ' # ================
49
55
# ' # = 1. Load Data =
50
56
# ' # ================
51
57
# ' data(docData)
52
- # ' mzData = subset(docData, zygosity %in% c("MZFF", "MZMM"))
53
- # ' dzData = subset(docData, zygosity %in% c("DZFF", "DZMM"))
54
- # '
55
- # ' # =======================================
56
- # ' # = 2. Define manifests for var 1 and 2 =
57
- # ' # =======================================
58
- # ' var1 = paste0("a", 1:3)
59
- # ' var2 = paste0("b", 1:3)
58
+ # ' docData = umx_scale_wide_twin_data(c(var1, var2), docData, sep= "_T")
59
+ # ' mzData = subset(docData, zygosity %in% c("MZFF", "MZMM"))
60
+ # ' dzData = subset(docData, zygosity %in% c("DZFF", "DZMM"))
60
61
# '
61
62
# ' # =======================================================
62
63
# ' # = 2. Make the non-causal (Cholesky) and causal models =
63
64
# ' # =======================================================
64
- # ' Chol= umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= FALSE)
65
- # ' DoC = umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= TRUE)
65
+ # ' Chol = umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= FALSE)
66
+ # ' DoC = umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= TRUE)
66
67
# '
67
68
# ' # ================================================
68
69
# ' # = Make the directional models by modifying DoC =
83
84
# '
84
85
# ' }
85
86
# '
86
- umxDoC <- function (name = " DOC" , var1Indicators , var2Indicators , mzData = NULL , dzData = NULL , sep = " _T" , causal = TRUE , autoRun = getOption(" umx_auto_run" ), intervals = FALSE , tryHard = c(" no" , " yes" , " ordinal" , " search" ), optimizer = NULL ) {
87
+ umxDoC <- function (name = " DoC" , var1Indicators , var2Indicators , mzData = NULL , dzData = NULL , sep = " _T" , causal = TRUE , autoRun = getOption(" umx_auto_run" ), intervals = FALSE , tryHard = c(" no" , " yes" , " ordinal" , " search" ), optimizer = NULL ) {
88
+ # TODO: umxDoC add some name checking to avoid variables like "a1"
89
+ if (name == " DoC" ){
90
+ if (causal ){
91
+ name = " DoC"
92
+ } else {
93
+ name = " Chol"
94
+ }
95
+ }
87
96
tryHard = match.arg(tryHard )
88
97
umx_check(is.logical(causal ), " stop" , " causal must be TRUE or FALSE" )
89
98
nSib = 2 # Number of siblings in a twin pair.
@@ -113,18 +122,18 @@ umxDoC <- function(name = "DOC", var1Indicators, var2Indicators, mzData= NULL, d
113
122
top = mxModel(" top" , # (was "ACE")
114
123
umxMatrix(" dzAr" , " Full" , nrow = nLat , ncol = nLat , free = FALSE , values = c(1 ,.5 ,.5 ,1 ) ), # Heredity Matrix for DZ
115
124
umxMatrix(" Ones" , " Full" , nrow = nLat , ncol = nLat , free = FALSE , values = 1 ), # Unit Matrix - For Com Env and MZ
116
- umxMatrix(" Diag1" , " Iden" , nrow = nSib , ncol = nSib ), # Identity matrix (2by2: 1s on diag, 0 off diag)
125
+ umxMatrix(" Diag1" , " Iden" , nrow = nSib , ncol = nSib ), # Identity matrix (2by2: 1s on diag, 0 off diag)
117
126
118
127
# Matrices for Cholesky (swapped out after if causal)
119
- umxMatrix(" a" , type = " Lower" , nrow = nLat , ncol = nLat , free = TRUE , values = .2 ), # Genetic effects on Latent Variables
120
- umxMatrix(" c" , type = " Lower" , nrow = nLat , ncol = nLat , free = TRUE , values = .2 ), # Common env effects on Latent Variables
128
+ umxMatrix(" a" , type = " Lower" , nrow = nLat , ncol = nLat , free = TRUE , values = .2 ), # Genetic effects on Latent Variables
129
+ umxMatrix(" c" , type = " Lower" , nrow = nLat , ncol = nLat , free = TRUE , values = .2 ), # Common env effects on Latent Variables
121
130
umxMatrix(" e" , type = " Lower" , nrow = nLat , ncol = nLat , free = c(FALSE ,TRUE ,FALSE ), values = 1 ), # Non-shared env effects on Latent Variables
122
131
123
132
# 4x4 Matrices for A, C, and E
124
133
mxAlgebra(name = " A" , Ones %x% (a %*% t(a ))),
125
134
mxAlgebra(name = " Adz" , dzAr %x% (a %*% t(a ))),
126
135
mxAlgebra(name = " C" , Ones %x% (c %*% t(c ))),
127
- mxAlgebra(name = " E" , Diag1 %x% (e %*% t(e ))),
136
+ mxAlgebra(name = " E" , Diag1 %x% (e %*% t(e ))),
128
137
mxAlgebra(name = " Vmz" , A + C + E ),
129
138
mxAlgebra(name = " Vdz" , Adz + C + E ),
130
139
@@ -164,24 +173,20 @@ umxDoC <- function(name = "DOC", var1Indicators, var2Indicators, mzData= NULL, d
164
173
MZ = mxModel(" MZ" , mzData , mxExpectationNormal(" top.expCovMZ" , means = " top.expMean" , dimnames = selVars ), mxFitFunctionML() )
165
174
DZ = mxModel(" DZ" , dzData , mxExpectationNormal(" top.expCovDZ" , means = " top.expMean" , dimnames = selVars ), mxFitFunctionML() )
166
175
167
- if (! causal ){
168
- # ========================
169
- # = Cholesky-based model =
170
- # ========================
171
- model = mxModel(" Chol" , top , MZ , DZ , mxFitFunctionMultigroup(c(" MZ" , " DZ" )) )
172
- }else {
176
+ if (causal ){
173
177
# ===================
174
178
# = DOC-based model =
175
179
# ===================
176
180
# Replace lower ace Matrices with diag.
177
181
# Because covariance between the traits is "caused", theses matrices are diagonal instead of lower
178
182
top = mxModel(top ,
179
- umxMatrix(" a" , " Diag" , nrow = nLat , ncol = nLat , free = TRUE , values = 0.2 ), # Genetic effects on Latent Variables
180
- umxMatrix(" c" , " Diag" , nrow = nLat , ncol = nLat , free = TRUE , values = 0.2 ), # Common env effects on Latent Variables
181
- umxMatrix(" e" , " Diag" , nrow = nLat , ncol = nLat , free = FALSE , values = 1 ) # Non-shared env effects on Latent Variables
183
+ umxMatrix(" a" , " Diag" , nrow = nLat , ncol = nLat , free = TRUE , values = 0.2 ), # Genetic effects on Latent Variables
184
+ umxMatrix(" c" , " Diag" , nrow = nLat , ncol = nLat , free = TRUE , values = 0.2 ), # Common env effects on Latent Variables
185
+ umxMatrix(" e" , " Diag" , nrow = nLat , ncol = nLat , free = FALSE , values = 1.0 ) # E@1
182
186
)
183
- model = mxModel(" DOC" , top , MZ , DZ , mxFitFunctionMultigroup(c(" MZ" , " DZ" )) )
184
187
}
188
+ model = mxModel(name , top , MZ , DZ , mxFitFunctionMultigroup(c(" MZ" , " DZ" )) )
189
+
185
190
# Factor loading matrix of Intercept and Slope on observed phenotypes
186
191
# SDt = mxAlgebra(name= "SDt", solve(sqrt(Diag1 *Rt))) # Standardized deviations (inverse)
187
192
model = omxAssignFirstParameters(model )
@@ -219,9 +224,33 @@ umxDoC <- function(name = "DOC", var1Indicators, var2Indicators, mzData= NULL, d
219
224
# ' @seealso - [umxDoC()], [umxSummary.MxModelDoC()], [umxModify()]
220
225
# ' @md
221
226
# ' @examples
222
- # ' #
223
- umxPlotDoC <- function (x = NA , means = FALSE , std = TRUE , digits = 2 , showFixed = TRUE , file = " name" , format = c(" current" , " graphviz" , " DiagrammeR" ), SEstyle = FALSE , strip_zero = TRUE , ... ) {
224
- message(" I'm sorry Dave, no plot for doc yet ;-(" )
227
+ # '
228
+ # ' # ================
229
+ # ' # = 1. Load Data =
230
+ # ' # ================
231
+ # ' data(docData)
232
+ # ' mzData = subset(docData, zygosity %in% c("MZFF", "MZMM"))
233
+ # ' dzData = subset(docData, zygosity %in% c("DZFF", "DZMM"))
234
+ # '
235
+ # ' # =======================================
236
+ # ' # = 2. Define manifests for var 1 and 2 =
237
+ # ' # =======================================
238
+ # ' var1 = paste0("varA", 1:3)
239
+ # ' var2 = paste0("varB", 1:3)
240
+ # '
241
+ # ' # =======================================================
242
+ # ' # = 2. Make the non-causal (Cholesky) and causal models =
243
+ # ' # =======================================================
244
+ # ' Chol= umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= FALSE)
245
+ # ' DoC = umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= TRUE)
246
+ # '
247
+ # ' # ================================================
248
+ # ' # = Make the directional models by modifying DoC =
249
+ # ' # ================================================
250
+ # ' a2b = umxModify(DoC, "a2b", free = TRUE, name = "A2B")
251
+ # ' plot(a2b)
252
+ umxPlotDoC <- function (x = NA , means = FALSE , std = TRUE , digits = 2 , showFixed = TRUE , file = " name" , format = c(" current" , " graphviz" , " DiagrammeR" ), SEstyle = FALSE , strip_zero = FALSE , ... ) {
253
+ message(" I'm sorry Dave, no plot for DoC yet ;-(" )
225
254
# 1. ✓ draw latents
226
255
# 2. ✓ draw manifests,
227
256
# 3. ✓ draw ace to latents
@@ -234,25 +263,27 @@ umxPlotDoC <- function(x = NA, means = FALSE, std = TRUE, digits = 2, showFixed
234
263
umx_check_model(model , " MxModelDoC" , callingFn = " umxPlotDoC" )
235
264
236
265
if (std ){
237
- model = xmu_standardize_DoC(model )
266
+ message(" I'm sorry Dave, no std for DoC yet ;-(" )
267
+ # model = xmu_standardize_DoC(model)
238
268
}
239
269
240
- nFac = dim(model $ top $ a_cp $ labels )[[1 ]]
241
- nVar = dim(model $ top $ as $ values )[[1 ]]
242
- selDVs = dimnames(model $ MZ $ data $ observed )[[2 ]]
243
- selDVs = selDVs [1 : (nVar )]
244
- selDVs = sub(" (_T)?[0-9]$" , " " , selDVs ) # trim "_Tn" from end
245
-
246
- out = list ( str = " " , latents = c(), manifests = c() )
270
+ nFac = dim(model $ top $ a_cp $ labels )[[1 ]]
271
+ nVar = dim(model $ top $ as $ values )[[1 ]]
272
+ selDVs = dimnames(model $ MZ $ data $ observed )[[2 ]]
273
+ selDVs = selDVs [1 : (nVar )]
274
+ selDVs = sub(" (_T)?[0-9]$" , " " , selDVs ) # trim "_Tn" from end
275
+ out = list ( str = " " , latents = c(), manifests = c())
276
+ selLat = c( " a " , " b " )
247
277
248
278
# Process [ace] matrices
249
279
# 1. Collect latents
250
- out = xmu_dot_mat2dot(model $ top $ a , cells = " diag" , from = " rows" , toLabel = c( " a " , " b " ) , fromType = " latent" , showFixed = showFixed , p = out )
251
- out = xmu_dot_mat2dot(model $ top $ c , cells = " diag" , from = " rows" , toLabel = c( " a " , " b " ) , fromType = " latent" , showFixed = showFixed , p = out )
252
- out = xmu_dot_mat2dot(model $ top $ e , cells = " diag" , from = " rows" , toLabel = c( " a " , " b " ) , fromType = " latent" , showFixed = showFixed , p = out )
280
+ out = xmu_dot_mat2dot(model $ top $ a , cells = " diag" , from = " rows" , toLabel = selLat , fromType = " latent" , showFixed = showFixed , p = out )
281
+ out = xmu_dot_mat2dot(model $ top $ c , cells = " diag" , from = " rows" , toLabel = selLat , fromType = " latent" , showFixed = showFixed , p = out )
282
+ out = xmu_dot_mat2dot(model $ top $ e , cells = " diag" , from = " rows" , toLabel = selLat , fromType = " latent" , showFixed = showFixed , p = out )
253
283
254
284
# 2. Process "FacLoad" nVar * nFac matrix: latents into common paths.
255
- out = xmu_dot_mat2dot(model $ top $ FacLoad , cells = " any" , toLabel = selDVs , from = " cols" , fromLabel = c(" a" , " b" ), fromType = " latent" , showFixed = showFixed , p = out )
285
+ out = xmu_dot_mat2dot(model $ top $ FacLoad , cells = " any" , toLabel = selDVs , from = " cols" , fromLabel = selLat , fromType = " latent" , showFixed = showFixed , p = out )
286
+
256
287
# 3. Process "as" matrix
257
288
out = xmu_dot_mat2dot(model $ top $ as , cells = " any" , toLabel = selDVs , from = " rows" , fromType = " latent" , showFixed = showFixed , p = out )
258
289
out = xmu_dot_mat2dot(model $ top $ cs , cells = " any" , toLabel = selDVs , from = " rows" , fromType = " latent" , showFixed = showFixed , p = out )
@@ -264,7 +295,7 @@ umxPlotDoC <- function(x = NA, means = FALSE, std = TRUE, digits = 2, showFixed
264
295
# [,1] [,2]
265
296
# [1,] "a2a" "b2a"
266
297
# [2,] "a2b" "b2b"
267
-
298
+ out = xmu_dot_mat2dot( model $ top $ beta , cells = " any " , toLabel = selLat , from = " cols " , fromType = " latent " , showFixed = showFixed , p = out , fromLabel = selLat )
268
299
# Process "expMean" 1 * nVar matrix
269
300
if (means ){
270
301
# from = "one"; target = selDVs[c]
@@ -314,13 +345,30 @@ plot.MxModelDoC <- umxPlotDoC
314
345
# ' @seealso - [umxDoC()], [plot.MxModelDoC()], [umxModify()], [umxCP()], [plot()], [umxSummary()] work for IP, CP, GxE, SAT, and ACE models.
315
346
# ' @md
316
347
# ' @examples
317
- # ' require(umx)
318
- # ' data(twinData)
348
+ # ' # ================
349
+ # ' # = 1. Load Data =
350
+ # ' # ================
319
351
# ' umx_set_auto_plot(FALSE) # turn off autoplotting for CRAN
352
+ # ' data(docData)
320
353
# ' mzData = subset(docData, zygosity %in% c("MZFF", "MZMM"))
321
354
# ' dzData = subset(docData, zygosity %in% c("DZFF", "DZMM"))
322
- # ' DoC = umxDoC(var1= paste0("a", 1:3), var2 = paste0("b", 1:3),
323
- # ' mzData= mzData, dzData= dzData, causal= TRUE)
355
+ # '
356
+ # ' # =======================================
357
+ # ' # = 2. Define manifests for var 1 and 2 =
358
+ # ' # =======================================
359
+ # ' var1 = paste0("varA", 1:3)
360
+ # ' var2 = paste0("varB", 1:3)
361
+ # '
362
+ # ' # =======================================================
363
+ # ' # = 2. Make the non-causal (Cholesky) and causal models =
364
+ # ' # =======================================================
365
+ # ' Chol= umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= FALSE)
366
+ # ' DoC = umxDoC(var1= var1, var2= var2, mzData= mzData, dzData= dzData, causal= TRUE)
367
+ # '
368
+ # ' # ================================================
369
+ # ' # = Make the directional models by modifying DoC =
370
+ # ' # ================================================
371
+ # ' A2B = umxModify(DoC, "a2b", free = TRUE, name = "A2B")
324
372
# ' A2B = umxModify(DoC, "a2b", free = TRUE, name = "A2B", comp=TRUE)
325
373
# ' B2A = umxModify(DoC, "b2a", free = TRUE, name = "B2A", comp=TRUE)
326
374
# ' umxCompare(B2A, A2B)
0 commit comments