Plots the residuals versus each term in a mean function and versus fitted values. Also computes a curvature test for each of the plots by adding a quadratic term 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, ...)
# S3 method for default
residualPlots(model, terms = ~., layout = NULL, ask,
main = "", fitted = TRUE, AsIs=TRUE, plot = TRUE,
tests = TRUE, groups, ...)
# S3 method for lm
residualPlots(model, ...)
# S3 method for glm
residualPlots(model, ...)
### residualPlots calls residualPlot, so these arguments can be
### used with either function
residualPlot(model, ...)
# S3 method for default
residualPlot(model, variable = "fitted", type = "pearson",
groups,
plot = TRUE,
linear = TRUE,
quadratic = if(missing(groups)) TRUE else FALSE,
smoother=NULL, smoother.args=list(),
col.smooth=palette()[3],
labels,
id.method = "r",
id.n = if(id.method[1]=="identify") Inf else 0,
id.cex=1, id.col=palette()[1], id.location="lr",
col = palette()[1], col.quad = palette()[2],
pch=1,
xlab, ylab, lwd = 1, lty = 1,
grid=TRUE, key=!missing(groups), ...)
# S3 method for lm
residualPlot(model, ...)
# S3 method for glm
residualPlot(model, variable = "fitted", type = "pearson",
plot = TRUE, quadratic = FALSE,
smoother = loessLine, smoother.args=list(k=3), ...)
A regression object.
A one-sided formula that specifies a subset of the predictors. One
residual plot is drawn for each specified. The default
~ .
is to plot against all predictors. For example, the
specification terms = ~ . - X3
would plot against all predictors
except for X3
. To get a plot against fitted values only, use the
arguments terms = ~ 1
, Interactions are skipped.
For polynomial terms, the
plot is against the first-order variable (which may be centered and scaled
depending on how the poly
function is used). Plots against factors
are boxplots. Plots against other matrix terms, like splines, use the
result of predict(model), type="terms")[, variable])
as the
horizontal axis; if the predict
method doesn't permit this type,
then matrix terms are skipped.
A grouping variable can also be specified in the terms, so, for example
terms= ~ .|type
would use the factor type
to set a different
color and symbol for each level of type
. Any fits in the plots will
also be done separately for each level of group.
If set to a value like c(1, 1)
or c(4, 3)
, the layout
of the graph will have this many rows and columns. If not set, the program
will select an appropriate layout. If the number of graphs exceed nine, you
must select the layout yourself, or you will get a maximum of nine per page.
If layout=NA
, the function does not set the layout and the user can
use the par
function to control the layout, for example to have
plots from two models in the same graphics window.
If TRUE
, ask the user before drawing the next plot; if FALSE
, don't
ask.
Main title for the graphs. The default is main=""
for no title.
If TRUE
, the default, include the plot against fitted values.
If FALSE
, terms that use the “as-is” function I
are skipped; if TRUE
, the default, they are included.
If TRUE
, draw the plot(s).
If TRUE
, display the curvature tests. With glm's, the argument start
is ignored in computing the curvature tests.
Additional arguments passed to residualPlot
and then to
plot
.
Quoted variable name for the horizontal axis, or
"fitted"
to plot versus fitted values.
Type of residuals to be used. Pearson residuals are
appropriate for lm
objects since these are equivalent to ordinary residuals
with ols and correctly weighted residuals with wls. Any quoted string that
is an appropriate value of the type
argument to
residuals.lm
or "rstudent"
or "rstandard"
for
Studentized or standardized residuals.
A list of group indicators. Points in different
groups will be plotted with different colors and symbols. If missing, no grouping.
In residualPlots
, the grouping variable can also be set in the terms
argument, as described above. The default is no grouping.
If TRUE
, adds a horizontal line at zero if no groups.
With groups, display the within level of groups ols regression of the residuals
as response and the horizontal axis as the regressor.
if TRUE
, fits the quadratic regression of the
vertical axis on the horizontal axis and displays a lack of fit test. Default
is TRUE
for lm
and FALSE
for glm
or if groups
not missing.
the name of the smoother to use, selected from the choices
described at ScatterplotSmoothers
For lm
objects the
default is NULL
. For glm
object the default is loessLine
.
arguments passed to the smoother.
See ScatterplotSmoothers
. For generalized linear models the
number of elements in the spline basis is set to
k=3
; this is done to allow fitting for predictors with just a few support
points. If you have many support points you may wish to set k
to a higher
number, or k=-1
for the default used by gam
.
color for the smoother if groups missing, and ignored if groups is set.
Arguments for the labelling of
points. The default is id.n=0
for labeling no points. See
showLabels
for details of these arguments.
default color for points. If groups is set, col can abe a list at least as long as the number of levels for groups giving the colors for each groups.
default color for quadratic fit if groups is missing. Ignored if groups are used.
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.
X-axis label. If not specified, a useful label is constructed by the function.
Y-axis label. If not specified, a useful label is constructed by the function.
line width for lines.
line type for quadratic.
If TRUE, the default, a light-gray background grid is put on the graph
Should a key be added to the plot? Default is !is.null(groups)
.
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
p-value. This function is used primarily for its side effect of drawing
residual plots.
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 first-order
term in the formula used to create the model. Interaction terms, spline terms,
and polynomial terms of more than one predictor are
skipped. In addition terms that use the “as-is” function, e.g., I(X^2)
,
will also be skipped unless you set the argument AsIs=TRUE
. A plot of
residuals versus fitted values is also included unless fitted=FALSE
.
In addition to plots, a table of curvature tests is displayed. For plots
against a term in the model formula, say X1
, the test displayed is
the t-test for for I(X^2)
in the fit of update, model, ~. + I(X^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, the test is Tukey's one-degree-of-freedom 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 non-standard predictors like B-splines are skipped.
Fox, J. and Weisberg, S. (2011) An R Companion to Applied Regression, Second Edition. Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Chapter 8
See Also lm
, identify
,
showLabels
# NOT RUN {
m1 <- lm(prestige ~ income, data=Prestige)
residualPlots(m1)
residualPlots(m1, terms= ~ 1 | type) # plot vs. yhat grouping by type
# }
Run the code above in your browser using DataLab