Square bracket substitution implemented

Posted on
Picture of user. mspiegel Joined: 07/31/2009
Square bracket substitution has been implemented in the subversion repository. This is a feature that allows you to construct MxMatrix objects, and within the labels of a matrix use a string of the form "foo[row,col]" where 'foo' refers to another MxMatrix or MxAlgebra and 'row','col' must be numeric literals. It is very important to keep in mind that this only applies to matrix labels. You cannot use this notation in mxAlgebra expressions. Here are some examples of this notation:

require(OpenMx)
A <- mxMatrix('Full', 1, 1, values = 1, name = 'A')
B <- mxMatrix('Full', 1, 1, values = 2, name = 'B')
C <- mxMatrix('Full', 1, 1, values = 3, name = 'C')
D <- mxMatrix('Full', 3, 1, labels = c('A[1,1]', 'B[1,1]', 'C[1,1]'), name = 'D')
model <- mxModel('model', A, B, C, D)
model <- mxRun(model)
omxCheckEquals(mxEval(D, model), as.matrix(c(1,2,3)))
A <- mxMatrix('Full', 2, 2, values = c(1,2,3,4), byrow = TRUE, name = 'A')
B <- mxAlgebra(A + A, name = 'B')
C <- mxMatrix('Full', 2, 2, labels = c('B[2,2]', 'B[2,1]', 
        'B[1,2]', 'B[1,1]'), byrow = TRUE, name = 'C')
model <- mxModel('model', A, B, C)
model <- mxRun(model)
omxCheckEquals(mxEval(C, model), matrix(rev(c(1,2,3,4)) * 2, 2, 2, byrow = TRUE))
Replied on Thu, 09/24/2009 - 17:57
Picture of user. tbates Joined: 07/31/2009

nifty!

Put some helper info on the [the wiki](http://openmx.psyc.virginia.edu/wiki/mxmatrix-help) as to how this seemed to work to me, and why one might wish to use it.

I wondered if it was possible to equate across groups by saying `groupName.MatrixName[r,c]` ... and it works: nice!


A = mxMatrix('Full', 2, 2, values = c(1,3,2,4), name = 'A')
B = mxAlgebra(A + A, name = 'B')
C = mxMatrix(name = 'C', 'Full', 2, 2, byrow = TRUE,
labels = c('modelA.B[2,2]', 'modelA.B[2,1]',
'modelA.B[1,2]', 'modelA.B[1,1]')
)

modelA = mxModel('modelA', A, B)
modelB = mxModel('modelB', C)
modelC = mxModel(modelA, modelB)

modelC = mxRun(modelC)

mxEval(modelB.C, modelC)
[,1] [,2]
[1,] 8 6
[2,] 4 2

Replied on Sun, 11/29/2009 - 14:29
Picture of user. Steve Joined: 07/30/2009

When we last talked about multilevel models in "long format" data, it was suggested that square bracket substitution could work for this. But it appears that definition variables cannot currently be used as indices. The following fails in R.

multilevelModel2 <- mxModel("Multilevel_2",
    mxMatrix("Full", numSubjects, 3, 
        values=.2,
        free=TRUE, 
        name="Rand"
    ),
    mxMatrix("Full", 4, 4, 
        values=c(  0,  0,
                   0,  0), 
        labels=c(NA,  NA,
                "Rand[data.ID,1]", NA), 
        free=c(F, F,
               F, F), 
        name="A", 
        byrow=TRUE
    )
)
<\pre>

MultilevelLongFormatFailing.R has been placed in models/failing.
Replied on Sun, 11/29/2009 - 16:55
Picture of user. mspiegel Joined: 07/31/2009

In reply to by Steve

OK, I've made several changes to MultilevelLongFormatFailing.R to get the model to run. Check to see if the output is correct and add omxCheckCloseEnough() statements to verify the output. Some issues to address are:

1) We do not support definition variables appearing in square-brackets in MxMatrices (or rather in the labels matrix of an MxMatrix object). It might be convenient to support this feature. I believe it's non-trivial to implement, I need to consult with Tim Brick on the issue.

2) We do not support definition variables appearing in square-brackets in MxAlgebra expressions. This is an oversight on my part. You'll see in MultilevelLongFormatFailing.R how "currentID" is defined as a 1 x 1 matrix with the definition variable and then "currentID" is used inside the square brackets. It should be relatively easy to implement autoboxing of definition variables into 1 x 1 matrices.

3) When I use the definition variable in the 1 x 1 matrix "currentID", I need to explicitly specify the starting value of the matrix as the first value of the definition variable. If I don't specify the starting value, then conformability checking will fail. Perhaps we should automate this process.