Skip to content
Closed
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
17 changes: 8 additions & 9 deletions R/methods_marginaleffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ model_parameters.marginaleffects <- function(model,
insight::check_if_installed("marginaleffects")

# Bayesian models have posterior draws as attribute
is_bayesian <- !is.null(suppressWarnings(marginaleffects::get_draws(model, "PxD")))
is_bayesian <- suppressWarnings(!is.null(marginaleffects::get_draws(model, "PxD")))

if (is_bayesian) {
# Bayesian
Expand Down Expand Up @@ -48,9 +48,7 @@ model_parameters.marginaleffects <- function(model,
# do not print or report these columns
out <- out[, !colnames(out) %in% c("predicted_lo", "predicted_hi"), drop = FALSE]

if (inherits(model, "marginalmeans")) {
attr(out, "coefficient_name") <- "Marginal Means"
} else if (inherits(model, "comparisons")) {
Copy link
Member

Choose a reason for hiding this comment

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

@vincentarelbundock Why did you remove this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The package has not produced objecta of this class for over a year "marginalmeans"

Copy link
Member

Choose a reason for hiding this comment

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

ah, ok!

if (inherits(model, "comparisons")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The if block for marginalmeans objects has been removed. Since model_parameters.marginalmeans is an alias for this function (defined on line 90), this change will prevent the coefficient_name attribute from being set correctly for marginalmeans objects, which appears to be a regression in functionality. This block should be restored to ensure correct behavior.

  if (inherits(model, "marginalmeans")) {
    attr(out, "coefficient_name") <- "Marginal Means"
  } else if (inherits(model, "comparisons")) {

attr(out, "coefficient_name") <- "Estimate"
attr(out, "title") <- "Contrasts between Adjusted Predictions"
if ("Type" %in% colnames(out)) {
Expand Down Expand Up @@ -137,10 +135,11 @@ model_parameters.predictions <- function(model,
out$rowid <- out$Type <- out$rowid_dedup <- NULL

# find at-variables
at_variables <- attributes(model)$newdata_at
if (is.null(at_variables)) {
at_variables <- attributes(model)$by
}
at_variables <- c(
marginaleffects::components(model, "variable_names_datagrid"),
marginaleffects::components(model, "variable_names_by"),
marginaleffects::components(model, "variable_names_by_hypothesis")
)

# find cofficient name - differs for Bayesian models
coef_name <- intersect(c("Predicted", "Coefficient"), colnames(out))[1]
Expand All @@ -153,7 +152,7 @@ model_parameters.predictions <- function(model,
}

# extract response, remove from data frame
reg_model <- attributes(model)$model
reg_model <- marginaleffects::components(model, "model")
if (!is.null(reg_model) && insight::is_model(reg_model)) {
resp <- insight::find_response(reg_model)
# check if response could be extracted
Expand Down
13 changes: 5 additions & 8 deletions tests/testthat/test-marginaleffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,10 @@ test_that("predictions, using bayestestR #1063", {
skip_if(is.null(m))

d <- insight::get_datagrid(m, by = "Days", include_random = TRUE)
x <- marginaleffects::avg_predictions(m, newdata = d, by = "Days")
x <- marginaleffects::predictions(m, newdata = d, allow_new_levels = TRUE)
out <- model_parameters(x)
Copy link
Member

Choose a reason for hiding this comment

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

@vincentarelbundock Why did you change the function call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, TBH. I think I had a problem that maybe wasn't a problem after all...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then I wasn't actually sure what the intended output was, so...

expect_named(
out,
c(
"Median", "CI", "CI_low", "CI_high", "pd", "ROPE_CI", "ROPE_low",
"ROPE_high", "ROPE_Percentage", "Days", "subgrp", "grp", "Subject"
)
)
cols <- c(
"Median", "CI", "CI_low", "CI_high", "pd", "ROPE_CI", "ROPE_low",
"ROPE_high", "ROPE_Percentage", "Days", "subgrp", "grp", "Subject")
expect_named(out, cols)
})
Loading