# Calculating expressions in model

I rewrote mrdoc to shorten the hundreds line model to the version below,

however in this version some paths are mixed in correlations. For example,

abraas is combining ab, ra, and as. I wanted to make it easier to the user

and provide each path separately, but I am not sure how to do this. I tried to use

mxEval, but I think it is beyond my capabilities.

Just as example, I wanted to provide ab instead of ab2 and its se. mxEval(model, expression=sqrt(ab2)) after the fact works. Also, ra is calculated as abraas-(sqrt(ab2)-sqrt(as2)).

- How to calculate these in the model and provide them in the list of parameters with SEs?

library(umx)

```
```data(docData)

mzData = subset(docData, zygosity %in% c("MZFF", "MZMM"))

dzData = subset(docData, zygosity %in% c("DZFF", "DZMM"))

top = mxModel("top",

umxMatrix("BE", type = "Full",nrow=3, ncol = 3,

labels = c(NA, "g2", "b1",

"g1", NA, "b2",

NA, NA, NA),

free = c(FALSE, FALSE, TRUE,

TRUE, FALSE, TRUE,

FALSE, FALSE, FALSE)),

umxMatrix('I', type='Iden', nrow= 3,ncol= 3 ),

umxMatrix('F', type='Full', nrow=5, ncol=6, free=FALSE,

values=c(1,0,0,0,0,0,

0,1,0,0,0,0,

0,0,1,0,0,0,

0,0,0,1,0,0,

0,0,0,0,1,0)),

umxMatrix('LY', type='Full',nrow=3, ncol = 3, free = FALSE, values = diag(3),

labels = NA),

umxMatrix('A', type='Symm', nrow=3,ncol = 3,

labels=c("ab2","abraas",NA,

"abraas","as2",NA,

NA,NA,"x2"),

free=c(TRUE,TRUE,FALSE,

TRUE,TRUE,FALSE,

FALSE,FALSE,TRUE)),

umxMatrix('C', type='Symm',nrow=3, ncol = 3,

labels =c("cb2","cbrccs",NA,

"cbrccs","cs2",NA,

NA,NA,NA),

free=c(TRUE,TRUE,FALSE,

TRUE,TRUE,FALSE,

FALSE,FALSE,FALSE)),

umxMatrix('E', type='Symm', nrow=3, ncol = 3,

labels =c("eb2",NA,NA,

NA,"es2",NA,

NA,NA,NA),

free= c(TRUE,FALSE,FALSE,

FALSE,TRUE,FALSE,

FALSE,FALSE,FALSE)),

umxMatrix('dzmu', type='Full', nrow=1, ncol=6, free=TRUE, values= 0, labels=c('muPh1','muPh2','muPS1','muPh1','muPh2','muPS1')),

mxAlgebra('mzmu', expression = dzmu%*%t(F)),

mxAlgebra('A_' , expression = solve(I - BE)%&%A),

mxAlgebra('C_' , expression = solve(I - BE)%&%C),

mxAlgebra('E_' , expression = solve(I - BE)%&%E),

mxAlgebra('SPh' , expression = A_ + C_ + E_),

mxAlgebra('Smz_', expression = rbind(

cbind(SPh, A_+C_),

cbind(A_+C_, SPh))),

mxAlgebra('Sdz', expression=rbind(

cbind(SPh, .5%x%A_+C_),

cbind(.5%x%A_+C_, SPh))),

mxAlgebra('Smz', expression= F%&%Smz_)

)

MZ = mxModel("MZ",

mxData(mzData[2:6], type="raw"),

mxExpectationNormal(covariance = "top.Smz",means = "top.mzmu", colnames(mzData[2:6])),

mxFitFunctionML()

)

DZ = mxModel("DZ",

mxData(dzData[2:7], type="raw"),

mxExpectationNormal(covariance = "top.Sdz",means = "top.dzmu", colnames(mzData[2:7])),

mxFitFunctionML()

)

model = mxModel("mrdoc", top, MZ, DZ, mxFitFunctionMultigroup(c("MZ","DZ") ))

out <- mxRun(model)

summary(out)

## mxSE()

Log in or register to post comments