To add a constraint to a model, simply call `mxConstraint(expression, name = NA)`

where "expression" is an R expression of matrix operators and matrix functions and name is an optional character string. e.g.

mxConstraint(ma ==mf, name = "equate_ma_and_mf)

mxConstraints define relationships between two MxAlgebra or MxMatrix objects. The constraint is used to affect the estimation of free parameters in the referenced objects.

Constraints are written identically to mxAlgebra expressions, however the outermost operator must be either ‘<’, ‘==’ or ‘>’. i.e., these are the only three relationships to which parameters can be constrained.

To affect an estimation or optimization, the mxConstraint must be included in an mxModel, along with the referenced mxAlgebra and mxMatrix's. For instance:

# Constrain a matrix of free parameters 'K' to be equal to matrix 'limit'

model <- mxModel("con_test", mxMatrix(type="Full", nrow=2, ncol=2, free=T, name="K"), mxMatrix(type="Full", nrow=2, ncol=2, free=F, name="limit", values=1:4), mxConstraint(K == limit, name = "Klimit_equality"), mxAlgebra(min(K), name="minK"), mxAlgebraObjective("minK") ) fit <- mxRun(model) fit@matrices$K@values

For efficiency, mxConstraints should not be used to constrain free MxMatrix or MxAlgebra parameters which could be equated by name or by position.

How?

You can constrain parameters in the same mxModel to equality by giving them the same label in their 'labels' matrices, or by setting the label of one parameter to the address of the other. Examples are:

Here, because two parameters share the label "param" both will have the same value:

mxMatrix("Full", 1, 1, free=TRUE, values=1, labels="param", name="F") mxMatrix("Full", 1, 1, free=TRUE, values=1, labels="param", name="G")

Here, because the label of G[1,1] "point to" the location of cell [1,1] of algebra "logP", they will be forced to be equal: Because G[1,1] is fixed, this has the effect of constraining log(start) == 1.

mxMatrix("Full", 1, 1, free=FALSE, values=1, labels="logP[1,1]", name="G") mxAlgebra(log(start), name="logP")

Why not?

The warning is one from a numerical efficiency perspective. If one labels two parameters the same, the optimizer has one fewer parameter to optimize and will execute more rapidly. If an mxConstraint is used to do the same thing, both parameters remain estimated and a Lagrangian multiplier is added to maintain the constraint. This constraint also has to have its gradients computed and the order of the Hessian grows as well. So while both approaches should work, the mxConstraint() will take longer to do so.

- Log in or register to post comments
- Talk
- Printer-friendly version