You are here

mxConstraint-help

Primary tabs

Wiki home page####Constraints

To add a constraint to a model, use mxConstraint.

mxConstraint takes an expression – a valid algebra defining the relationship between two mxAlgebra or mxMatrix objects. It is also useful to set the (optional) name parameter can be used elsewhere

. e.g.

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

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.

Example

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

  1. Constrain a matrix of free parameters 'K' to be equal to matrix 'limit'
model <- mxModel("con_test", 
    mxMatrix(name="K", "Full", nrow = 2, ncol = 2, free = TRUE),
    mxMatrix(name="limit", "Full", nrow = 2, ncol = 2, free = FALSE, values = 1:4),
    mxConstraint(name = "Klimit_equality", K == limit), 
    mxAlgebra(name="minK", min(K)), 
    mxFitFunctionAlgebra("minK")
 
)
fit <- mxRun(model)
fit@matrices$K@values
 
     [,1] [,2]
[1,]    1    3
[2,]    2    4

Efficiency Note

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

How can I use labels to implement a constraint on the model?

Parameters within an mxModel can be constrained to equality by giving them the same label (in their 'labels' matrix), or by setting the label of one parameter to the square brackets address of the other.

For example, here, because two parameters share the label param both will have the same value:

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

Why avoid mxConstraints where labels will do?

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 equate two parameters, both 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.