The R Project SVN R

Rev

Rev 74363 | Rev 81099 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/stats/man/plot.lm.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
76078 maechler 3
% Copyright 1995-2019 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{plot.lm}
56186 murdoch 7
\alias{plot.lm}
63605 ripley 8
% \alias{plot.mlm} %which is .NotYetImplemented()
27442 ripley 9
\title{Plot Diagnostics for an lm Object}
10
\usage{
76078 maechler 11
\method{plot}{lm}(x, which = c(1,2,3,5), %% was which = 1:4
45710 maechler 12
     caption = list("Residuals vs Fitted", "Normal Q-Q",
56280 ripley 13
       "Scale-Location", "Cook's distance",
14
       "Residuals vs Leverage",
15
       expression("Cook's dist vs Leverage  " * h[ii] / (1 - h[ii]))),
76078 maechler 16
     panel = if(add.smooth) function(x, y, ...)
17
              panel.smooth(x, y, iter=iter.smooth, ...) else points,
35617 maechler 18
     sub.caption = NULL, main = "",
27442 ripley 19
     ask = prod(par("mfcol")) < length(which) && dev.interactive(),
20
     \dots,
34063 maechler 21
     id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75,
35617 maechler 22
     qqline = TRUE, cook.levels = c(0.5, 1.0),
76078 maechler 23
     add.smooth = getOption("add.smooth"),
24
     iter.smooth = if(isGlm && binomialLike) 0 else 3,
25
     label.pos = c(4,2),
68206 maechler 26
     cex.caption = 1, cex.oma.main = 1.25)
27442 ripley 27
}
28
\arguments{
29
  \item{x}{\code{lm} object, typically result of \code{\link{lm}} or
30
    \code{\link{glm}}.}
34063 maechler 31
  \item{which}{if a subset of the plots is required, specify a subset of
71501 maechler 32
    the numbers \code{1:6}, see \code{caption} below (and the
33
    \sQuote{Details}) for the different kinds.}
45710 maechler 34
  \item{caption}{captions to appear above the plots;
35
    \code{\link{character}} vector or \code{\link{list}} of valid
68206 maechler 36
    graphics annotations, see \code{\link{as.graphicsAnnot}}, of length
37
    6, the j-th entry corresponding to \code{which[j]}.  Can be set to
38
    \code{""} or \code{NA} to suppress all captions.
45710 maechler 39
  }
35617 maechler 40
  \item{panel}{panel function.  The useful alternative to
41
    \code{\link{points}}, \code{\link{panel.smooth}} can be chosen
42
    by \code{add.smooth = TRUE}.}
49470 ripley 43
  \item{sub.caption}{common title---above the figures if there are more
44
    than one; used as \code{sub} (s.\code{\link{title}}) otherwise.  If
45
    \code{NULL}, as by default, a possible abbreviated version of
34063 maechler 46
    \code{deparse(x$call)} is used.}
49470 ripley 47
  \item{main}{title to each plot---in addition to \code{caption}.}
27442 ripley 48
  \item{ask}{logical; if \code{TRUE}, the user is \emph{ask}ed before
49
    each plot, see \code{\link{par}(ask=.)}.}
50
  \item{\dots}{other parameters to be passed through to plotting
51
    functions.}
52
  \item{id.n}{number of points to be labelled in each plot, starting
53
    with the most extreme.}
54
  \item{labels.id}{vector of labels, from which the labels for extreme
55
    points will be chosen.  \code{NULL} uses observation numbers.}
56
  \item{cex.id}{magnification of point labels.}
35621 maechler 57
  \item{qqline}{logical indicating if a \code{\link{qqline}()} should be
68201 maechler 58
    added to the normal Q-Q plot.}
34063 maechler 59
  \item{cook.levels}{levels of Cook's distance at which to draw contours.}
35617 maechler 60
  \item{add.smooth}{logical indicating if a smoother should be added to
61
    most plots; see also \code{panel} above.}
76078 maechler 62
  \item{iter.smooth}{the number of robustness iterations, the argument
63
    \code{iter} in \code{\link{panel.smooth}()}; the default uses no such
64
    iterations for \code{\link{glm}(*, family=binomial)} fits which is
65
    particularly desirable for the (predominant) case of binary observations.}
34063 maechler 66
  \item{label.pos}{positioning of labels, for the left half and right
67
    half of the graph respectively, for plots 1-3.}
43067 ripley 68
  \item{cex.caption}{controls the size of \code{caption}.}
68206 maechler 69
  \item{cex.oma.main}{controls the size of the \code{sub.caption} only if
70
    that is \emph{above} the figures when there is more than one.}
27442 ripley 71
}
72
\description{
34063 maechler 73
  Six plots (selectable by \code{which}) are currently available: a plot
27442 ripley 74
  of residuals against fitted values, a Scale-Location plot of
50807 ripley 75
  \eqn{\sqrt{| residuals |}}{sqrt(| residuals |)}
76
  against fitted values, a Normal Q-Q plot, a
34063 maechler 77
  plot of Cook's distances versus row labels, a plot of residuals
78
  against leverages, and a plot of Cook's distances against
68206 maechler 79
  leverage/(1-leverage).  By default, the first three and \code{5} are
80
  provided.
27442 ripley 81
}
82
\details{
83
  \code{sub.caption}---by default the function call---is shown as
84
  a subtitle (under the x-axis title) on each plot when plots are on
85
  separate pages, or as a subtitle in the outer margin (if any) when
86
  there are multiple plots per page.
87
 
42961 ripley 88
  The \sQuote{Scale-Location} plot, also called \sQuote{Spread-Location} or
89
  \sQuote{S-L} plot, takes the square root of the absolute residuals in
74363 maechler 90
  order to diminish skewness (\eqn{\sqrt{| E |}}{sqrt(|E|)} is much less skewed
27442 ripley 91
  than \eqn{| E |} for Gaussian zero-mean \eqn{E}).
92
 
34063 maechler 93
  The \sQuote{S-L}, the Q-Q, and the Residual-Leverage plot, use
94
  \emph{standardized} residuals which have identical variance (under the
95
  hypothesis).  They are given as
50807 ripley 96
  \eqn{R_i / (s \times \sqrt{1 - h_{ii}})}{R[i] / (s * sqrt(1 - h.ii))}
27442 ripley 97
  where \eqn{h_{ii}}{h.ii} are the diagonal entries of the hat matrix,
50420 ripley 98
  \code{\link{influence}()$hat} (see also \code{\link{hat}}), and
99
  where the Residual-Leverage plot uses standardized Pearson residuals
45676 maechler 100
  (\code{\link{residuals.glm}(type = "pearson")}) for \eqn{R[i]}.
34063 maechler 101
 
102
  The Residual-Leverage plot shows contours of equal Cook's distance,
36637 ripley 103
  for values of \code{cook.levels} (by default 0.5 and 1) and omits
45687 maechler 104
  cases with leverage one with a warning.  If the leverages are constant
105
  (as is typically the case in a balanced \code{\link{aov}} situation)
106
  the plot uses factor level combinations instead of the leverages for
107
  the x-axis.  (The factor levels are ordered by mean fitted value.)
35617 maechler 108
 
109
  In the Cook's distance vs leverage/(1-leverage) plot, contours of
71501 maechler 110
  standardized residuals (\code{\link{rstandard}(.)}) that are equal in
111
  magnitude are lines through the origin.  The contour lines are
112
  labelled with the magnitudes.
27442 ripley 113
}
114
\references{
74265 hornik 115
  Belsley, D. A., Kuh, E. and Welsch, R. E. (1980).
116
  \emph{Regression Diagnostics}.
117
  New York: Wiley.
27442 ripley 118
 
74265 hornik 119
  Cook, R. D. and Weisberg, S. (1982).
120
  \emph{Residuals and Influence in Regression}.
27442 ripley 121
  London: Chapman and Hall.
122
 
74265 hornik 123
  Firth, D. (1991) Generalized Linear Models.
124
  In Hinkley, D. V. and Reid, N. and Snell, E. J., eds:
125
  Pp.\sspace{}55-82 in Statistical Theory and Modelling.
126
  In Honour of Sir David Cox, FRS.
127
  London: Chapman and Hall.
34063 maechler 128
 
74265 hornik 129
  Hinkley, D. V. (1975).
130
  On power transformations to symmetry.
131
  \emph{Biometrika}, \bold{62}, 101--111.
132
  \doi{10.2307/2334491}.
27442 ripley 133
 
74265 hornik 134
  McCullagh, P. and Nelder, J. A. (1989).
135
  \emph{Generalized Linear Models}.
27442 ripley 136
  London: Chapman and Hall.
137
}
138
\author{
139
  John Maindonald and Martin Maechler.
140
}
141
\seealso{\code{\link{termplot}}, \code{\link{lm.influence}},
34063 maechler 142
  \code{\link{cooks.distance}}, \code{\link{hatvalues}}.
27442 ripley 143
}
144
\examples{
41508 ripley 145
require(graphics)
146
 
27442 ripley 147
## Analysis of the life-cycle savings data
148
## given in Belsley, Kuh and Welsch.
56280 ripley 149
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
150
plot(lm.SR)
27442 ripley 151
 
44243 ripley 152
## 4 plots on 1 page;
153
## allow room for printing model formula in outer margin:
27442 ripley 154
par(mfrow = c(2, 2), oma = c(0, 0, 2, 0))
155
plot(lm.SR)
61168 ripley 156
plot(lm.SR, id.n = NULL)                 # no id's
157
plot(lm.SR, id.n = 5, labels.id = NULL)  # 5 id numbers
27442 ripley 158
 
44243 ripley 159
## Was default in R <= 2.1.x:
160
## Cook's distances instead of Residual-Leverage plot
34063 maechler 161
plot(lm.SR, which = 1:4)
162
 
27625 ripley 163
## Fit a smooth curve, where applicable:
27442 ripley 164
plot(lm.SR, panel = panel.smooth)
165
## Gives a smoother curve
61168 ripley 166
plot(lm.SR, panel = function(x, y) panel.smooth(x, y, span = 1))
27442 ripley 167
 
61168 ripley 168
par(mfrow = c(2,1))  # same oma as above
27442 ripley 169
plot(lm.SR, which = 1:2, sub.caption = "Saving Rates, n=50, p=5")
34063 maechler 170
 
171
\dontshow{
172
## An example with *long* formula that needs abbreviation:
61160 ripley 173
for(i in 1:5) assign(paste("long.var.name", i, sep = "."), runif(10))
34063 maechler 174
plot(lm(long.var.name.1 ~
175
        long.var.name.2 + long.var.name.3 + long.var.name.4 + long.var.name.5))
27442 ripley 176
}
34063 maechler 177
}
27442 ripley 178
\keyword{hplot}
179
\keyword{regression}