Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jan 27, 2025
1 parent 400030f commit 6252113
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 12 additions & 7 deletions R/get_marginalcontrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ get_marginalcontrasts <- function(model,
contrast <- "auto"
}

# Guess arguments
# check whether contrasts should be made for numerics or categorical
model_data <- insight::get_data(model, source = "mf", verbose = FALSE)
on_the_fly_factors <- attributes(model_data)$factors

# Guess arguments
my_args <- .guess_marginaleffects_arguments(model, by, contrast, verbose = verbose, ...)

# sanitize comparison argument, to ensure compatibility between different
# marginaleffects versions - newer versions don't accept a string argument,
# only formulas (older versions don't accept formulas)
my_args <- .get_marginaleffects_hypothesis_argument(comparison, my_args, ...)

# check whether contrasts should be made for numerics or categorical
model_data <- insight::get_data(model, source = "mf", verbose = FALSE)
on_the_fly_factors <- attributes(model_data)$factors
my_args <- .get_marginaleffects_hypothesis_argument(comparison, my_args, model_data, ...)

# extract first focal term
first_focal <- my_args$contrast[1]
Expand Down Expand Up @@ -115,11 +115,16 @@ get_marginalcontrasts <- function(model,

# make "comparison" argument compatible -----------------------------------

.get_marginaleffects_hypothesis_argument <- function(comparison, my_args, ...) {
.get_marginaleffects_hypothesis_argument <- function(comparison, my_args, model_data = NULL, ...) {
# init
comparison_slopes <- NULL
original_by <- my_args$by

# make sure "by" is a valid column name, and no filter-directive, like "Species='setosa'".
if (!is.null(my_args$by) && grepl("[^0-9A-Za-z\\.]", my_args$by)) {
my_args$by <- NULL
}

# convert comparison and by into a formula
if (!is.null(comparison)) {
# if we have a formula as comparison, we convert it into strings in order to
Expand Down
4 changes: 1 addition & 3 deletions R/get_marginaltrends.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ get_marginaltrends <- function(model,
dots <- list(...)

# Guess arguments
trend <- .guess_marginaltrends_arguments(model, trend, by, verbose, ...)

trend <- .guess_marginaltrends_arguments(model, trend, verbose, ...)

# First step: create a data grid --------------------------------------------
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -93,7 +92,6 @@ get_marginaltrends <- function(model,
#' @keywords internal
.guess_marginaltrends_arguments <- function(model,
trend = NULL,
by = NULL,
verbose = TRUE,
...) {
# Gather info
Expand Down

0 comments on commit 6252113

Please sign in to comment.