statnet / ergm

Fit, Simulate and Diagnose Exponential-Family Models for Networks
Other
94 stars 36 forks source link

Use concatenation of ergm_model and ergm_conlist objects instead of formula alchemy whenever possible. #475

Open krivit opened 1 year ago

krivit commented 1 year ago

Formulas come with an environment in which their terms should be evaluated, e.g., ~nodefactor(x), where x is a variable present in the formula's environment. If the environment changes, the formula may no longer evaluate as intended.

When combining two formulas, there is no unique way to combine their environments, unless one of them is trivial (e.g., baseenv or globalenv) or known to be unused (e.g., known to contain specific terms . statnet.common::nonsimp_update.formula() offers a number of options, but every option has the possibility of breaking something.

Thus, we should, in general, avoid concatenating formulas. Currently, the "unsafe" concatenations are done in .handle.auto.constraints() and, to an extent, ergm(), though the latter is only a problem if the RHS of the formula references TARGET_STATS. In principle, it should be able to rename it to something else that's not a regularly used variable name.

Auto constraints is a bit more complicated, since the API makes use of dot-substitution of the %ergmlhs% constraints into the constraints= constraints. We might therefore need some method for passing multiple formulas that are processed into constraint lists and then dot-substituted.