Square bracket substitution implemented

5 replies [Last post]
mspiegel's picture
Offline
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))

Steve's picture
Offline
Joined: 07/30/2009
When we last talked about

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.

mspiegel's picture
Offline
Joined: 07/31/2009
OK, I've made several changes

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.

tbates's picture
Online
Joined: 07/31/2009
nifty! Put some helper info

nifty!
Put some helper info on the wiki as to how this seemed to work to me, and why one might wish to use it.

http://openmx.psyc.virginia.edu/wiki/mxmatrix-help-0

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

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

modelA <- mxModel('modelA', A, B)
modelB <- mxModel('modelB', C)
model = mxModel(modelA,modelB)
fit <- mxRun(model)

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

neale's picture
Offline
Joined: 07/31/2009
(thumbs up icon) neale likes

(thumbs up icon) neale likes this. We can now part with \part from Mx1, yay!

tbates's picture
Online
Joined: 07/31/2009
\parting is such sweet sorrow

\parting is such sweet sorrow :-)

The next piebald guinea pig substitute for darts... Shakespearian elegiac algebra
... (2*b) || !(2*b)?