View source: R/residualPlots.R
residualPlots  R Documentation 
Draws a plot or plots of residuals versus one or more term in a mean function and/or versus fitted values. For linear models curvature tests are computed for each of the plots by adding a quadratic term to the regression function and testing the quadratic to be zero. This is Tukey's test for nonadditivity when plotting against fitted values.
### This is a generic function with only one required argument:
residualPlots (model, ...)
## Default S3 method:
residualPlots(model, terms= ~ . ,
layout=NULL, ask, main="",
fitted=TRUE, AsIs=TRUE, plot=TRUE, tests=TRUE, groups, ...)
## S3 method for class 'lm'
residualPlots(model, ...)
## S3 method for class 'glm'
residualPlots(model, ...)
### residualPlots calls residualPlot, so these arguments can be
### used with either function
residualPlot(model, ...)
## Default S3 method:
residualPlot(model, variable = "fitted", type = "pearson",
groups, plot = TRUE, linear = TRUE,
quadratic = if(missing(groups)) TRUE else FALSE,
smooth=FALSE, id=FALSE,
col = carPalette()[1], col.quad = carPalette()[2], pch=1,
xlab, ylab, lwd=1, grid=TRUE, key=!missing(groups), ...)
## S3 method for class 'lm'
residualPlot(model, ...)
## S3 method for class 'glm'
residualPlot(model, variable = "fitted", type = "pearson",
plot = TRUE, quadratic = FALSE, smooth=TRUE, ...)
model 
A regression object. 
terms 
A onesided formula that specifies a subset of the terms that appear in the formula that defined the model. The default
A grouping variable can also be specified, so, for example

layout 
If set to a value like 
ask 
If 
main 
Main title for the graphs. The default is 
fitted 
If 
AsIs 
Some terms in a model formula use the asis or identity function, for example a term like 
plot 
If 
tests 
If 
... 
Additional arguments passed to 
variable 
Quoted variable name for the factor or regressor to be put on the horizontal axis, or
the default 
type 
Specifies the type of residual to be plotted. Any of 
groups 
A grouping indicator, provided as the quoted name of a grouping variable in the model, such as "type", or "ageGroup". Points in different
groups will be plotted with different colors and symbols. If missing, no grouping is used. In 
linear 
If 
quadratic 
if 
smooth 
specifies the smoother to be used along with its arguments; if 
id 
controls point identification; if 
col 
default color for points. If groups is set, col can be a list at least as long as the number of levels for groups giving the colors for each groups. 
col.quad 
default color for quadratic fit if groups is missing. Ignored if groups are used. 
pch 
plotting character. The default is pch=1. If groups are used, pch can be set to a vector at least as long as the number of groups. 
xlab 
Xaxis label. If not specified, a useful label is constructed by the function. 
ylab 
Yaxis label. If not specified, a useful label is constructed by the function. 
lwd 
line width for the quadratic fit, if any. 
grid 
If TRUE, the default, a lightgray background grid is put on the graph 
key 
Should a key be added to the plot? Default is 
residualPlots
draws one or more residuals plots depending on the
value of the terms
and fitted
arguments. If terms = ~ .
,
the default, then a plot is produced of residuals versus each firstorder
term in the formula used to create the model. A plot of
residuals versus fitted values is also included unless fitted=FALSE
. Setting terms = ~1
will provide only the plot against fitted values.
A table of curvature tests is displayed for linear models. For plots
against a term in the model formula, say X1
, the test displayed is
the ttest for for I(X1^2)
in the fit of update, model, ~. + I(X1^2))
.
Econometricians call this a specification test. For factors, the displayed
plot is a boxplot, no curvature test is computed, and grouping is ignored.
For fitted values in a linear model, the test is Tukey's onedegreeoffreedom test for
nonadditivity. You can suppress the tests with the argument tests=FALSE
.
If grouping is used curvature tests are not displayed.
residualPlot
, which is called by residualPlots
,
should be viewed as an internal function, and is included here to display its
arguments, which can be used with residualPlots
as well. The
residualPlot
function returns the curvature test as an invisible result.
residCurvTest
computes the curvature test only. For any factors a
boxplot will be drawn. For any polynomials, plots are against the linear term.
Other nonstandard predictors like Bsplines are skipped.
For lm
objects,
returns a data.frame with one row for each plot drawn, one column for
the curvature test statistic, and a second column for the corresponding
pvalue. This function is used primarily for its side effect of drawing
residual plots.
Sanford Weisberg, sandy@umn.edu
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition. Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Chapter 8
See Also lm
, identify
,
showLabels
m1 < lm(prestige ~ income + type, data=Prestige)
residualPlots(m1)
residualPlots(m1, terms= ~ 1  type) # plot vs. yhat grouping by type
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.