Repository navigation
Keep by variables whose names insight renames (marginaleffects backend) - #677
Merged
strengejacke merged 8 commits intoOct 13, 2026
Merged
Conversation
model_parameters() renames columns such as type, n or value, and the format step removes columns such as p, CI or S by name. A by variable with one of these names was dropped, renamed, or mislabeled other columns. Focal columns that hold the data grid values now get temporary names until the renaming is done.
The "recipes that do not change" test in test-visualisation_recipe.R had no closing "})", so the file did not parse and devtools::test() stopped there.
Another test file assigns a global d, which the formula environment could find before the local data.
The test asked every median from a random 200-draw subsample to differ from the full-draw median by more than 0.001, which failed in 6 of 10 local runs. It now tests that the medians differ and that 200 draws are kept.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Focal names can still collide with temporary or generated columns, and custom grids can bypass the protection.
3 open findings
What changed in this PR
This PR protects focal variable names while the marginaleffects backend formats means, slopes, and contrasts.
Changes:
- Temporarily renames focal columns during formatting and adds name-clash tests.
- Repairs two existing tests, documents the fix, and bumps the development version.
| File | Description |
|---|---|
| tests/testthat/test-visualisation_recipe.R | Restores a missing test closure. |
| tests/testthat/test-focal-name-clashes.R | Adds focal-name regression coverage. |
| tests/testthat/test-brms.R | Replaces a flaky median assertion. |
| R/format.R | Protects focal columns during standardization. |
| NEWS.md | Documents the behavior change and limits. |
| 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.
With a custom newdata, the datagrid attribute holds the generated grid, not the one passed to marginaleffects, so focal columns were not protected. Temporary column names are now also unique against the existing columns.
Member
|
Thanks, looks good to me! |
This was referenced Oct 13, 2026
Informative errors for variable names that marginaleffects overwrites (marginaleffects backend)
#679
Merged
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.


With the marginaleffects backend, some
byvariable names caused trouble. If insight or modelbased also uses the name for a column, the variable was dropped or renamed, or it mislabeled other columns..standardize_marginaleffects_columns()callsparameters::model_parameters(), which runsinsight::standardize_names(), and then removes some columns by name. For example, withlmand a factorbyvariable:type,p,CI,S,predicted,conf.level,df_residualanddf_errorwere dropped,nbecamen_Obs,valuebecameLevel,responsebecameResponse, and so on,Medianwas labeledMean, and the means were labeledCoefficient,F,Chi2,chisq,ChisqandWreplaced the t column with the factor levels.This PR gives these focal columns temporary names before
model_parameters()and restores the original names after the renaming and removal steps. A focal column is protected only if all its values occur in the data grid. Columns that marginaleffects writes itself (for examplehypothesisin contrasts, ors.value) are handled as before. Bayesian models are not changed, becausedescribe_posterior()drops columns it does not know.Not fixed here:
byvariables named like an output column of the same call (Meanin means,pin slopes,Differenceandpin contrasts). Following the answer in Keep marginaleffects focal terms named like reserved columns parameters#1274, I plan a separate PR that keeps our column name and renames the variable (for examplep.1).s.value,predictedandhypothesis, which marginaleffects overwrites with its own values.Tests: a new file,
test-focal-name-clashes.R, compares each call with the same call where the variable is namedsafe_var. It coversestimate_means()with one and two focal terms,estimate_slopes(by =),estimate_contrasts(by =)andestimate_contrasts(). It uses 99 candidate names. The test also checks that every name which worked on main still works. The output for the neutral name is identical to main.Two other test fixes:
test-visualisation_recipe.Rlost the closing})of one test in Color plots of emmeans ordinal output by response category or threshold #676, so the file did not parse on main. This adds it back.test-brms.R:26required every median from a random 200-draw subsample to differ from the full-draw median by more than 0.001, which failed in 6 of 10 local runs. It now checks that the medians differ and thatiterations = 200keeps 200 draws.devtools::test()anddevtools::check()locally: 0 failures, 0 errors, 0 warnings.The version is bumped to 0.17.0.11.