I'm not sure if this is the right place for this. I'm getting an error message, but I'm fairly confidant that the error is due to the current capabilities of OpenMx, and not to user error.
Given some mxMatrix P and the definition variables foo and bar, the following works:
mxAlgebra(P[data.foo, ], name='Work')
But the following breaks:
mxAlgebra(P[data.foo, data.bar], name='Break')
Here are some simplified test cases:
-----------------------------
Works
xdat <- data.frame(foo=rep(1, 10), bar=rep(2, 10))
wmod <- mxModel(
name = 'WorkingModel',
mxData(observed=xdat, type='raw'),
mxMatrix(nrow=10, ncol=2,
values=c(rep(0.5, 10), rep(0.3, 10)),
name='P'),
mxAlgebra(P[data.foo, 2], name='Fred'),
mxRowObjective(rowAlgebra='Fred',
rowResults='G',
reduceAlgebra='R'),
mxAlgebra(sum(G), name='R')
)
mxRun(wmod)@objective@result
-----------------------------
Breaks
xdat <- data.frame(foo=rep(1, 10), bar=rep(2, 10))
bmod <- mxModel(
name = 'BreakingModel',
mxData(observed=xdat, type='raw'),
mxMatrix(nrow=10, ncol=2,
values=c(rep(0.5, 10), rep(0.3, 10)),
name='P'),
mxAlgebra(P[data.foo, data.bar], name='Fred'),
mxRowObjective(rowAlgebra='Fred',
rowResults='G',
reduceAlgebra='R'),
mxAlgebra(sum(G), name='R')
)
mxRun(bmod)@objective@result
Running BreakingModel
Error in mxRun(bmod) :
Attempted to set improper value (1, 1) from (10, 0) matrix.
End Test cases
-----------------------------
The only difference is using data.bar instead of the number 2. It appears that definition variables work with square bracket, but you can only use one definition variable with them.
I know in the current case this is a silly thing to be doing with models, but later I want to optimize the values of the P matrix based on some definition variables. A non-optimizing case needs to works before I can be concerned with optimization.