R/outlier_stats.R

Defines functions outlier_stats

Documented in outlier_stats

#' Outlier statistics
#'
#' \code{outlier_stats} returns the ordered
#' studentized residuals (decreasing based on magnitude) of
#' \code{model} to
#' Identify the most unusual observations.
#' @inheritParams residual_plot.lm
#' @inheritParams utils::head
#'
#' @return A vector of statistics.
#' @export
#'
#' @examples
#' lmod <- lm(price ~ sqft_living, data = home_sales)
#' outlier_stats(lmod, n = 3)
outlier_stats <- function(model, n = 6L) {
  if (!is.element("lm", class(model))) {
    stop("model must be an lm object")
  }
  r <- stats::rstudent(model)
  utils::head(r[order(abs(r), decreasing = TRUE)], n = n)
}

Try the api2lm package in your browser

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

api2lm documentation built on July 9, 2023, 5:52 p.m.