R/has_intercept.R

Defines functions .check_for_intercept has_intercept

Documented in has_intercept

#' @title Checks if model has an intercept
#' @name has_intercept
#'
#' @description Checks if model has an intercept.
#'
#' @param x A model object.
#' @param verbose Toggle warnings.
#'
#' @return `TRUE` if `x` has an intercept, `FALSE` otherwise.
#'
#' @examples
#' model <- lm(mpg ~ 0 + gear, data = mtcars)
#' has_intercept(model)
#'
#' model <- lm(mpg ~ gear, data = mtcars)
#' has_intercept(model)
#'
#' @examplesIf require("lme4", quietly = TRUE)
#' model <- lmer(Reaction ~ 0 + Days + (Days | Subject), data = sleepstudy)
#' has_intercept(model)
#'
#' model <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)
#' has_intercept(model)
#' @export
has_intercept <- function(x, verbose = TRUE) {
  f <- find_formula(x)
  if (is_multivariate(x)) {
    lapply(f, .check_for_intercept, verbose = verbose)
  } else {
    .check_for_intercept(f, verbose)
  }
}

.check_for_intercept <- function(f, verbose = TRUE) {
  if (!is.null(f$conditional)) {
    f_terms <- stats::terms(f$conditional)
    intercept <- as.vector(attr(f_terms, "intercept"))
    return(intercept == 1)
  } else if (verbose) {
    format_warning("Cannot extract terms from model formula.")
  }
}

Try the insight package in your browser

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

insight documentation built on Nov. 26, 2023, 5:08 p.m.