Skip to content

BUG: Give eval_env the same meaning in every from_formula - #11223

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

bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-formula-eval-env-ints

Conversation

@bashtage

Copy link
Copy Markdown
Member

First of three PRs that make eval_env mean the same in every from_formula; the other two are stacked on this one.

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.

  • 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.
@bashtage
bashtage marked this pull request as ready for review November 26, 2026 09:56
@bashtage
bashtage merged commit d62807a into statsmodels:main Nov 26, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-formula-eval-env-ints 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