R/plot_DFFITS.R

Defines functions plot_DFFITS

Documented in plot_DFFITS

#'plot_DFBETAS
#'DFBETAS to come
#' @export
plot_DFFITS <- function(model){

  # How many observations (n) and predictors (p)
  p <- length(model$coefficients)
  n <- model$df.residual + p

  # Here, 100 is considered the cutoff for a "large" dataset
  cutoff <- ifelse(n > 100, 1, 2 * sqrt(p / n))

  # DFFITS
  DFFITS <- dffits(model)

  df <- data.frame(index = 1:length(DFFITS), DFFITS = DFFITS)

  # Which points are high leverage?
  DFFITS.df <- df %>%
    filter(DFFITS > cutoff)

  # Create plot
  p <-  df %>%
    ggplot(aes(x = index, y = DFFITS)) +
    geom_point() +
    ggtitle("DFFITS above or below cutoff be problematic") +
    geom_hline(yintercept = cutoff, color = "red") +
    geom_hline(yintercept = -cutoff, color = "red")

  output <- list()

  output$plot <- p
  output$df <- DFFITS.df

  return(output)
}
cbreenmachine/wiscR documentation built on Jan. 26, 2022, 9:10 a.m.