Appending mxAlgebra expressions

2 replies [Last post]
Lawrence L Lo's picture
Offline
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

mspiegel's picture
Offline
Joined: 07/31/2009
The trick is to construct the

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))

Lawrence L Lo's picture
Offline
Joined: 02/24/2010
Thanks a bunch. This did the

Thanks a bunch. This did the trick.