Attachment | Size |
---|---|
![]() | 53.87 KB |
Hi!
I am trying to constrain the results of an algebra, either to a fixed value or constraining the results of two algebras to be equivalent. I am using https://openmx.ssri.psu.edu//wiki/mxConstraint-help as a starting point.
Example output is attached, but the relevant lines of code are below
I want to fix AlCO[2,2] to 0 in the first example.
nl <- 3 # number of latent factors # Matrices ac, cc, and ec to store a, c, and e path coefficients for latent phenotype(s) XMN <- mxMatrix(type="Lower", nrow=nl, ncol=nl, free=TRUE, values=c(.6, .1, .1, .1, .1, 0), labels=c("x11MN", "x21MN", "x22MN", "x31MN", "x32MN", "x33MN"), name="XMN" ) YMN <- mxMatrix(type="Lower", nrow=nl, ncol=nl, free=TRUE, values=c(.6, .1, .1, .1, .1, .1), labels=c("y11MN", "y21MN", "y22MN", "y31MN", "y32MN", "y33MN"), name="YMN" ) ZMN <- mxMatrix(type="Lower", nrow=nl, ncol=nl, free=TRUE, values=c(.6, .1, .1, .1, .1, .1), labels=c("z11MN", "z21MN", "z22MN", "z31MN", "z32MN", "z33MN"), name="ZMN" ) XCO <- mxMatrix(type="Lower", nrow=nl, ncol=nl, free=TRUE, values=c(.6, .1, .1, .1, .1, 0), labels=c("x11CO", "x21CO", "x22CO", "x31CO", "x32CO", "x33CO"), name="XCO" ) YCO <- mxMatrix(type="Lower", nrow=nl, ncol=nl, free=TRUE, values=c(.6, .1, .1, .1, .1, .1), labels=c("y11CO", "y21CO", "y22CO", "y31CO", "y32CO", "y33CO"), name="YCO" ) ZCO <- mxMatrix(type="Lower", nrow=nl, ncol=nl, free=TRUE, values=c(.6, .1, .1, .1, .1, .1), labels=c("z11CO", "z21CO", "z22CO", "z31CO", "z32CO", "z33CO"), name="ZCO" ) AlMN <- mxAlgebra(XMN %*% t(XMN), name="AlMN") ClMN <- mxAlgebra(YMN %*% t(YMN), name="ClMN") ElMN <- mxAlgebra(ZMN %*% t(ZMN), name="ElMN") AlCO <- mxAlgebra(XCO %*% t(XCO), name="AlCO") ClCO <- mxAlgebra(YCO %*% t(YCO), name="ClCO") ElCO <- mxAlgebra(ZCO %*% t(ZCO), name="ElCO") CO_ACons2 <- mxMatrix("Full", 1, 1, free=FALSE, values=0, labels="AlCO[2,2]", name="CO_ACons2")
Example 1 runs but the output of the constrained correlation matrix is no different than the base model (seen in attached output)
In this second example, I want to constrain corLMN[1,2] and corLCO[1,2] to be the same.
VarLMN <- mxAlgebra(expression = AlMN + ClMN + ElMN, name="VarLMN") VarLCO <- mxAlgebra(expression = AlCO + ClCO + ElCO, name="VarLCO") corLMN <- mxAlgebra(cov2cor(VarLMN), name="corLMN") corLCO <- mxAlgebra(cov2cor(VarLCO), name="corLCO") MN_CorCons12 <- mxMatrix("Full", 1, 1, free=TRUE, labels="corLMN[1,2]", name="MN_CorCons12") CO_CorCons12 <- mxMatrix("Full", 1, 1, free=TRUE, labels="corLCO[1,2]", name="CO_CorCons12") CorCons12 <- mxConstraint(MN_CorCons12 == CO_CorCons12, name="CorCons12")
The model does not run in this example, I get an error "entity unknown"
Unfortunately, the wiki is extremely out-of-date. What it says may not be accurate for OpenMx version 2.x.
Try replacing this,
, with this,
. Try replacing this,
, with this,
I think the syntax in your second example ought to work, so you may have discovered a bug. I suspect the syntax in your first example hasn't worked since before version 1.3, if it ever actually did.
This should also work:
It's sort of a like a matrix element containing a definition variable: the element doesn't have a free value of its own, but rather, its value is "fixed" to the value of whatever its label references (in this case, an algebra element).
Seems to me that we should warn users when using labels to constrain elements that have been declared as fixed (say because they are the results of an algebra - it's a reasonable state to guess) that it's not going to work. Or we should make it work either way. Lacking repair, we should issue either a warning or error, methinks.
OpenMx's behavior in the first example seems reasonable to me. Consider this short script:
The sole element of 'foo' is initialized at 1, per argument to
mxMatrix()
. If computed beforemxRun()
, 'foo' is the value of what it references, at the start values. AftermxRun()
It nominally retains that value, but if computed, it returns the value of what it references, at the solution.Thanks for the help!
The above does work!
I'm still trying to find a solution to constrain the solutions of two algebras to be equivalent. I have tried three things suggested here.
For this code, the model runs but there is no difference in output.
When I run the code below this, the model does not run and results in “Error: The label with square brackets has been assigned to a free parameter in matrix 'MZMN.MN_CorCons1' at row 1 and column 1”
When I run the code below this, the model does not run and results in “Error: Unknown reference 'corLMN' detected in the entity 'CorCons12' in model 'LinearGrowthACE'”
As discussed upthread, that syntax is only supposed to work with
free=F
. As for the other two blocks of code, if you're putting the new MxMatrices and MxConstraints directly into the container model, you may need to prefix the names of algebras that are in submodels appropriately, e.g. "MZMN.corLMN[1,2]" and "MZCO.corLCO[1,2]".