mxEval(formula, model, environment = model) # add environment

tbates's picture
Project:OpenMx
Component:Code
Category:feature request
Priority:minor
Assigned:tbates
Status:active
Description

mxEval looks in the parent environments to find objects. This can be handy, allowing reference to such objects in algebra. However, when used in the context of getting things out of models (quite a common use-case), it can be dangerous: mxEval might return an object with the same name from the environment, rather than from inside the model you have set as the target.

Example. Say you have a matrix

m1$submodels$top$matrices$nVar$values
    [,1]
[1,]    1

If you also have a variable nVar in my environment, set to whatever it happens to be set to, say "2", then mxEval() doesn't return the nVar matrix from the model, but rather nVar from the environment:

nVar = 2
mxEval(nVar, m1)

(parenthetically, I had thought it searched exhaustively inside the model before traversing the parent environment, but this is not the case.)

[1] 2
m1$submodels$top$matrices$nVar$values
    [,1]
[1,]    1

i.e., it has discovered nVar in the environment, even though it exists in the submodel, and there’s no warning.

Did that used to happen?

I suggest we add an option to mxEval(), something like “env = 'self’ ” for when we’d rather get an “object not found” error than risk mistakenly accessing a variable from other environments. It's really pretty hairy...

then this would occur:

mxEval(nVar, m1, env=’self’)
# error: var not found
 
mxEval(nVar, m1$top, env=’self’)
    [,1]
[1,]    1
 
mxEval(nVar, m1, env=global)
2

Comments

mhunter's picture

#1

Priority:normal» minor
Assigned to:Anonymous» tbates

I'm fine with this. If you feel strongly about it, then implement it.