| Line 1... |
Line 1... |
| 1 |
\name{plot.lm}
|
1 |
\name{plot.lm}
|
| 2 |
\alias{plot.lm}
|
2 |
\alias{plot.lm}
|
| 3 |
\alias{plot.mlm}%which is .NotYetImplemented()
|
3 |
\alias{plot.mlm}%which is .NotYetImplemented()
|
| 4 |
\title{Plot Diagnostics for an lm Object}
|
4 |
\title{Plot Diagnostics for an lm Object}
|
| 5 |
\usage{
|
5 |
\usage{
|
| 6 |
\method{plot}{lm}(x, which = 1:4,
|
6 |
\method{plot}{lm}(x, which = c(1:3,5), %% was which = 1:4
|
| 7 |
caption = c("Residuals vs Fitted", "Normal Q-Q plot",
|
7 |
caption = c("Residuals vs Fitted", "Normal Q-Q",
|
| 8 |
"Scale-Location plot", "Cook's distance plot"),
|
8 |
"Scale-Location", "Cook's distance",
|
| 9 |
panel = points,
|
9 |
"Residuals vs Leverage", "Cook's distance vs Leverage"),
|
| 10 |
sub.caption = deparse(x$call), main = "",
|
10 |
panel = points, sub.caption = NULL, main = "",
|
| 11 |
ask = prod(par("mfcol")) < length(which) && dev.interactive(),
|
11 |
ask = prod(par("mfcol")) < length(which) && dev.interactive(),
|
| 12 |
\dots,
|
12 |
\dots,
|
| 13 |
id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75)
|
13 |
id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75,
|
| - |
|
14 |
cook.levels = c(0.5, 1), label.pos = c(4, 2))
|
| 14 |
}
|
15 |
}
|
| 15 |
\arguments{
|
16 |
\arguments{
|
| 16 |
\item{x}{\code{lm} object, typically result of \code{\link{lm}} or
|
17 |
\item{x}{\code{lm} object, typically result of \code{\link{lm}} or
|
| 17 |
\code{\link{glm}}.}
|
18 |
\code{\link{glm}}.}
|
| 18 |
\item{which}{If a subset of the plots is required, specify a subset of
|
19 |
\item{which}{if a subset of the plots is required, specify a subset of
|
| 19 |
the numbers \code{1:4}.}
|
20 |
the numbers \code{1:6}.}
|
| 20 |
\item{caption}{Captions to appear above the plots}
|
21 |
\item{caption}{captions to appear above the plots}
|
| 21 |
\item{panel}{Panel function. A useful alternative to
|
22 |
\item{panel}{panel function. A useful alternative to
|
| 22 |
\code{\link{points}} is \code{\link{panel.smooth}}.}
|
23 |
\code{\link{points}} is \code{\link{panel.smooth}}.}
|
| 23 |
\item{sub.caption}{common title---above figures if there are multiple;
|
24 |
\item{sub.caption}{common title---above figures if there are multiple;
|
| 24 |
used as \code{sub} (s.\code{\link{title}}) otherwise.}
|
25 |
used as \code{sub} (s.\code{\link{title}}) otherwise. If \code{NULL},
|
| - |
|
26 |
as by default, a possible shortened version of
|
| - |
|
27 |
\code{deparse(x$call)} is used.}
|
| 25 |
\item{main}{title to each plot---in addition to the above
|
28 |
\item{main}{title to each plot---in addition to the above
|
| 26 |
\code{caption}.}
|
29 |
\code{caption}.}
|
| 27 |
\item{ask}{logical; if \code{TRUE}, the user is \emph{ask}ed before
|
30 |
\item{ask}{logical; if \code{TRUE}, the user is \emph{ask}ed before
|
| 28 |
each plot, see \code{\link{par}(ask=.)}.}
|
31 |
each plot, see \code{\link{par}(ask=.)}.}
|
| 29 |
\item{\dots}{other parameters to be passed through to plotting
|
32 |
\item{\dots}{other parameters to be passed through to plotting
|
| Line 31... |
Line 34... |
| 31 |
\item{id.n}{number of points to be labelled in each plot, starting
|
34 |
\item{id.n}{number of points to be labelled in each plot, starting
|
| 32 |
with the most extreme.}
|
35 |
with the most extreme.}
|
| 33 |
\item{labels.id}{vector of labels, from which the labels for extreme
|
36 |
\item{labels.id}{vector of labels, from which the labels for extreme
|
| 34 |
points will be chosen. \code{NULL} uses observation numbers.}
|
37 |
points will be chosen. \code{NULL} uses observation numbers.}
|
| 35 |
\item{cex.id}{magnification of point labels.}
|
38 |
\item{cex.id}{magnification of point labels.}
|
| - |
|
39 |
\item{cook.levels}{levels of Cook's distance at which to draw contours.}
|
| - |
|
40 |
\item{label.pos}{positioning of labels, for the left half and right
|
| - |
|
41 |
half of the graph respectively, for plots 1-3.}
|
| 36 |
}
|
42 |
}
|
| 37 |
\description{
|
43 |
\description{
|
| 38 |
Four plots (selectable by \code{which}) are currently provided: a plot
|
44 |
Six plots (selectable by \code{which}) are currently available: a plot
|
| 39 |
of residuals against fitted values, a Scale-Location plot of
|
45 |
of residuals against fitted values, a Scale-Location plot of
|
| 40 |
\eqn{\sqrt{| residuals |}} against fitted values, a Normal Q-Q plot,
|
46 |
\eqn{\sqrt{| residuals |}} against fitted values, a Normal Q-Q plot, a
|
| 41 |
and a plot of Cook's distances versus row labels.
|
47 |
plot of Cook's distances versus row labels, a plot of residuals
|
| - |
|
48 |
against leverages, and a plot of Cook's distances against
|
| - |
|
49 |
leverage/(1-leverage). By default, the first
|
| - |
|
50 |
% four of these are provided.
|
| - |
|
51 |
three and \code{5} are provided.
|
| 42 |
}
|
52 |
}
|
| 43 |
\details{
|
53 |
\details{
|
| 44 |
\code{sub.caption}---by default the function call---is shown as
|
54 |
\code{sub.caption}---by default the function call---is shown as
|
| 45 |
a subtitle (under the x-axis title) on each plot when plots are on
|
55 |
a subtitle (under the x-axis title) on each plot when plots are on
|
| 46 |
separate pages, or as a subtitle in the outer margin (if any) when
|
56 |
separate pages, or as a subtitle in the outer margin (if any) when
|
| Line 49... |
Line 59... |
| 49 |
The \dQuote{Scale-Location} plot, also called \dQuote{Spread-Location} or
|
59 |
The \dQuote{Scale-Location} plot, also called \dQuote{Spread-Location} or
|
| 50 |
\dQuote{S-L} plot, takes the square root of the absolute residuals in
|
60 |
\dQuote{S-L} plot, takes the square root of the absolute residuals in
|
| 51 |
order to diminish skewness (\eqn{\sqrt{| E |}} is much less skewed
|
61 |
order to diminish skewness (\eqn{\sqrt{| E |}} is much less skewed
|
| 52 |
than \eqn{| E |} for Gaussian zero-mean \eqn{E}).
|
62 |
than \eqn{| E |} for Gaussian zero-mean \eqn{E}).
|
| 53 |
|
63 |
|
| 54 |
This \sQuote{S-L} and the Q-Q plot use \emph{standardized} residuals
|
64 |
The \sQuote{S-L}, the Q-Q, and the Residual-Leverage plot, use
|
| 55 |
which have identical variance (under the hypothesis). They are given
|
65 |
\emph{standardized} residuals which have identical variance (under the
|
| - |
|
66 |
hypothesis). They are given as
|
| 56 |
as \eqn{R_i / (s \times \sqrt{1 - h_{ii}})}{R[i] / (s*sqrt(1 - h.ii))}
|
67 |
\eqn{R_i / (s \times \sqrt{1 - h_{ii}})}{R[i] / (s*sqrt(1 - h.ii))}
|
| 57 |
where \eqn{h_{ii}}{h.ii} are the diagonal entries of the hat matrix,
|
68 |
where \eqn{h_{ii}}{h.ii} are the diagonal entries of the hat matrix,
|
| 58 |
% bug in Rdconv: "$" and \link inside \code fails; '\$' doesn't help :
|
69 |
% bug in Rdconv: "$" and \link inside \code fails; '\$' doesn't help :
|
| 59 |
\code{\link{influence}()}\code{$hat}, see also \code{\link{hat}}.
|
70 |
\code{\link{influence}()}\code{$hat}, see also \code{\link{hat}}.
|
| - |
|
71 |
|
| - |
|
72 |
The Residual-Leverage plot shows contours of equal Cook's distance,
|
| - |
|
73 |
by default for values of 0.5 and 1. In the Cook's distance vs
|
| - |
|
74 |
leverage/(1-leverage) plot, contours of standardized residuals that
|
| - |
|
75 |
are equal in magnitude are lines through the origin. The contour
|
| - |
|
76 |
lines are labeled with the magnitudes.
|
| 60 |
}
|
77 |
}
|
| 61 |
\references{
|
78 |
\references{
|
| 62 |
Belsley, D. A., Kuh, E. and Welsch, R. E. (1980)
|
79 |
Belsley, D. A., Kuh, E. and Welsch, R. E. (1980)
|
| 63 |
\emph{Regression Diagnostics.} New York: Wiley.
|
80 |
\emph{Regression Diagnostics.} New York: Wiley.
|
| 64 |
|
81 |
|
| 65 |
Cook, R. D. and Weisberg, S. (1982)
|
82 |
Cook, R. D. and Weisberg, S. (1982)
|
| 66 |
\emph{Residuals and Influence in Regression.}
|
83 |
\emph{Residuals and Influence in Regression.}
|
| 67 |
London: Chapman and Hall.
|
84 |
London: Chapman and Hall.
|
| 68 |
|
85 |
|
| - |
|
86 |
Firth, D. (1991) Generalized Linear Models. In Hinkley, D. V. and
|
| - |
|
87 |
Reid, N. and Snell, E. J., eds: Pp. 55-82 in Statistical Theory and
|
| - |
|
88 |
Modelling. In Honour of Sir David Cox, FRS. London: Chapman and Hall.
|
| - |
|
89 |
|
| 69 |
Hinkley, D. V. (1975) On power transformations to
|
90 |
Hinkley, D. V. (1975) On power transformations to
|
| 70 |
symmetry. \emph{Biometrika} \bold{62}, 101--111.
|
91 |
symmetry. \emph{Biometrika} \bold{62}, 101--111.
|
| 71 |
|
92 |
|
| 72 |
McCullagh, P. and Nelder, J. A. (1989)
|
93 |
McCullagh, P. and Nelder, J. A. (1989)
|
| 73 |
\emph{Generalized Linear Models.}
|
94 |
\emph{Generalized Linear Models.}
|
| Line 75... |
Line 96... |
| 75 |
}
|
96 |
}
|
| 76 |
\author{
|
97 |
\author{
|
| 77 |
John Maindonald and Martin Maechler.
|
98 |
John Maindonald and Martin Maechler.
|
| 78 |
}
|
99 |
}
|
| 79 |
\seealso{\code{\link{termplot}}, \code{\link{lm.influence}},
|
100 |
\seealso{\code{\link{termplot}}, \code{\link{lm.influence}},
|
| 80 |
\code{\link{cooks.distance}}.
|
101 |
\code{\link{cooks.distance}}, \code{\link{hatvalues}}.
|
| 81 |
}
|
102 |
}
|
| 82 |
\examples{
|
103 |
\examples{
|
| 83 |
## Analysis of the life-cycle savings data
|
104 |
## Analysis of the life-cycle savings data
|
| 84 |
## given in Belsley, Kuh and Welsch.
|
105 |
## given in Belsley, Kuh and Welsch.
|
| 85 |
plot(lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings))
|
106 |
plot(lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings))
|
| Line 88... |
Line 109... |
| 88 |
par(mfrow = c(2, 2), oma = c(0, 0, 2, 0))
|
109 |
par(mfrow = c(2, 2), oma = c(0, 0, 2, 0))
|
| 89 |
plot(lm.SR)
|
110 |
plot(lm.SR)
|
| 90 |
plot(lm.SR, id.n = NULL) # no id's
|
111 |
plot(lm.SR, id.n = NULL) # no id's
|
| 91 |
plot(lm.SR, id.n = 5, labels.id = NULL)# 5 id numbers
|
112 |
plot(lm.SR, id.n = 5, labels.id = NULL)# 5 id numbers
|
| 92 |
|
113 |
|
| - |
|
114 |
% ## Replace Cook's distance plot by Residual-Leverage plot
|
| - |
|
115 |
% plot(lm.SR, which=c(1:3, 5))
|
| - |
|
116 |
## Was default in R <= 2.1.x: Cook's distances instead of Residual-Leverage plot
|
| - |
|
117 |
plot(lm.SR, which = 1:4)
|
| - |
|
118 |
|
| 93 |
## Fit a smooth curve, where applicable:
|
119 |
## Fit a smooth curve, where applicable:
|
| 94 |
plot(lm.SR, panel = panel.smooth)
|
120 |
plot(lm.SR, panel = panel.smooth)
|
| 95 |
## Gives a smoother curve
|
121 |
## Gives a smoother curve
|
| 96 |
plot(lm.SR, panel = function(x,y) panel.smooth(x, y, span = 1))
|
122 |
plot(lm.SR, panel = function(x,y) panel.smooth(x, y, span = 1))
|
| 97 |
|
123 |
|
| 98 |
par(mfrow=c(2,1))# same oma as above
|
124 |
par(mfrow=c(2,1))# same oma as above
|
| 99 |
plot(lm.SR, which = 1:2, sub.caption = "Saving Rates, n=50, p=5")
|
125 |
plot(lm.SR, which = 1:2, sub.caption = "Saving Rates, n=50, p=5")
|
| - |
|
126 |
|
| - |
|
127 |
\dontshow{
|
| - |
|
128 |
## An example with *long* formula that needs abbreviation:
|
| - |
|
129 |
for(i in 1:5) assign(paste("long.var.name",i,sep="."), runif(10))
|
| - |
|
130 |
plot(lm(long.var.name.1 ~
|
| - |
|
131 |
long.var.name.2 + long.var.name.3 + long.var.name.4 + long.var.name.5))
|
| - |
|
132 |
}
|
| 100 |
}
|
133 |
}
|
| 101 |
\keyword{hplot}
|
134 |
\keyword{hplot}
|
| 102 |
\keyword{regression}
|
135 |
\keyword{regression}
|