residual_plot {MESS} | R Documentation |
Plots a standardized residual
Description
Plots a standardized residual plot from an lm or glm object and provides additional graphics to help evaluate the variance homogeneity and mean.
Usage
residual_plot(
x,
y = NULL,
candy = TRUE,
bandwidth = 0.3,
xlab = "Fitted values",
ylab = "Std.res.",
col.sd = "blue",
alpha = 0.1,
ylim = NA,
...
)
## Default S3 method:
residual_plot(
x,
y = NULL,
candy = TRUE,
bandwidth = 0.3,
xlab = "Fitted values",
ylab = "Std.res.",
col.sd = "blue",
alpha = 0.1,
ylim = NA,
...
)
## S3 method for class 'lm'
residual_plot(
x,
y,
candy = TRUE,
bandwidth = 0.3,
xlab = "Fitted values",
ylab = "Stud.res.",
col.sd = "blue",
alpha = 0.1,
...
)
## S3 method for class 'glm'
residual_plot(
x,
y,
candy = TRUE,
bandwidth = 0.4,
xlab = "Fitted values",
ylab = "Std. dev. res.",
col.sd = "blue",
alpha = 0.1,
...
)
Arguments
x |
lm object or a numeric vector |
y |
numeric vector for the y axis values |
candy |
logical. Should a lowess curve and local standard deviation of
the residual be added to the plot. Defaults to |
bandwidth |
The width of the window used to calculate the local smoothed version of the mean and the variance. Value should be between 0 and 1 and determines the percentage of the window width used |
xlab |
x axis label |
ylab |
y axis label |
col.sd |
color for the background residual deviation |
alpha |
number between 0 and 1 determining the transprency of the standard deviation plotting color |
ylim |
pair of observations that set the minimum and maximum of the y axis. If set to NA (the default) then the limits are computed from the data. |
... |
Other arguments passed to the plot function |
Details
The y axis shows the studentized residuals (for lm objects) or standardized deviance residuals (for glm objects). The x axis shows the linear predictor, i.e., the predicted values for lm objects.
The blue area is a smoothed estimate of 1.96*SD of the standardized residuals in a window around the predicted value. The blue area should largely be rectangular if the standardized residuals have more or less the same variance.
The dashed line shows the smoothed mean of the standardized residuals and should generally follow the horizontal line through (0,0).
Solid circles correspond to standardized residuals outside the range from [-1.96; 1.96] while open circles are inside that interval. Roughly 5
Value
Produces a standardized residual plot
Author(s)
Claus Ekstrom <claus@rprimer.dk>
See Also
Examples
# Linear regression example
data(trees)
model <- lm(Volume ~ Girth + Height, data=trees)
residual_plot(model)
model2 <- lm(Volume ~ Girth + I(Girth^2) + Height, data=trees)
residual_plot(model2)
# Add extra information about points by adding geom_text to the object produced
m <- lm(mpg ~ hp + factor(vs), data=mtcars)
residual_plot(m) + ggplot2::geom_point(ggplot2::aes(color=factor(cyl)), data=mtcars)