Appending mxAlgebra expressions

Posted on
Picture of user. Lawrence L Lo Joined: 02/24/2010
Hello,

I am trying to generate mxAlgebra expressions from a size variable. I have a variable amount of mx submodels and am trying to add the objectives from these together for a full model objective. I have tried things like
mxAlgebra(sum(get(paste("submodel",1:x,".objective",sep=""))),
name="TotObj")
and other various methods. I also tried the method described on
http://openmx.psyc.virginia.edu/thread/537
but don't think this will help in my particular situation.
Any suggestions?

Best,
-LLL

Replied on Mon, 02/07/2011 - 20:24
Picture of user. mspiegel Joined: 07/31/2009

The trick is to construct the expression outside of the mxAlgebra() call and then feed the expression into mxAlgebra(). The mxAlgebra() will only recognize matrix operators or functions, so it barfs on the get() and the paste(). Try this:

modelnames <- c("model1", "model2", "model3")
objectives <- paste(modelnames, "objective", sep = ".")
objectives <- paste(objectives, collapse = " + ")
expression <- paste("mxAlgebra(", objectives, ", name = 'TotObj')", sep = "")
algebra <- eval(parse(text=expression))