Skip to content

Keep output column names when a marginaleffects by variable shares them - #1279

Merged
strengejacke merged 6 commits into
mainfrom
marginaleffects-clash-suffix
Oct 15, 2026
Merged

strengejacke merged 6 commits into
mainfrom
marginaleffects-clash-suffix

Conversation

@jmgirard

Copy link
Copy Markdown
Contributor

Follow-up to #1274, as agreed there: the output column keeps its name, and the variable gets the suffix.

For marginaleffects objects, a by variable with the name of an output column used to keep that name. The output column then became p.1, SE.1 and so on, or it was dropped in predictions(). For example, avg_slopes(m, variables = "Petal.Length", by = "p") returned the variable as p and the p-value as p.1, and predictions(m, by = "p") lost the p-value.

Now the by columns get temporary names before the renaming steps. Their names are restored at the end, after the CI column is added. If an output column or another by variable has the name, the variable gets .1, or .2 if .1 is taken. Both model_parameters.marginaleffects() (slopes, comparisons, hypotheses) and model_parameters.predictions() do this.

d <- iris
d$p <- factor(rep(c("a", "b", "c"), 50))
m <- lm(Sepal.Width ~ p * Petal.Length, data = d)
parameters::model_parameters(marginaleffects::avg_slopes(m, variables = "Petal.Length", by = "p"))
# now: `p` holds the p-values, `p.1` holds a, b, c

Not covered: Bayesian marginaleffects objects, and newdata columns that are not by variables. Also not covered are the predictions() columns Predicted, SE, CI_low and CI_high, which marginaleffects overwrites itself.

modelbased: released modelbased 0.17.0 reorders columns by the focal-term names. With this change, a focal term named p shows up as NA there, where main put the p-values under p.1. Both are wrong. easystats/modelbased#677 renames focal columns before it calls model_parameters(), so it needs the matching change on its side.

The tests loop over every output column name of slopes, comparisons and predictions. They compare the result with the output for a neutral variable name, and they check the printed table for by = "p". The other local test failures (test-mipo.R, test-printing-stan.R, test-standardize_parameters.R) and the devtools::check() errors also occur on main.

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.

🟡 Changes recommended

Duplicate raw names can select the wrong column, and the hypothesis-specific collision path lacks a regression test.

2 open findings
What changed in this PR

This PR changes marginaleffects output handling so a by variable takes a suffix when its name conflicts with an output column, leaving the output column’s name intact.

Changes:

  • Temporarily rename by columns during processing, then restore them with unique names.
  • Add collision and printing tests for slopes, comparisons, and predictions.
  • Document the behavior and bump the development version.
File Description
tests/​testthat/​test-marginaleffects.R Adds name-collision and printed-output tests.
R/​methods_marginaleffects.R Protects and restores by column names.
NEWS.md Documents the behavior and its exclusions.
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.

Comment thread R/methods_marginaleffects.R
Comment thread R/methods_marginaleffects.R
@strengejacke
strengejacke merged commit c4438bb into main Oct 15, 2026
9 of 21 checks passed
@strengejacke
strengejacke deleted the marginaleffects-clash-suffix branch October 15, 2026 14:50
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