Creating a function that creates OpenMx models

3 replies [Last post]
rabil's picture
Offline
Joined: 01/14/2010

I'm trying to write an R function that would create and run an OpenMx model. I can do this for a simple one factor model. I read in a data.frame for the data and construct all the paths and then do an mxRun. This works regardless of the number of variables in the data.frame. But I want to generate mxAlgebra statements (and other statements, like including parameters in an mxCI statement) which change depending on the number of variables. I've tried using "call" and can create individual mxAlgebra statements, but I cannot get mxModel to accept them when I try to generate them in a loop. What am I missing?

mspiegel's picture
Offline
Joined: 07/31/2009
I think this will help:
rabil's picture
Offline
Joined: 01/14/2010
Thanks but I'm not sure I

Thanks but I'm not sure I understand. I have no problem generating any number of mxAlgebra objects. The problem is, sometimes I might need 5 of them, sometimes 8, etc. I can write in a fixed number of mxAlgebra objects into the mxModel statement in the function. But how can I handle a variable number of mxAlgebra objects in the mxModel statement? Or do I have to construct the entire mxModel object in a similar fashion as you do for just the mxAlgebra object?

rabil's picture
Offline
Joined: 01/14/2010
Actually, I almost had it

Actually, I almost had it right. I had created the evaluated mxAlgebra's in a list but did not realize all I had to do was include the list name in the mxModel. (I was trying to use a loop through the list.) Your other comment that was referenced solved my problem. Thanks.