From cc80601b7076caeb3321db55e73c1a33066d13b6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 25 Aug 2025 20:12:43 +0200 Subject: [PATCH 1/2] Deal with / fix `tidy()` method in the _marginaleffects_ methods for `model_parameters()` Fixes #1156 --- DESCRIPTION | 2 +- R/methods_marginaleffects.R | 11 +++++++++++ tests/testthat/test-marginaleffects.R | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index c9164f443..a45f84306 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: parameters Title: Processing of Model Parameters -Version: 0.28.0.7 +Version: 0.28.0.9 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/R/methods_marginaleffects.R b/R/methods_marginaleffects.R index 60b66e545..1a752f20a 100644 --- a/R/methods_marginaleffects.R +++ b/R/methods_marginaleffects.R @@ -25,6 +25,17 @@ model_parameters.marginaleffects <- function(model, } else { # handle non-Bayesian models tidy_model <- marginaleffects::tidy(model, conf_level = ci, ...) + # remove redundant columns + to_remove <- setdiff( + # all columns + union( + colnames(marginaleffects::components(model, "newdata")), + colnames(marginaleffects::components(model, "modeldata")) + ), + # columns we want to keep + marginaleffects::components(model, "variable_names_by") + ) + tidy_model <- tidy_model[, !colnames(tidy_model) %in% to_remove, drop = FALSE] } out <- .rename_reserved_marginaleffects(tidy_model) diff --git a/tests/testthat/test-marginaleffects.R b/tests/testthat/test-marginaleffects.R index 750204bea..6076c0865 100644 --- a/tests/testthat/test-marginaleffects.R +++ b/tests/testthat/test-marginaleffects.R @@ -41,6 +41,31 @@ test_that("marginaleffects()", { variables = "Petal.Length" ) expect_identical(nrow(parameters(model)), 1L) + + # remove redundant columns + skip_if_not_installed("mgcv") + data(iris) + model <- mgcv::gam(Sepal.Width ~ s(Petal.Length, by = Species), data = iris) + mfx <- marginaleffects::avg_slopes(model, variables = "Petal.Length") + out <- model_parameters(mfx) + expect_identical(dim(out), c(1L, 11L)) + expect_named( + out, + c( + "Parameter", "Comparison", "Coefficient", "SE", "Statistic", + "p", "S", "CI", "CI_low", "CI_high", "Predicted" + ) + ) + mfx <- marginaleffects::avg_slopes(model, variables = "Petal.Length", by = "Species") + out <- model_parameters(mfx) + expect_identical(dim(out), c(3L, 11L)) + expect_named( + out, + c( + "Parameter", "Comparison", "Species", "Coefficient", "SE", "Statistic", + "p", "S", "CI", "CI_low", "CI_high" + ) + ) }) From 0198ed6a8f9b8dbb2a98fffd28567568da227ce4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 25 Aug 2025 21:11:14 +0200 Subject: [PATCH 2/2] comment --- R/methods_marginaleffects.R | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/R/methods_marginaleffects.R b/R/methods_marginaleffects.R index 1a752f20a..cbe0c6d0f 100644 --- a/R/methods_marginaleffects.R +++ b/R/methods_marginaleffects.R @@ -25,16 +25,17 @@ model_parameters.marginaleffects <- function(model, } else { # handle non-Bayesian models tidy_model <- marginaleffects::tidy(model, conf_level = ci, ...) - # remove redundant columns - to_remove <- setdiff( - # all columns - union( - colnames(marginaleffects::components(model, "newdata")), - colnames(marginaleffects::components(model, "modeldata")) - ), - # columns we want to keep - marginaleffects::components(model, "variable_names_by") + + # all columns in data grid and model data, we only want to keep "by" variables + all_data_cols <- union( + colnames(marginaleffects::components(model, "newdata")), + colnames(marginaleffects::components(model, "modeldata")) ) + # columns we want to keep + by_cols <- marginaleffects::components(model, "variable_names_by") + + # remove redundant columns + to_remove <- setdiff(all_data_cols, by_cols) tidy_model <- tidy_model[, !colnames(tidy_model) %in% to_remove, drop = FALSE] }