# Square bracket substitution implemented

6 posts / 0 new
Offline
Joined: 07/31/2009 - 15:24
Square bracket substitution implemented

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))
Offline
Joined: 07/31/2009 - 14:25
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

Offline
Joined: 07/31/2009 - 15:14
(thumbs up icon) neale likes

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

Offline
Joined: 07/31/2009 - 14:25
\parting is such sweet sorrow

\parting is such sweet sorrow :-)

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

Offline
Joined: 07/30/2009 - 14:03

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.

<

pre>
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.

Offline
Joined: 07/31/2009 - 15:24