How to add object to a model without re-run

Posted on
No user picture. JuanJMV Joined: 07/20/2016
Dear all,

I have fitted a very computationally demanding model (around one week) and now I need to add an mxAlgebra object into this model:

threG2 <- mxAlgebra( expression= threG[,1:7], name="threG2" )

I know I can do it in this way:

modelIPAE2 <- mxModel( fitIPAE,threG2, name="mulIPAE2c" )
fitIPAE2 <- mxTryHardOrdinal( modelIPAE2, intervals=F, extraTries=11 )

But I wonder if there is any way to modify the model without re-running it

I need it because I would like to use this line later:

fsModel@expectation <- mxExpectationNormal("as","mean2","threG[,1:7]",dimnames=selVars[1:7])

But I cannot make a subset of a S4 object, so I need to use this:

fsModel@expectation <- mxExpectationNormal("as","mean2","threG2",dimnames=selVars[1:7])

But I previously need to have threG2 in my model.

Thank you so much in advance!

Replied on Tue, 07/23/2024 - 11:09
Picture of user. AdminRobK Joined: 01/24/2014

Could you say a little bit more about what you're trying to do here? A full script (or at least part of one) could help provide some context.
Replied on Tue, 07/23/2024 - 14:27
No user picture. JuanJMV Joined: 07/20/2016

In reply to by AdminRobK

Hi Rob,

Thank you so much for your help.

Attached you can find the script that I am using.

First you can find a multivariate model including an independent pathway model (Boulder's script) (lines 1 to 190).

Then I have a script (I adapted it from this paper (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3773873/) to calculate individual factor scores for A an E (lines 191-230).

It has a loop to calculate the IF first for MZ Twin 1 (751 participants). The problem is that this model has thresholds and I cannot add the object “threG” because it has 14 columns. I had the same problem with mean but I created and added to the model this:

mean2 <- mxAlgebra( expression= mean[,1:7], name="mean2")

I wonder if there is any way to add this:

threG2 <- mxAlgebra( expression= threG[,1:7], name="threG2" )

To the model without re-running or any way to modify the model to have this:

mxExpectationNormal("as","mean2","threG[,1:7]",dimnames=selVars[1:7])

Thank you so much in advance.

With all good wishes

Replied on Fri, 08/30/2024 - 09:32
No user picture. lf-araujo Joined: 11/25/2020

It is difficult to help without a minimal working example. Does this work?


modelIPAE2 <- mxModel( fitIPAE,threG2, name="mulIPAE2c" )
fitIPAE2 <- mxTryHardOrdinal( modelIPAE2, intervals=F, extraTries=11 )

fitIPAE2 <- mxModel(fitIPAE2, mxExpectationNormal("as","mean2","threG[,1:7]",dimnames=selVars[1:7]))

Or with overloading:


modelIPAE2 <- mxModel( fitIPAE,threG2, name="mulIPAE2c" )

fitIPAE2 <- mxTryHardOrdinal( modelIPAE2, intervals=F, extraTries=11 )

fitIPAE2 <- fitIPAE2 + mxExpectationNormal("as","mean2","threG[,1:7]",dimnames=selVars[1:7])

Replied on Tue, 09/03/2024 - 03:13
No user picture. JuanJMV Joined: 07/20/2016

In reply to by lf-araujo

Thank you so much for this @If-araujo

That will work but if I do that I have to run the model again (more than one week).

Here you have a simple example using some simulated data. It is a univariate model an I have added a new object "mean2" but re-running the model. My question is if there is any way to add "mean2" into the model without re-running it.

Thank you so much in advance!

library(OpenMx)
library(psych); library(polycor)
source("miFunctions.R")
mxOption(NULL,"Default optimizer","CSOLNP")
set.seed(1)
cov.matone <- matrix(c(1, .7,
.7, 1), nrow = 2)

dfMZ <- data.frame(MASS::mvrnorm(n = 1e4,
mu = c(0, 0),
Sigma = cov.matone))

cov.mattwo <- matrix(c(1, .35,
.35, 1), nrow = 2)

dfDZ <- data.frame(MASS::mvrnorm(n = 1e4,
mu = c(0, 0),
Sigma = cov.mattwo))

cov(dfMZ)
cov(dfDZ)

# ----------------------------------------------------------------------------------------------------------------------
# Program: oneACEvc.R
# Author: Hermine Maes
# Date: 10 22 2018
#
# Twin Univariate ACE model to estimate causes of variation across multiple groups
# Matrix style model - Raw data - Continuous data
# -------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
# Select Variables for Analysis
vars <- 'X' # list of variables names
nv <- 1 # number of variables
ntv <- nv*2 # number of total variables
selVars <- paste(vars,c(rep(1,nv),rep(2,nv)),sep="")
# Select Data for Analysis
mzData <- dfMZ
dzData <- dfDZ
# Generate Descriptive Statistics
colMeans(mzData,na.rm=TRUE)
colMeans(dzData,na.rm=TRUE)
cov(mzData,use="complete")
cov(dzData,use="complete")
# Set Starting Values
svMe <- 20 # start value for means
svPa <- .2 # start value for path coefficient
svPe <- .5 # start value for path coefficient for e
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
meanG <- mxMatrix( type="Full", nrow=1, ncol=ntv, free=TRUE, values=svMe, labels=labVars("mean",vars), name="meanG" )
# Create Matrices for Variance Components
covA <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=svPa, label="VA11", name="VA" )
covC <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=svPa, label="VC11", name="VC" )
covE <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=svPa, label="VE11", name="VE" )
# Create Algebra for expected Variance/Covariance Matrices in MZ & DZ twins
covP <- mxAlgebra( expression= VA+VC+VE, name="V" )
covMZ <- mxAlgebra( expression= VA+VC, name="cMZ" )
covDZ <- mxAlgebra( expression= 0.5%x%VA+ VC, name="cDZ" )
expCovMZ <- mxAlgebra( expression= rbind( cbind(V, cMZ), cbind(t(cMZ), V)), name="expCovMZ" )
expCovDZ <- mxAlgebra( expression= rbind( cbind(V, cDZ), cbind(t(cDZ), V)), name="expCovDZ" )
# Create Data Objects for Multiple Groups
dataMZ <- mxData( observed=mzData, type="raw" )
dataDZ <- mxData( observed=dzData, type="raw" )
# Create Expectation Objects for Multiple Groups
expMZ <- mxExpectationNormal( covariance="expCovMZ", means="meanG", dimnames=selVars )
expDZ <- mxExpectationNormal( covariance="expCovDZ", means="meanG", dimnames=selVars )
funML <- mxFitFunctionML()
# Create Model Objects for Multiple Groups
pars <- list( meanG, covA, covC, covE, covP )
modelMZ <- mxModel( pars, covMZ, expCovMZ, dataMZ, expMZ, funML, name="MZ" )
modelDZ <- mxModel( pars, covDZ, expCovDZ, dataDZ, expDZ, funML, name="DZ" )
multi <- mxFitFunctionMultigroup( c("MZ","DZ") )
# Create Algebra for Unstandardized and Standardized Variance Components
rowUS <- rep('US',nv)
colUS <- rep(c('VA','VC','VE','SA','SC','SE'),each=nv)
estUS <- mxAlgebra( expression=cbind(VA,VC,VE,VA/V,VC/V,VE/V), name="US", dimnames=list(rowUS,colUS) )
# Create Confidence Interval Objects
ciACE <- mxCI( "US[1,1:3]" )
# Build Model with Confidence Intervals
modelACE <- mxModel( "oneACEvc", pars, modelMZ, modelDZ, multi, estUS, ciACE )
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
fitACE <- mxRun( modelACE, intervals=T )
sumACE <- summary( fitACE )
fitACE$algebras

mean2 <- mxAlgebra( expression= meanG[,1:1], name="mean2")
modelACE2 <- mxModel( fitACE,mean2, name="mulIPAE2c" )
fitACE2 <- mxRun( modelACE2, intervals=T )
sumACE2 <- summary( fitACE2 )
fitACE2
fitACE2$algebras

Replied on Wed, 09/04/2024 - 08:56
No user picture. lf-araujo Joined: 11/25/2020

Now I think I understand what you are trying to do. Simple answer: no, you can't.

By adding the new means algebra the estimation step must be run. Now, I know of the existence of mxEval() and I am not sure if you can use it to add the new expression and ask the optimizer to only run that, given the already estimated model.

Replied on Wed, 09/04/2024 - 09:03
No user picture. lf-araujo Joined: 11/25/2020

So here the mxEval example:

library(OpenMx)

# Set up a 1x1 matrix
matrixA <- mxMatrix("Full", nrow = 1, ncol = 1, values = 1, name = "A")

# Set up an algebra
algebraB <- mxAlgebra(A + A, name = "B")

# Put them both in a model
testModel <- mxModel(model="testModel3", matrixA, algebraB)

# Even though the model has not been run, we can evaluate the algebra
# given the starting values in matrixA.
mxEval(B, testModel, compute=TRUE)

# If we just print the algebra, we can see it has not been evaluated
testModel$B

So you may be able to do something like:

mean2 <- mxAlgebra( expression= meanG[,1:1], name="mean2")
modelACE2 <- mxModel( fitACE,mean2, name="mulIPAE2c" )
# then

mxEval(mean2, modelACE2, compute=TRUE)

That said, I really never get mxEval to work properly, but this is certainly a skill issue.

Best,
L

Replied on Sun, 09/15/2024 - 04:05
No user picture. JuanJMV Joined: 07/20/2016

In reply to by lf-araujo

Thank you so much If-araujo.

That works to make calculations but I need to use it in this line:

fsModel@expectation <- mxExpectationNormal("as", "mean2","threG",dimnames=selVars[1:7])

So, I think it must be computed.
I have tried to add it as a separate matrix but I got this error
Error en mxExpectationNormal("as", "mean2", mxMatrix(values = c(1, 2, :
'thresholds' argument must be a single matrix or algebra name

So, I think it must be computed and contained in the model.

I am re-running the model, so do not worry and thank you so much for your help!

With all good wishes,