Can't derive CIs in multiple groups twin model. Too nested!
**Summary**: if I try go get cis for parameters in multiple groups twin models (written in matrix algebra only), open mx complains about it being too nested. Multiple groups here refers to running the model for males and females, for example. See MWE.
**Error**:
> super_o <- mxRun(super, intervals = T)
trace: mxRun(super, intervals = T)
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
**MWE**:
require(OpenMx)
require(dplyr)
# Load Data
data(twinData)
# Select Variables for Analysis
Vars <- 'bmi'
nv <- 1 # number of variables
ntv <- nv*2 # number of total variables
selVars <- paste(Vars,c(rep(1,nv),rep(2,nv)),sep="") #c('bmi1','bmi2')
# Select Data for Analysis
mzData_m <- twinData |> filter(zygosity == "MZMM") |> select(bmi1, bmi2)
mzData_f <- twinData |> filter(zygosity == "MZFF") |> select(bmi1, bmi2)
dzData_m <- twinData |> filter(zygosity == "DZMM") |> select(bmi1, bmi2)
dzData_f <- twinData |> filter(zygosity == "DZFF") |> select(bmi1, bmi2)
# Prepare Data
# -----------------------------------------------------------------------------
# Set Starting Values
svMe <- 20 # start value for means
svPa <- .5 # start value for path coefficients (sqrt(variance/#ofpaths))
# ACE Model
# Matrices declared to store a, d, and e Path Coefficients
pathA <- mxMatrix( type="Full", nrow=nv, ncol=nv,
free=TRUE, values=svPa, label="a11", name="m_a" )
pathC <- mxMatrix( type="Full", nrow=nv, ncol=nv,
free=TRUE, values=svPa, label="c11", name="m_c" )
pathE <- mxMatrix( type="Full", nrow=nv, ncol=nv,
free=TRUE, values=svPa, label="e11", name="m_e" )
# Matrices generated to hold A, C, and E computed Variance Components
covA <- mxAlgebra( expression=m_a %*% t(m_a), name="A" )
covC <- mxAlgebra( expression=m_c %*% t(m_c), name="C" )
covE <- mxAlgebra( expression=m_e %*% t(m_e), name="E" )
# Algebra to compute total variances
covP <- mxAlgebra( expression=A+C+E, name="V" )
# Algebra for expected Mean and Variance/Covariance Matrices in MZ & DZ twins
meanG <- mxMatrix( type="Full", nrow=1, ncol=ntv,
free=TRUE, values=svMe, label="mean", name="expMean" )
covMZ <- mxAlgebra( expression=rbind( cbind(V, A+C),
cbind(A+C, V)), name="expCovMZ" )
covDZ <- mxAlgebra( expression=rbind( cbind(V, 0.5%x%A+C),
cbind(0.5%x%A+C , V)), name="expCovDZ" )
# Data objects for Multiple Groups
dataMZ <- mxData( observed=mzData_m, type="raw" )
dataDZ <- mxData( observed=dzData_f, type="raw" )
# Objective objects for Multiple Groups
expMZ <- mxExpectationNormal( covariance="expCovMZ", means="expMean",
dimnames=selVars )
expDZ <- mxExpectationNormal( covariance="expCovDZ", means="expMean",
dimnames=selVars )
funML <- mxFitFunctionML()
# Combine Groups
pars <- list( pathA, pathC, pathE, covA, covC, covE, covP )
modelMZ <- mxModel( pars, meanG, covMZ, dataMZ, expMZ, funML, name="MZ" )
modelDZ <- mxModel( pars, meanG, covDZ, dataDZ, expDZ, funML, name="DZ" )
fitML <- mxFitFunctionMultigroup(c("MZ", "DZ") )
ci <- mxCI("m_a")
twinACEModel <- mxModel( "ACE_m", pars, modelMZ, modelDZ, fitML ,ci)
# Run Model
twinACEFit <- mxRun(twinACEModel, intervals=T)
summary(twinACEFit)
###### AGAIN FOR FEMALES
# ACE Model
# Matrices declared to store a, d, and e Path Coefficients
pathA <- mxMatrix( type="Full", nrow=nv, ncol=nv,
free=TRUE, values=svPa, label="a11", name="f_a" )
pathC <- mxMatrix( type="Full", nrow=nv, ncol=nv,
free=TRUE, values=svPa, label="c11", name="f_c" )
pathE <- mxMatrix( type="Full", nrow=nv, ncol=nv,
free=TRUE, values=svPa, label="e11", name="f_e" )
# Matrices generated to hold A, C, and E computed Variance Components
covA <- mxAlgebra( expression=f_a %*% t(f_a), name="A" )
covC <- mxAlgebra( expression=f_c %*% t(f_c), name="C" )
covE <- mxAlgebra( expression=f_e %*% t(f_e), name="E" )
# Algebra to compute total variances
covP <- mxAlgebra( expression=A+C+E, name="V" )
# Algebra for expected Mean and Variance/Covariance Matrices in MZ & DZ twins
meanG <- mxMatrix( type="Full", nrow=1, ncol=ntv,
free=TRUE, values=svMe, label="mean", name="expMean" )
covMZ <- mxAlgebra( expression=rbind( cbind(V, A+C),
cbind(A+C, V)), name="expCovMZ" )
covDZ <- mxAlgebra( expression=rbind( cbind(V, 0.5%x%A+C),
cbind(0.5%x%A+C , V)), name="expCovDZ" )
# Data objects for Multiple Groups
dataMZ <- mxData( observed=mzData_m, type="raw" )
dataDZ <- mxData( observed=dzData_f, type="raw" )
# Objective objects for Multiple Groups
expMZ <- mxExpectationNormal( covariance="expCovMZ", means="expMean",
dimnames=selVars )
expDZ <- mxExpectationNormal( covariance="expCovDZ", means="expMean",
dimnames=selVars )
funML <- mxFitFunctionML()
# Combine Groups
pars <- list( pathA, pathC, pathE, covA, covC, covE, covP )
modelMZ <- mxModel( pars, meanG, covMZ, dataMZ, expMZ, funML, name="MZ_f" )
modelDZ <- mxModel( pars, meanG, covDZ, dataDZ, expDZ, funML, name="DZ_f" )
fitML <- mxFitFunctionMultigroup(c("MZ_f", "DZ_f") )
ci <- mxCI("f_a")
twinACEModel_f <- mxModel( "ACE_f", pars, modelMZ, modelDZ, fitML ,ci)
# Run Model
twinACEFit_f <- mxRun(twinACEModel_f, intervals=T)
summary(twinACEFit_f)
### Supermodel
super <- mxModel("super", twinACEModel, twinACEModel_f,
mxFitFunctionMultigroup(groups = c("ACE_m", "ACE_f")))
super_o <- mxRun(super, intervals = T)
summary(super_o)
Found solution
removing the third level nesting solved it.
Log in or register to post comments