# Breaking Square Bracket

6 posts / 0 new
Offline
Joined: 07/31/2009 - 15:26
Breaking Square Bracket

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.

Offline
Joined: 07/31/2009 - 15:14
I think this may have

I think this may have something to do with the dimensions of the data frame and the dimensions of the matrix into which data.foo and data.bar are being placed. Also, I am accustomed to seeing data.foo's and the like used as labels for matrix elements in an mxMatrix command, rather than bits of algebra in mxAlgebra commands. Note that the definition variable approach is designed to operate in a row-by-row fashion. That is, the definition variables corresponding to the *row* of data being analyzed are to be placed in specific matrix elements. In order to get rows of data from the dataframe into the matrix P, I would make P 1 by 2.

Nevertheless, I am also a bit stuck. First, the "Works" part of your script does not work for me (possibly I am using a later version of OpenMx). Second, when I try to fix it thusly:

> #-----------------------------
> # Works
>
> xdat <- data.frame(foo=rep(1, 10), bar=rep(2, 10))
> wmod <- mxModel(
+ name = 'WorkingModel',
+ mxData(observed=xdat, type='raw'),
+ mxMatrix(nrow=1, ncol=2,
+ values=c(.5,.3),labels=list('data.foo','data.bar'),
+ name='P'),
+ mxAlgebra("P",name="Fred"),
+ mxRowObjective(rowAlgebra='Fred',
+ rowResults='G',
+ reduceAlgebra='R'),
+ mxAlgebra(sum(G), name='R')
+ )
Error: 'labels' argument to mxMatrix must be a scalar, a vector, or a matrix.
> mxRun(wmod)@objective@result
Running WorkingModel
Error in mxRun(wmod) :
VECTOR_ELT() can only be applied to a 'list', not a 'character'
1: In mxRun(wmod) : NAs introduced by coercion
2: In mxRun(wmod) :
Internal Error: Algebra has been passed incorrectly: detected NoOp: (Operator Arg ...)

I end up with some errors that I am having difficulty parsing into my brain. Probably I have not understood mxRowObjective(). Like you, I await feedback from the coding team...

Offline
Joined: 07/31/2009 - 15:26
Sorry, the 'WorkingModel'

Sorry, the 'WorkingModel' only works with revision 1189. The mxRowObjective does not work with the latest binary release.

For your script, changing labels=list('data.foo', 'data.bar') to labels=c('data.foo', 'data.bar') should make it come up with a different error. The error says that labels should be a vector, c(...), and you set it as a list, list(...).

Also, I'm not trying to populate the P matrix. I'm trying to *select* the data.foo row and the data.bar column of the matrix P. Then I'd like to put this element of the P matrix into the algebra 'Fred'.

This is an odd thing to do, but since data.foo is all 1's and data.bar is all 2's the objective should come out the same as using mxAlgebra(P[1, 2], name='Fred'), which works.

Offline
Joined: 07/31/2009 - 15:14
I see... have svn'd up to

I see... have svn'd up to 1191 and your error is reproducible, and I agree that it looks like a bug, since data.bar ought to have populated Fred with a 2.

Michael S, any ideas?

Offline
Joined: 07/31/2009 - 15:10
It looks like this is a

It looks like this is a strange interaction with the mxRowObjective, rather than a problem with the square bracket operator. Similar cases seem to work in FIMLObjective functions.

There's a reason that Row Objective isn't in the documented function set yet--it's still fragile and untested.

I'll take a look at it when I can and get back to you about the fix.

Offline
Joined: 07/31/2009 - 15:24
Oops. It was a bug in my

Oops. It was a bug in my code. Thanks to Tim for finding it. It's been fixed in the repository: http://openmx.psyc.virginia.edu/dev/changeset/1194.