Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 12 additions & 0 deletions R/methods_marginaleffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)
Copy link

Copilot AI Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The marginaleffects::components() function calls may fail if the model object doesn't have the expected components. Consider adding error handling or checking if these components exist before calling colnames() on them.

Copilot uses AI. Check for mistakes.
# 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]
Copy link

Copilot AI Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If to_remove is empty or contains non-existent column names, this subsetting could behave unexpectedly. Consider adding a check to ensure to_remove contains valid column names that exist in tidy_model.

Suggested change
tidy_model <- tidy_model[, !colnames(tidy_model) %in% to_remove, drop = FALSE]
# Only remove columns that exist in tidy_model
valid_to_remove <- intersect(to_remove, colnames(tidy_model))
if (length(setdiff(to_remove, valid_to_remove)) > 0 && length(to_remove) > 0) {
warning("Some columns specified for removal do not exist in tidy_model: ", paste(setdiff(to_remove, valid_to_remove), collapse = ", "))
}
tidy_model <- tidy_model[, !colnames(tidy_model) %in% valid_to_remove, drop = FALSE]

Copilot uses AI. Check for mistakes.
}

out <- .rename_reserved_marginaleffects(tidy_model)
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-marginaleffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)
})


Expand Down
Loading