residual_plot.lm {api2lm}R Documentation

Plot residuals of a fitted lm object

Description

residual_plot.lm plots the residuals of a fitted lm object. In general, it is intended to provide similar functionality to plot.lm when which = 1, but can be used for different types of residuals and can also plot first-order predictor variables along the x-axis instead of only the fitted values.

Details about the different types of residuals are discussed in the get_residuals function.

Usage

## S3 method for class 'lm'
residual_plot(
  model,
  rtype = c("ordinary", "standardized", "studentized", "loo", "jackknife", "deleted",
    "internally studentized", "externally studentized"),
  xaxis = "fitted",
  id_n = 3,
  predictors = ~.,
  smooth = stats::loess,
  add_reference = TRUE,
  add_smooth = TRUE,
  ...,
  text_arglist = list(),
  abline_arglist = list(),
  smooth_arglist = list(),
  lines_arglist = list(),
  extendrange_f = 0.08
)

Arguments

model

A fitted model object from the lm function.

rtype

The residual type to plot. The default is "ordinary". The other options are "standardized", "studentized", "loo", "jackknife", "deleted", "internally studentized", "externally studentized".

xaxis

The variable to use on the x-axis of the plot(s). The default is "fitted" to use fitted values. The other option is "predictors".

id_n

The number of points to identify with labels. The default is 3.

predictors

A formula describing the first-order predictors to plot the residuals against. The default is all available first-order predictors.

smooth

A function with a formula argument to smooth the desired plot. The default function is loess.

add_reference

A logical value indicating whether a reference line should be added. The default is TRUE.

add_smooth

A logical value indicating whether a smooth should be added to each plot produced. The default is TRUE.

...

Additional arguments passed to the plot function.

text_arglist

Additional arguments passed to the text function, which is used to display the points that are identified.

abline_arglist

A named list specifying additional arguments passed to the abline function for the horizontal reference line added to the plot.

smooth_arglist

A named list specifying additional arguments passed to the function provided in the smooth argument.

lines_arglist

A named list specifying additional arguments passed to the lines function for plotting the result of applying the smooth function.

extendrange_f

Positive number(s) specifying the fraction by which the range of the residuals should be extended using the extendrange function. If longer than one, f[1] is used on the left and f[2] on the right.

Author(s)

Joshua French

See Also

plot, text, abline, lines loess.

Examples

lmod <- lm(Petal.Length ~ Sepal.Length + Species,
           data = iris)
# similarity with built-in plot.lm functionality
residual_plot(lmod)
plot(lmod, which = 1)
# residual plot for other residual types
residual_plot(lmod, rtype = "standardized", id_n = 0)
# another residual plot with several customizations
residual_plot(lmod,
              text_arglist = list(col = "blue", cex = 2),
              abline_arglist = list(lwd = 2, lty = 2,
                                    col = "brown"),
              lines_arglist = list(col = "purple"),
              )
# residual plot for predictors
residual_plot(lmod, xaxis = "pred", id_n = 2)
# residual plot for individual predictors
residual_plot(lmod, xaxis = "pred",
              predictors = ~ Sepal.Length, id_n = 2)
residual_plot(lmod, xaxis = "pred",
              predictors = ~ Species,)

[Package api2lm version 0.2 Index]