plot_model_check: plot_model_check

View source: R/plot_model_check.R

plot_model_checkR Documentation

plot_model_check

Description

Function provides multi-plots to check model assumptions of heteroscedasticity, normally distributed residuals and leverage of each datum.

Function plots fitted values versus residuals, fitted values versus response values, a QQ plot of the standardized residuals, and histogram of residual values.

For the QQ plot only standardized residuals are computed without taking the square root of the absolute value.

Usage

plot_model_check(
  fitted_v,
  response_v,
  residual_v,
  id_v = NULL,
  residual_label_threshold = NULL,
  label_color = "red",
  label_sd = NULL,
  title = NULL,
  axis_text_size = 11,
  pts_color = "black",
  pts_fill = "white",
  pts_alpha = 1,
  pts_size = 1,
  histo_fill = "white",
  histo_alpha = 1,
  ref_line_color = "red",
  trend_line = TRUE,
  trend_line_color = "red"
)

Arguments

fitted_v

A required numeric vector of fitted values.

response_v

A required numeric vector of observed response/dependent values.

residual_v

A required numeric vector of corresponding residual values.

id_v

An optional numeric/string vector that labels the fit/residual pairs. If this argument is NULL then the fit/residual pairs are numbered for identification.

residual_label_threshold

A numeric that sets the residual threshold beyond which observations will be labeled with their id.

label_color

A string that sets the label/point color for observations whose absolute residual is greater than the 'label_threshold'.

label_sd

A numeric that sets the number times +/- residual standard deviations to plot as a pair of horizontal dotted lines. Typical values could be 1 or 2 standard deviations.

title

A string that sets the plot title.

axis_text_size

A numeric that sets the font size along the axis'. Default is 11.

pts_color

A string that sets the color of the points.

pts_fill

A string that sets the fill color of the points.

pts_alpha

A numeric value that sets the alpha level of 'pts_color'.

pts_size

A numeric value that sets the size of the points.

histo_fill

A string that sets the histogram fill color.

histo_alpha

A numeric that sets the histogram fill alpha.

ref_line_color

A string that sets the reference lines color.

trend_line

A logical which if TRUE plots a polynomial based trend line across the residuals.

trend_line_color

A string that sets the color of the trend line.

Value

A multi-paneled ggplot2 object with various plots for checking linear model assumptions.

Examples

library(data.table)
library(ggplot2)
library(gtable)
library(ggplotify)
library(RplotterPkg)
library(RregressPkg)

sample_size = 500
x <- runif(n = sample_size) * 5
y <- 3 + 5 * x^2 + rnorm(n = sample_size, mean = 0, sd = 5)
sim_df <- data.frame(x,y)

fit_lm <- lm(y ~ x, data = sim_df)

sim_ols <- RregressPkg::ols_calc(
  df = sim_df,
  formula_obj = fit_lm
)

a_plot <- RregressPkg::plot_model_check(
  fitted_v = sim_ols$fitted_vals,
  response_v = sim_df$y,
  residual_v = sim_ols$residual_vals,
  histo_fill = "blue",
  histo_alpha = 0.5,
  ref_line_color = "darkorange"
)


deandevl/RregressPkg documentation built on Feb. 5, 2025, 12:11 p.m.