R/sanitize_reserved.R

Defines functions sanity_reserved

sanity_reserved <- function(model = NULL, modeldata = data.frame()) {
    if (is.null(model)) {
        return(invisible(NULL))
    }

    predictors <- insight::find_variables(model, flatten = TRUE)

    # reserved keywords
    # Issue #697: we used to allow "group", as long as it wasn't in
    # `variables`, but this created problems with automatic `by=TRUE`. Perhaps
    # I could loosen this, but there are many interactions, and the lazy way is
    # just to forbid entirely.
    # NOTE: if no modeldata is available, we use `newdata`, but that often has a
    # `rowid` column. This used to break the extensions.Rmd vignette.
    reserved <- c(
        "group", "term", "contrast", "estimate",
        "std.error", "statistic", "conf.low", "conf.high", "p.value",
        "p.value.nonsup", "p.value.noninf", "by")
    bad <- unique(intersect(c(names(predictors), colnames(modeldata)), reserved))

    if (length(bad) > 0) {
        msg <- c(
            "These variable names are forbidden to avoid conflicts with the outputs of `marginaleffects`:",
            sprintf("%s", paste(sprintf('"%s"', bad), collapse = ", ")),
            "Please rename your variables before fitting the model.")
        insight::format_error(msg)
    }

    return(invisible(NULL))
}

Try the marginaleffects package in your browser

Any scripts or data that you put into this service are public.

marginaleffects documentation built on April 4, 2025, 4:36 a.m.