Skip to content

BUG: Evaluate second formulas with the model's eval_env - #11225

Merged
bashtage merged 6 commits into
statsmodels:mainfrom
bashtage:wip-formula-eval-env-secondary
Nov 26, 2026
Merged

bashtage merged 6 commits into
statsmodels:mainfrom
bashtage:wip-formula-eval-env-secondary

Conversation

@bashtage

Copy link
Copy Markdown
Member

Stacked on #11223, merge that first (its three commits are the first ones here).

BUG: Give eval_env the same meaning in every from_formula

eval_env in from_formula selects the namespace in which variables of
the formula that are not in the data are looked up. An integer meant a
different namespace in different models:

  • Model.from_formula (ols, glm, logit, ...): 1 is the namespace
    that calls from_formula, 2 the one above it.
  • Models whose from_formula calls their parent's through
    advance_eval_env (GEE, NominalGEE, OrdinalGEE, PHReg, MixedLM,
    ConditionalLogit, ConditionalMNLogit, ConditionalPoisson, QIF,
    OrderedModel, BetaModel): one frame lower, so 1 looked inside
    statsmodels and 2 was the calling namespace. In MixedLM, re_formula
    and vc_formula already counted like Model.from_formula, so one integer
    meant two namespaces in the same model.
  • RollingOLS.from_formula: one frame higher, so 0 was the calling
    namespace and the default (None) silently looked in the namespace above
    the caller, where a variable of the same name would be used. A dict
    raised a TypeError.

The docstrings said that "the default eval_env=0 uses the calling
namespace", which was true of no model.

Behavior changes

  • Every model follows Model.from_formula: None or 1 is the namespace
    that calls from_formula, an integer k the namespace k stack frames
    above it, -1 an empty namespace, and a dict holds the variables. Code
    that passed an integer to the models above now refers to another
    namespace (pass 1 instead of 2, or None, for the calling namespace).
  • advance_eval_env moves integers up a frame, as it did for None.
  • RollingOLS.from_formula uses the calling namespace by default, counts
    integers like the other models, and accepts a dict.
  • numpy integers count like Python integers (they were not moved up in
    Model.from_formula).

Not changed

  • None, -1 and dicts mean what they meant in every model except
    RollingOLS (above), and variables in the data are found first, so a
    formula whose variables are all in the data gives the same model.

Tests

  • For both formula engines and ols, gee, phreg, mixedlm (formula
    and re_formula), conditional_logit, QIF, OrderedModel,
    BetaModel and RollingOLS: a variable defined in the calling
    namespace and, with other values, one frame above it is taken from the
    calling namespace for None, 1 and np.int64(1), and from the frame above
    for 2; a dict is used, and a name in neither the data nor the dict is not
    found. The integer cases of every model but ols, and the default of
    RollingOLS, fail on main.

DOC: Describe eval_env in the formula guide

The Namespaces section of the formula guide suggested passing a patsy EvalEnvironment, which is deprecated as an evaluation environment, and spelled the keyword as eval_func. It now lists what eval_env accepts (None or 1 for the calling namespace, an integer number of stack frames, a dict, and -1 for an empty namespace), says that it means the same in every model, and that variables in the data are found first.

Documentation only.

REF: Translate eval_env for get_matrices in one place

RollingOLS.from_formula and the re_formula and vc_formula of
MixedLM.from_formula call FormulaManager.get_matrices in their own
body, and each translated eval_env for it with the same few lines (None
is the calling namespace, -1 an empty namespace). A helper,
statsmodels.formula.formulatools.get_matrices_eval_env, does it once, as
advance_eval_env does for a call to the parent's from_formula. Other
from_formula methods that evaluate their own formulas use it in the
follow-up pull requests.

No change in behavior: the tests of the previous commit and the MixedLM and
RollingOLS suites pass unchanged.

BUG: Select the subset rows for BetaModel's precision formula

BetaModel.from_formula with exog_precision_formula and a subset
keyword (even subset=None) raised
AttributeError: 'DataFrame' object has no attribute 'ix': the rows for
the precision formula were selected with DataFrame.ix, which pandas
removed in 1.0.

Changes

  • The precision formula is evaluated on data.loc[subset], the rows the
    formula of the mean uses, or on all of data when subset is None.

Not changed

  • Without subset, the model is the same as before.

Tests

  • With a subset of 20 of 30 rows and with subset=None: the design of the
    mean and of the precision equal the rows of the data written out by hand.
    Both fail on main.

BUG: Evaluate second formulas with the model's eval_env

Some from_formula methods evaluate further formulas themselves. These
ignored eval_env and looked variables that are not in the data up in
statsmodels' own code, so they were not found:

  • BetaModel.from_formula: exog_precision_formula.
  • ProcessMLE.from_formula: scale_formula, smooth_formula and
    noise_formula, and also the mean formula, because eval_env was not
    passed on at all.
  • GEE.from_formula (and NominalGEE, OrdinalGEE): dep_data given as a
    formula.

Changes

  • These formulas use the same namespace as the model's formula: None or 1
    is the namespace that calls from_formula, an integer k the namespace k
    stack frames above it, -1 an empty namespace, and a dict holds the
    variables.
  • They use get_matrices_eval_env for this, and ProcessMLE passes
    eval_env on to the mean formula with advance_eval_env.
  • The docstrings say so; ProcessMLE.from_formula documents eval_env.

Not changed

  • Variables in the data are found first, so formulas that worked before
    give the same matrices.

Tests

  • For both engines and each of these formulas: a variable defined in the
    calling namespace and, with other values, one frame above it, is taken
    from the calling namespace for None and 1 and from the frame above for 2;
    a dict is used; -1 finds nothing. All fail on main.

  • get_matrices_eval_env(-1) is an empty namespace (pd, which
    statsmodels' own code has, is not found) and None is the calling
    namespace.

  • tests added / passed.

  • code/documentation is well formatted.

  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: mapping which namespace eval_env selects in every from_formula, writing the change and its tests, and checking them (tests failing on main, a mutation check of the changed lines, and the tests in the oldest, middle, latest and formulaic-only environments).
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

`eval_env` in `from_formula` selects the namespace in which variables of
the formula that are not in the data are looked up. An integer meant a
different namespace in different models:

- `Model.from_formula` (`ols`, `glm`, `logit`, ...): 1 is the namespace
  that calls `from_formula`, 2 the one above it.
- Models whose `from_formula` calls their parent's through
  `advance_eval_env` (GEE, NominalGEE, OrdinalGEE, PHReg, MixedLM,
  ConditionalLogit, ConditionalMNLogit, ConditionalPoisson, QIF,
  OrderedModel, BetaModel): one frame lower, so 1 looked inside
  statsmodels and 2 was the calling namespace. In MixedLM, `re_formula`
  and `vc_formula` already counted like `Model.from_formula`, so one integer
  meant two namespaces in the same model.
- `RollingOLS.from_formula`: one frame higher, so 0 was the calling
  namespace and the default (None) silently looked in the namespace above
  the caller, where a variable of the same name would be used. A dict
  raised a TypeError.

The docstrings said that "the default ``eval_env=0`` uses the calling
namespace", which was true of no model.

Behavior changes
- Every model follows `Model.from_formula`: None or 1 is the namespace
  that calls `from_formula`, an integer k the namespace k stack frames
  above it, -1 an empty namespace, and a dict holds the variables. Code
  that passed an integer to the models above now refers to another
  namespace (pass 1 instead of 2, or None, for the calling namespace).
- `advance_eval_env` moves integers up a frame, as it did for None.
- `RollingOLS.from_formula` uses the calling namespace by default, counts
  integers like the other models, and accepts a dict.
- `numpy` integers count like Python integers (they were not moved up in
  `Model.from_formula`).

Not changed
- None, -1 and dicts mean what they meant in every model except
  `RollingOLS` (above), and variables in the data are found first, so a
  formula whose variables are all in the data gives the same model.

Tests
- For both formula engines and `ols`, `gee`, `phreg`, `mixedlm` (formula
  and `re_formula`), `conditional_logit`, `QIF`, `OrderedModel`,
  `BetaModel` and `RollingOLS`: a variable defined in the calling
  namespace and, with other values, one frame above it is taken from the
  calling namespace for None, 1 and `np.int64(1)`, and from the frame above
  for 2; a dict is used, and a name in neither the data nor the dict is not
  found. The integer cases of every model but `ols`, and the default of
  `RollingOLS`, fail on main.
The Namespaces section of the formula guide suggested passing a patsy EvalEnvironment, which is deprecated as an evaluation environment, and spelled the keyword as eval_func. It now lists what eval_env accepts (None or 1 for the calling namespace, an integer number of stack frames, a dict, and -1 for an empty namespace), says that it means the same in every model, and that variables in the data are found first.

Documentation only.
`RollingOLS.from_formula` and the `re_formula` and `vc_formula` of
`MixedLM.from_formula` call `FormulaManager.get_matrices` in their own
body, and each translated `eval_env` for it with the same few lines (None
is the calling namespace, -1 an empty namespace). A helper,
`statsmodels.formula.formulatools.get_matrices_eval_env`, does it once, as
`advance_eval_env` does for a call to the parent's `from_formula`. Other
`from_formula` methods that evaluate their own formulas use it in the
follow-up pull requests.

No change in behavior: the tests of the previous commit and the MixedLM and
RollingOLS suites pass unchanged.
`BetaModel.from_formula` with `exog_precision_formula` and a `subset`
keyword (even `subset=None`) raised
`AttributeError: 'DataFrame' object has no attribute 'ix'`: the rows for
the precision formula were selected with `DataFrame.ix`, which pandas
removed in 1.0.

Changes
- The precision formula is evaluated on `data.loc[subset]`, the rows the
  formula of the mean uses, or on all of `data` when `subset` is None.

Not changed
- Without `subset`, the model is the same as before.

Tests
- With a subset of 20 of 30 rows and with `subset=None`: the design of the
  mean and of the precision equal the rows of the data written out by hand.
  Both fail on main.
Some `from_formula` methods evaluate further formulas themselves. These
ignored `eval_env` and looked variables that are not in the data up in
statsmodels' own code, so they were not found:

- `BetaModel.from_formula`: `exog_precision_formula`.
- `ProcessMLE.from_formula`: `scale_formula`, `smooth_formula` and
  `noise_formula`, and also the mean `formula`, because `eval_env` was not
  passed on at all.
- `GEE.from_formula` (and NominalGEE, OrdinalGEE): `dep_data` given as a
  formula.

Changes
- These formulas use the same namespace as the model's formula: None or 1
  is the namespace that calls `from_formula`, an integer k the namespace k
  stack frames above it, -1 an empty namespace, and a dict holds the
  variables.
- They use `get_matrices_eval_env` for this, and `ProcessMLE` passes
  `eval_env` on to the mean formula with `advance_eval_env`.
- The docstrings say so; `ProcessMLE.from_formula` documents `eval_env`.

Not changed
- Variables in the data are found first, so formulas that worked before
  give the same matrices.

Tests
- For both engines and each of these formulas: a variable defined in the
  calling namespace and, with other values, one frame above it, is taken
  from the calling namespace for None and 1 and from the frame above for 2;
  a dict is used; -1 finds nothing. All fail on main.
- `get_matrices_eval_env(-1)` is an empty namespace (`pd`, which
  statsmodels' own code has, is not found) and None is the calling
  namespace.
@bashtage
bashtage marked this pull request as ready for review November 26, 2026 09:57
@bashtage
bashtage merged commit 5b7b99e into statsmodels:main Nov 26, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-formula-eval-env-secondary branch November 26, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant