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..cbe0c6d0f 100644 --- a/R/methods_marginaleffects.R +++ b/R/methods_marginaleffects.R @@ -25,6 +25,18 @@ model_parameters.marginaleffects <- function(model, } else { # handle non-Bayesian models tidy_model <- marginaleffects::tidy(model, conf_level = ci, ...) + + # 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] } 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" + ) + ) })