The R Project SVN R

Rev

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