Repository navigation
Keep by variables whose names insight renames (marginaleffects backend)
#677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
strengejacke
merged 8 commits into
easystats:main
from
jmgirard:focal-names-insight-renames
Oct 13, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8fedea7
Keep by variables whose names insight renames (marginaleffects backend)
jmgirard e142450
Close the test block that lost its closing brace in #676
jmgirard e0fa611
Build the focal-name test models from local data only
jmgirard 1c9c80e
Narrow the NEWS examples to estimate_means()
jmgirard 7f7dff0
Make the brms subsample test stable
jmgirard 994f449
Say in NEWS which cases the focal-name fix does not cover
jmgirard bc5e035
Bump version to 0.17.0.11
jmgirard 3f56a37
Check focal columns against the grid used for predictions
jmgirard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| skip_on_cran() | ||
| skip_if_not_installed("marginaleffects", minimum_version = "0.29.0") | ||
|
|
||
| # Focal variables can have names that insight, parameters or modelbased use | ||
| # for their own columns. Each test renames the focal variable and compares the | ||
| # output with the output for a neutral name (`safe_var`). | ||
|
|
||
| # names that `insight::standardize_names()` changes, or that modelbased drops | ||
| # by name | ||
| focal_reported_names <- c( | ||
| "type", "p", "CI", "S", "s.value", "predicted", "conf.level", "df_residual", | ||
| "df_error", "response", "n", "value", "component", "effect", "weight", | ||
| "weights", "sumsq", "meansq", "std.dev", "y.level", "hypothesis", "Median", | ||
| "Mean", "MAP", "Dxy", "rho", "r", "tau", "Difference", "Estimate", "F", | ||
| "Chi2", "chisq", "Chisq", "W" | ||
| ) | ||
|
|
||
| focal_data <- function(name, numeric = FALSE) { | ||
| d <- datasets::mtcars | ||
| d$am_f <- factor(d$am) | ||
| d[[name]] <- if (numeric) d$wt else factor(d$cyl) | ||
| d | ||
| } | ||
|
|
||
| # the formula environment is the calling function, so that the data `d` is | ||
| # found there and not in the global environment | ||
| focal_formula <- function(rhs, env = parent.frame()) { | ||
| stats::as.formula(paste("mpg ~", rhs), env = env) | ||
| } | ||
|
|
||
| focal_calls <- list( | ||
| means = function(name) { | ||
| d <- focal_data(name) | ||
| m <- stats::lm(focal_formula(paste0("`", name, "` + hp")), data = d) | ||
| estimate_means(m, name) | ||
| }, | ||
| means_two = function(name) { | ||
| d <- focal_data(name) | ||
| m <- stats::lm(focal_formula(paste0("`", name, "` * am_f")), data = d) | ||
| estimate_means(m, c("am_f", name)) | ||
| }, | ||
| slopes_by = function(name) { | ||
| d <- focal_data(name) | ||
| m <- stats::lm(focal_formula(paste0("`", name, "` * wt")), data = d) | ||
| estimate_slopes(m, "wt", by = name) | ||
| }, | ||
| contrasts_by = function(name) { | ||
| d <- focal_data(name) | ||
| m <- stats::lm(focal_formula(paste0("`", name, "` * am_f")), data = d) | ||
| estimate_contrasts(m, "am_f", by = name) | ||
| }, | ||
| contrasts = function(name) { | ||
| d <- focal_data(name) | ||
| m <- stats::lm(focal_formula(paste0("`", name, "` + hp")), data = d) | ||
| estimate_contrasts(m, name) | ||
| } | ||
| ) | ||
|
|
||
| # Returns NULL when `out` equals `ref` with "safe_var" replaced by `name`, | ||
| # otherwise a short description of the first difference. | ||
| focal_difference <- function(ref, out, name) { | ||
| if (inherits(out, "error")) { | ||
| return(paste("error:", conditionMessage(out))) | ||
| } | ||
| swap <- function(x) { | ||
| if (is.character(x)) gsub("safe_var", name, x, fixed = TRUE) else x | ||
| } | ||
| ref_names <- swap(colnames(ref)) | ||
| if (!identical(ref_names, colnames(out))) { | ||
| return(paste0( | ||
| "columns [", toString(colnames(out)), "], expected [", | ||
| toString(ref_names), "]" | ||
| )) | ||
| } | ||
| for (i in seq_along(ref)) { | ||
| r <- ref[[i]] | ||
| o <- out[[i]] | ||
| if (!identical(class(r), class(o))) { | ||
| return(paste0("column ", i, " has class ", class(o)[1])) | ||
| } | ||
| if (is.factor(r)) { | ||
| levels(r) <- swap(levels(r)) | ||
| } | ||
| r <- swap(r) | ||
| if (!isTRUE(all.equal(r, o, check.attributes = FALSE))) { | ||
| return(paste0("column ", i, " has other values")) | ||
| } | ||
| } | ||
| for (a in c("focal_terms", "by", "trend", "contrast")) { | ||
| if (!identical(swap(attr(ref, a, exact = TRUE)), attr(out, a, exact = TRUE))) { | ||
| return(paste0("attribute ", a, " differs")) | ||
| } | ||
| } | ||
| NULL | ||
| } | ||
|
|
||
| expect_focal_names_match <- function(call_name, names) { | ||
| call <- focal_calls[[call_name]] | ||
| ref <- call("safe_var") | ||
| for (name in names) { | ||
| out <- tryCatch(suppressWarnings(call(name)), error = function(e) e) | ||
| diff <- focal_difference(ref, out, name) | ||
| expect_null(diff, label = paste0(call_name, "(\"", name, "\"): ", diff)) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| test_that("estimate_means() keeps focal variables named like renamed columns", { | ||
| names <- setdiff(focal_reported_names, c("Mean", "s.value")) | ||
| expect_focal_names_match("means", names) | ||
| expect_focal_names_match("means_two", names) | ||
| }) | ||
|
|
||
|
|
||
| test_that("estimate_slopes() keeps by-variables named like renamed columns", { | ||
| names <- setdiff(focal_reported_names, c("p", "s.value", "predicted")) | ||
| expect_focal_names_match("slopes_by", names) | ||
| }) | ||
|
|
||
|
|
||
| test_that("estimate_contrasts() keeps by-variables named like renamed columns", { | ||
| names <- setdiff( | ||
| focal_reported_names, | ||
| c("p", "s.value", "hypothesis", "Difference") | ||
| ) | ||
| expect_focal_names_match("contrasts_by", names) | ||
| }) | ||
|
|
||
|
|
||
| test_that("focal variable names that worked before still work", { | ||
| # names that matched on upstream main b964ea75, computed once from a list | ||
| # of 99 candidate names (the reported names and other column, argument and | ||
| # output names) | ||
| matched_before <- list( | ||
| means = c( | ||
| "z", "rowidcf", "wts", "predicted_lo", "predicted_hi", "emmean", | ||
| "prob", "rate", "lower.CL", "upper.CL", "asymp.LCL", "asymp.UCL", | ||
| "Level1", "Level2", "Parameter", "Comparison", "Response", "Threshold", | ||
| "Row", "Residuals", "Group", "Level", "Slope", "Coefficient", | ||
| "Probability", "Contrast", "Component", "Effects", "Type", "Ratio", | ||
| "iter_1", "x", "model", "data", "at", "ci", "trend", "predict", | ||
| "comparison", "length", "range", "fun", "i", "out", "means", "grid" | ||
| ), | ||
| means_two = c( | ||
| "z", "rowidcf", "wts", "predicted_lo", "predicted_hi", "emmean", | ||
| "prob", "rate", "lower.CL", "upper.CL", "asymp.LCL", "asymp.UCL", | ||
| "Level1", "Level2", "Parameter", "Comparison", "Response", "Threshold", | ||
| "Row", "Residuals", "Group", "Level", "Slope", "Coefficient", | ||
| "Probability", "Contrast", "Component", "Effects", "Type", "Ratio", | ||
| "iter_1", "x", "model", "data", "at", "ci", "trend", "predict", | ||
| "comparison", "length", "range", "fun", "i", "out", "means", "grid" | ||
| ), | ||
| slopes_by = c( | ||
| "rowidcf", "wts", "emmean", "prob", "rate", "lower.CL", "upper.CL", | ||
| "asymp.LCL", "asymp.UCL", "Level1", "Level2", "Response", "Threshold", | ||
| "Row", "Residuals", "Group", "Level", "Probability", "Contrast", | ||
| "Component", "Effects", "Type", "Ratio", "iter_1", "x", "model", | ||
| "data", "at", "ci", "trend", "predict", "comparison", "length", | ||
| "range", "fun", "i", "out", "means", "grid" | ||
| ), | ||
| contrasts_by = c( | ||
| "z", "rowidcf", "wts", "predicted_lo", "predicted_hi", "emmean", | ||
| "prob", "rate", "lower.CL", "upper.CL", "asymp.LCL", "asymp.UCL", | ||
| "Level1", "Level2", "Comparison", "Response", "Threshold", "Row", | ||
| "Residuals", "Group", "Level", "Probability", "Contrast", "Component", | ||
| "Effects", "Type", "Ratio", "iter_1", "x", "model", "data", "at", "ci", | ||
| "trend", "predict", "comparison", "length", "range", "fun", "i", "out", | ||
| "means", "grid" | ||
| ), | ||
| contrasts = c( | ||
| "z", "F", "Chi2", "chisq", "Chisq", "W", "Median", "Mean", "MAP", | ||
| "Dxy", "rho", "r", "tau", "df_residual", "df_error", "std.dev", | ||
| "s.value", "component", "effect", "predicted", "response", "y.level", | ||
| "conf.level", "n", "type", "weight", "weights", "hypothesis", "value", | ||
| "sumsq", "meansq", "Estimate", "rowidcf", "wts", "predicted_lo", | ||
| "predicted_hi", "emmean", "prob", "rate", "lower.CL", "upper.CL", | ||
| "asymp.LCL", "asymp.UCL", "Predicted", "CI", "Level1", "Level2", | ||
| "Comparison", "Response", "Threshold", "Row", "Residuals", "Group", | ||
| "Level", "Slope", "Coefficient", "Probability", "Contrast", | ||
| "Component", "Effects", "Type", "Ratio", "S", "iter_1", "x", "model", | ||
| "data", "at", "ci", "trend", "predict", "comparison", "length", | ||
| "range", "fun", "i", "out", "means", "grid" | ||
| ) | ||
| ) | ||
| for (call_name in names(matched_before)) { | ||
| expect_focal_names_match(call_name, matched_before[[call_name]]) | ||
| } | ||
| }) | ||
|
|
||
|
|
||
| test_that("estimate_means() keeps numeric focal variables for glm", { | ||
| names <- setdiff(focal_reported_names, c("Mean", "s.value")) | ||
| call <- function(name) { | ||
| d <- focal_data(name, numeric = TRUE) | ||
| d$vs <- datasets::mtcars$vs | ||
| m <- stats::glm( | ||
| stats::as.formula(paste0("vs ~ am_f * `", name, "`")), | ||
| family = stats::binomial(), | ||
| data = d | ||
| ) | ||
| estimate_means(m, c("am_f", name)) | ||
| } | ||
| ref <- suppressWarnings(call("safe_var")) | ||
| for (name in names) { | ||
| out <- tryCatch(suppressWarnings(call(name)), error = function(e) e) | ||
| diff <- focal_difference(ref, out, name) | ||
| expect_null(diff, label = paste0("glm(\"", name, "\"): ", diff)) | ||
| } | ||
| }) | ||
|
|
||
|
|
||
| test_that("estimate_means() keeps focal variables with custom newdata", { | ||
| # the values in `newdata` are not part of the generated data grid | ||
| names <- setdiff(focal_reported_names, c("Mean", "s.value")) | ||
| call <- function(name) { | ||
| d <- focal_data(name, numeric = TRUE) | ||
| m <- stats::lm(focal_formula(paste0("`", name, "` + hp")), data = d) | ||
| newdata <- data.frame(x = c(2.123, 4.567), hp = 150) | ||
| colnames(newdata)[1] <- name | ||
| estimate_means(m, by = name, newdata = newdata) | ||
| } | ||
| ref <- call("safe_var") | ||
| for (name in names) { | ||
| out <- tryCatch(suppressWarnings(call(name)), error = function(e) e) | ||
| diff <- focal_difference(ref, out, name) | ||
| expect_null(diff, label = paste0("newdata(\"", name, "\"): ", diff)) | ||
| } | ||
| }) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.