Skip to contents

Generates a 4-panel diagnostic plot for a multiple regression model, including: 1) Fitted values vs. observed values (check for non-linearity), 2) Quantile–Quantile plot of residuals (check for non-normality), 3) Residuals vs. fitted values (check for heteroskedasticity), 4) Autocorrelation or influence plot depending on whether data are time series.

Usage

lm_plot.4way(
  mdl,
  opt = list(ts = FALSE),
  parm = list(),
  plt_nms = c("fit", "var", "qq", ifelse(opt$ts, "ac", "infl"))
)

Arguments

mdl

A fitted model object (typically from lm).

opt

A named list of options. Recognized elements include:

ts

Logical; TRUE if data are time series, FALSE otherwise.

pred_intvl_pts

Integer; number of prediction points (default 100).

pval.SW, pval.BP, pval.DW

Logical; include p-values from Shapiro–Wilk, Breusch–Pagan, and Durbin–Watson tests in the plots.

cook.loess

Logical; whether to overlay Cook's distance loess curve.

parm

List of plot formatting parameters (see lm_plot.parms).

plt_nms

Character vector of which panels to plot. Defaults to fit, var, qq, and ac/infl depending on opt$ts.

Value

A ggplot object representing the 4-way diagnostic panel. Invisibly returns a list containing:

  • p_4way – the combined plot,

  • other elements passed through from the individual plot functions.

Details

This function is a high-level wrapper that calls internal plotting functions (lm_plot.fit, lm_plot.var, lm_plot.qq, and either lm_plot.ac or lm_plot.infl) and assembles their outputs into a combined plot_grid.

Examples

if (FALSE) { # \dontrun{
fit <- lm(mpg ~ wt + hp, data = mtcars)
lm_plot.4way(fit, opt = list(ts = FALSE, pval.DW = TRUE))
} # }