Skip to content

Suffix by variables named like an output column (marginaleffects backend) - #678

Merged
strengejacke merged 8 commits into
easystats:mainfrom
jmgirard:focal-names-output-columns
Oct 14, 2026
Merged

strengejacke merged 8 commits into
easystats:mainfrom
jmgirard:focal-names-output-columns

Conversation

@jmgirard

Copy link
Copy Markdown
Contributor

This follows #677 and the answer in easystats/parameters#1274. With the marginaleffects backend, a by variable can have the name of an output column of the same call. Such a variable now gets the suffix .1, and the output column keeps its name. Before, the variable kept its name and the output column was renamed. For example, with lm and a factor by variable:

  • estimate_means() kept a variable named Mean and renamed the means to Mean.1. A variable named t renamed the t column to t.1.
  • estimate_contrasts(by = "p") renamed the p-values to p.1.
  • estimate_slopes(by =) did the same for Slope, SE, CI_low, CI_high, t and p.

Now the variable is Mean.1, t.1 or p.1. The focal_terms, by and at attributes and the columns of the datagrid attribute use the new name. print() and plot() find the column, and the plot keeps the variable label. by filters such as by = "p='4'" work, also with estimate = "average". coef_name stays on the estimate column.

If a focal name is already a column, the restore step in .standardize_marginaleffects_columns() appends .1. It saves the old and new names in a renamed_focal attribute. .rename_focal_attributes() uses that attribute each time the attributes are copied back from the unformatted object.

Not fixed here:

  • SE, CI_low, CI_high and Predicted with estimate_means() and estimate_contrasts(). marginaleffects::predictions() renames these columns to std.error, conf.low, conf.high and estimate before modelbased sees them. The output then holds the variable's values in place of the standard errors or intervals, with no warning (also on main).
  • Bayesian models, inequality comparisons, and estimate_contrasts() on the output of estimate_relation().
  • plot(show_data = TRUE) and residual plots for a suffixed variable, because the model data has no Mean.1 column. These also failed on main, with another error.
  • Data that already has a column <name>.1 next to <name>.

Tests: test-focal-name-clashes.R gets new loops. Each one compares a call with the same call for a variable named safe_var, with safe_var replaced by <name>.1. They cover:

  • estimate_means() with one and two focal terms (Mean, t),
  • estimate_slopes(by =) (Slope, SE, CI_low, CI_high, t, p),
  • estimate_contrasts(by =) (Difference, t, p),
  • filters on by, with and without estimate = "average",
  • print(), plot() and the plot label.

The comparison now also checks the column names of the datagrid attribute.

devtools::test() and devtools::check() locally: 0 failures, 0 errors, 0 warnings.

The version is bumped to 0.17.0.12.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

R/format.R can remove the focal variable from datagrid, causing a mismatch with the returned data.

0 open findings

What changed in this PR

Updates marginaleffects handling so conflicting focal/by variables receive a .1 suffix while output columns retain their names.

Changes:

  • Propagates renamed focal variables through attributes, filters, printing, plotting, and labels.
  • Adds regression tests for collisions and datagrid metadata.
  • Documents the change and bumps the development version.
  • R/format.R still needs adjustment to preserve the focal variable in datagrid.
File Summary
tests/​testthat/​test-focal-name-clashes.R Adds collision, filtering, plotting, and datagrid tests.
R/​visualisation_recipe_internal.R Resolves original model-data names for labels.
R/​get_marginalmeans.R Preserves renamed focal metadata.
R/​get_marginalcontrasts.R Supports filtering renamed focal variables.
R/​format.R Implements focal-column suffixing and attribute restoration.
R/​estimate_slopes.R Applies renamed focal attributes to slopes.
R/​estimate_means.R Applies renamed focal attributes to means.
R/​estimate_contrasts.R Applies renamed focal attributes to contrasts.
NEWS.md Documents the behavior change and limitations.
DESCRIPTION Bumps the development version.

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@strengejacke
strengejacke merged commit 5395985 into easystats:main Oct 14, 2026
8 of 18 checks passed
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.

3 participants