Repository navigation
Keep output column names when a marginaleffects by variable shares them - #1279
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 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
bycolumns 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Follow-up to #1274, as agreed there: the output column keeps its name, and the variable gets the suffix.
For marginaleffects objects, a
byvariable with the name of an output column used to keep that name. The output column then becamep.1,SE.1and so on, or it was dropped inpredictions(). For example,avg_slopes(m, variables = "Petal.Length", by = "p")returned the variable aspand the p-value asp.1, andpredictions(m, by = "p")lost the p-value.Now the
bycolumns get temporary names before the renaming steps. Their names are restored at the end, after theCIcolumn is added. If an output column or anotherbyvariable has the name, the variable gets.1, or.2if.1is taken. Bothmodel_parameters.marginaleffects()(slopes, comparisons, hypotheses) andmodel_parameters.predictions()do this.Not covered: Bayesian marginaleffects objects, and
newdatacolumns that are notbyvariables. Also not covered are thepredictions()columnsPredicted,SE,CI_lowandCI_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
pshows up asNAthere, where main put the p-values underp.1. Both are wrong. easystats/modelbased#677 renames focal columns before it callsmodel_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 thedevtools::check()errors also occur on main.