Repository navigation
Informative errors for variable names that marginaleffects overwrites (marginaleffects backend) - #679
Merged
Merged
Conversation
… contrast variable named Parameter
… and reword the error
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
Targeted regression tests cover the changed behavior, and no unresolved issues were identified.
0 open findings
What changed in this PR
This PR makes the marginaleffects backend report variable-name collisions clearly instead of returning incorrect results or unclear errors. It also fixes several contrast-output name collisions.
Changes:
- Add checks for variable names that marginaleffects reserves or overwrites.
- Resolve contrast-column collisions and choose the next available suffix.
- Add regression tests, update NEWS, and bump the development version.
| File | Description |
|---|---|
| tests/testthat/test-focal-name-clashes.R | Tests errors, valid names, and collision handling. |
| R/get_marginaltrends.R | Checks names before calculating trends. |
| R/get_marginalmeans.R | Checks names before calculating means and contrasts. |
| R/format.R | Resolves contrast labels and focal-column suffixes. |
| R/checks.R | Defines the name-collision check and error. |
| NEWS.md | Documents the behavior and remaining 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.
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.
This follows #677 and #678. With the marginaleffects backend, some variable names cannot work, because marginaleffects uses them for its own columns.
marginaleffects::predictions()renames columns namedPredicted,SE,CI_lowandCI_high(reported as vincentarelbundock/marginaleffects#1771). marginaleffects also writes or usess.value,df,predicted*androwiditself. Before, these names gave wrong estimates, SEs or CIs, missing columns, or unclear errors such as "missing value where TRUE/FALSE needed".estimate_means(),estimate_contrasts()andestimate_slopes()now stop with an error that names the variable and asks users to rename it:byvariables ofestimate_means(), and ofestimate_contrasts()with a factor contrast:Predicted,SE,CI_low,CI_high,df,rowid,s.value.contrastvariable:SE,CI_low,CI_high,df,rowid.estimate_means()andestimate_contrasts():SE,CI_low,CI_high,df.byvariables ofestimate_slopes():df,rowid,s.value,predicted,predicted_hi,predicted_lo.The check is
.check_marginaleffects_names()in R/checks.R. It runs inget_marginalmeans()andget_marginaltrends()before the marginaleffects call, and it skips Bayesian models.estimate_slopes()with abyvariable namedSE,CI_loworCI_highkeeps working with the.1suffix from #678.Some clashes come from modelbased itself, and these now work:
estimate_contrasts(by = "Level1")(or"Level2") gave two columns namedLevel1. The variable is nowLevel1.1.estimate_contrasts(by = "hypothesis")did not split the contrast labels intoLevel1andLevel2, because marginaleffects names its own columnhypothesis.1.estimate_contrasts(m, "Parameter")failed, because the contrast labels were converted to the type of theParametervariable and became NA.<name>.1is already a variable, the suffix is now the next free number (for examplep.2). Before,by = c("p", "p.1")gave a columnp.1with the values ofp.Not fixed here: Bayesian models, inequality comparisons, contrasts of slopes (numeric contrast variable), and the trend and covariate roles of
estimate_slopes(). Some of these also give wrong output on main.Tests in
test-focal-name-clashes.R:safe_var,Level1/Level2,hypothesis,Parameterand the free suffix.Level1andLevel2are removed from the "worked before" list forestimate_contrasts(by =). That test passed only because the duplicate column names matched the reference.devtools::test()gives 0 failures and 0 errors locally.devtools::check()gives 0 errors and 0 warnings. The version is bumped to 0.17.0.14.