Square bracket substitution implemented
Posted on
mspiegel
Joined: 07/31/2009
Forums
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))
nifty!Put some helper info
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
Log in or register to post comments
In reply to nifty!Put some helper info by tbates
(thumbs up icon) neale likes
Log in or register to post comments
In reply to (thumbs up icon) neale likes by neale
\parting is such sweet sorrow
The next piebald guinea pig substitute for darts... Shakespearian elegiac algebra
... (2*b) || !(2*b)?
Log in or register to post comments
When we last talked about
Log in or register to post comments
In reply to When we last talked about by Steve
OK, I've made several changes
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.
Log in or register to post comments