Referring to an OpenMx matrix in the OpenMx environment

1 reply [Last post]
mverdam's picture
Offline
Joined: 02/04/2014

I'm trying to run a model in which I have several matrices that should contain the same parameter estimates (they overlap partly). I know I can use labels or mxConstraint to put equality constraints on parameter estimates, but I was wondering if it is also possible to refer directly to the parameter estimates of a matrix in other parts of the OpenMx environment. For example, I have tried to build an mxAlgebra that refers to the parameter estimates of an mxMatrix by using:

mxAlgebra(expression=cbind(Model.mxMatrix[,i],Model.mxMatrix[,j]), name="part_mxMatrix")

but this doesn't seem to work.

If anybody has any suggestion on how to make this work, it is more than welcome!

mhunter's picture
Offline
Joined: 07/31/2009
The following code

The following code works.

require(OpenMx)
 
m1 <- mxMatrix(name="A", nrow=1, ncol=2, values=c(0, 1), labels=c("a1", "a2"))
m2 <- mxMatrix(name="B", nrow=2, ncol=1, values=c(3, 4), labels=c("b1", "b2"))
 
alg1 <- mxAlgebra(name="OneWay", expression=cbind(A[1,1], B[1,1]))
 
alg2 <- mxAlgebra(name="AnotherWay", expression=cbind(a1, b1))
 
mod <- mxModel(name="Algebra that involves entries of mxMatrix",
	m1, m2,
	alg1, alg2
)
 
mxEval(OneWay, mod, compute=TRUE)
mxEval(AnotherWay, mod, compute=TRUE)

Based on your example I think I know where you might be heading wrong. In OpenMx, you can refer to the entries of an mxMatrix either by the labels associated with that entry, or by the name of the matrix followed by square brackets (e.g. A[1,2]). However, the square brackets don't work quite like they do in the rest of R. The square brackets must contain numeric literals only, no variables (e.g. A[i,j] ) and no blanks (e.g. A[,]).

If you want the entire row of a matrix, you could refer to each element of that row or you could create a matrix, C, of ones and zeros that selects out the desired row via matrix multiplication (i.e. A[i,] == C %*% A).

Here's an R code snippet of row extraction via matrix multiplication.

A <- matrix(rnorm(20), 4, 5)
C <- matrix(c(0, 1, 0, 0), 1, 4)
C %*% A
A[2,]
all(C %*% A == A[2,])