@submodels slot

2 replies [Last post]
carey's picture
Offline
Joined: 10/19/2009

run:

MZf <- mxModel(name="MZf", mxData(ObsCovMZf, type="cov", numObs=nMZf),
mxMLObjective(covariance='myModel.PreCovMZf'))
DZf <- mxModel(name="DZf", mxData(ObsCovDZf, type="cov", numObs=nDZf),
mxMLObjective(covariance='myModel.PreCovDZf'))
myModel1 <- mxModel(name="myModel", MZf, DZf)
myModel1

then run and compare:

MZf <- mxModel(name="MZf", mxData(ObsCovMZf, type="cov", numObs=nMZf),
mxMLObjective(covariance='myModel.PreCovMZf'))
DZf <- mxModel(name="DZf", mxData(ObsCovDZf, type="cov", numObs=nDZf),
mxMLObjective(covariance='myModel.PreCovDZf'))
myModel2 <- mxModel(name="myModel")
myModel2 <- mxModel(myModel2, MZf, DZf)
myModel2

myModel1 has only DZf as a submodel while myModel2 has both MZf and DZf as submodels. is this intentional? or should documentation or the MxMOdel constructor be changed?

Ryne's picture
Offline
Joined: 07/31/2009
Short version: either delete

Short version: either delete 'name=' in the top model/both models or change it to 'model=' and they'll both create a model 'myModel' with two submodels.

Long version: The issue you've happened across deals with the 'model' argument in mxModel, which will take the first unnamed argument in an mxModel statement unless it is explicitly stated. If the first unnamed argument is argument is a character string, it will be used to populate the 'name' argument. If it is an existing MxModel object, the contents of that MxModel object will be placed in the new model, and subsequent arguments will add or subtract to that model. Your first model above doesn't obliterate MZf, but rather puts the data and objective function from MZf into myModel1 and changes the name from 'MZf' to 'myModel'.

The central point is that the 'model' and 'name' arguments are different.

mspiegel's picture
Offline
Joined: 07/31/2009
This is not a bug. It is

This is not a bug. It is however an unfortunate interaction between the specification of the 1st argument of the mxModel() function and the "name=" argument. When this interaction was discovered, it was too late to change the behavior of the mxModel() function.

The function mxModel() behaves differently based on the type of the first argument:

(i) If the type is a string, then create a new model with that name.
(ii) If the type is a MxModel object, then modify that MxModel object.
(iii) If the type is a MxMatrix, MxAlgebra, MxConstraint, etc., then create a new model.
(iv) If the value is NA, then create a new model.

The first example creates myModel1 using case (ii) with the argument as MZf. The second example creates myModel2 by first using case (iv) and then using case (ii) with the argument as myModel2. The solution is to create myModel1 with mxModel("myModel", MZf, DZf).