residPlot: Construct a residual plot from lm or nls objects.

Description Usage Arguments Details Value Note Author(s) See Also Examples

Description

Constructs a residual plot for lm or nls objects. Different symbols for different groups can be added to the plot if an indicator variable regression is used.

Usage

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
residPlot(object, ...)

## S3 method for class 'lm'
residPlot(object, ...)

## S3 method for class 'SLR'
residPlot(
  object,
  xlab = "Fitted Values",
  ylab = "Residuals",
  main = "",
  pch = 16,
  col = "black",
  lty.ref = 3,
  lwd.ref = 1,
  col.ref = "black",
  resid.type = c("raw", "standardized", "studentized"),
  outlier.test = TRUE,
  alpha = 0.05,
  loess = FALSE,
  lty.loess = 2,
  lwd.loess = 1,
  col.loess = "black",
  trans.loess = 8,
  inclHist = TRUE,
  ...
)

## S3 method for class 'POLY'
residPlot(object, ...)

## S3 method for class 'IVR'
residPlot(object, legend = "topright", cex.leg = 1, box.lty.leg = 0, ...)

## S3 method for class 'ONEWAY'
residPlot(
  object,
  xlab = "Fitted Values",
  ylab = "Residuals",
  main = "",
  pch = 16,
  col = "black",
  lty.ref = 3,
  lwd.ref = 1,
  col.ref = "black",
  resid.type = c("raw", "standardized", "studentized"),
  bp = TRUE,
  outlier.test = TRUE,
  alpha = 0.05,
  loess = FALSE,
  lty.loess = 2,
  lwd.loess = 1,
  col.loess = "black",
  trans.loess = 8,
  inclHist = TRUE,
  ...
)

## S3 method for class 'TWOWAY'
residPlot(
  object,
  xlab = "Fitted Values",
  ylab = "Residuals",
  main = "",
  pch = 16,
  col = "black",
  lty.ref = 3,
  lwd.ref = 1,
  col.ref = "black",
  resid.type = c("raw", "standardized", "studentized"),
  bp = TRUE,
  outlier.test = TRUE,
  alpha = 0.05,
  loess = FALSE,
  lty.loess = 2,
  lwd.loess = 1,
  col.loess = "black",
  trans.loess = 8,
  inclHist = TRUE,
  ...
)

## S3 method for class 'nls'
residPlot(
  object,
  xlab = "Fitted Values",
  ylab = "Residuals",
  main = "",
  pch = 16,
  col = "black",
  lty.ref = 3,
  lwd.ref = 1,
  col.ref = "black",
  resid.type = c("raw", "standardized", "studentized"),
  loess = FALSE,
  lty.loess = 2,
  lwd.loess = 1,
  col.loess = "black",
  trans.loess = 8,
  inclHist = TRUE,
  ...
)

## S3 method for class 'nlme'
residPlot(
  object,
  xlab = "Fitted Values",
  ylab = "Residuals",
  main = "",
  pch = 16,
  col = "black",
  lty.ref = 3,
  lwd.ref = 1,
  col.ref = "black",
  resid.type = c("raw", "standardized", "studentized"),
  loess = FALSE,
  lty.loess = 2,
  lwd.loess = 1,
  col.loess = "black",
  trans.loess = 8,
  inclHist = TRUE,
  ...
)

Arguments

object

An lm or nls object (i.e., returned from fitting a model with either lm or nls).

...

Other arguments to the generic plot function.

xlab

A string for labeling the x-axis.

ylab

A string for labeling the y-axis.

main

A string for the main label to the plot. See details.

pch

A numeric that indicates the plotting character to be used or a vector of numerics that indicates what plotting character codes to use for the levels of the second factor. See par.

col

A vector of color names that indicates what color of points and lines to use for the levels of the first factor. See par.

lty.ref

A numeric that indicates the line type to use for the reference line at residual=0. See par.

lwd.ref

A numeric that indicates the line width to use for the reference line at residual=0. See par.

col.ref

A numeric or character that indicates the line color to use for the reference line at residual=0. See par.

resid.type

The type of residual to use. ‘Raw’ residuals are used by default. See details.

outlier.test

A logical that indicates if an outlierTest will TRUE (default) be performed and if the individual with the largest studentized residual is deemed to be a significant outlier it will be noted on the residual plot by its observation number.

alpha

A numeric that indicates the alpha level to use for the outlier test (only used if outlier.test=TRUE).

loess

A logical that indicates if a loess smoother line and approximate confidence interval band is fit to and shown on the residual plot (TRUE).

lty.loess

A numeric that indicates the line type to use for loess fit line. See par.

lwd.loess

A numeric that indicates the line width to use for loess fit line. See par.

col.loess

A numeric or character that indicates the line color to use for loess fit line. See par.

trans.loess

A single numeric that indicates how transparent the loess band should be (larger numbers are more transparent).

inclHist

A logical that indicates if a second pane that includes the histogram of residuals should be constructed.

legend

If TRUE, draw a legend and the user must click in the upper-left corner of where the legend should be placed; if FALSE do not draw a legend. If a vector of length 2 then draw the upper left corner of the legend at the coordinates given in the vector of length 2.

cex.leg

A single numeric values used to represent the character expansion value for the legend. Ignored if legend=FALSE.

box.lty.leg

A single numeric values used to indicate the type of line to use for the box around the legend. The default is to not plot a box.

bp

A logical that indicates if the plot for the one-way and two-way ANOVA will be a boxplot (TRUE; default) or not.

Details

Three types of residuals are allowed for most model types. Raw residuals are simply the difference between the observed response variable and the predicted/fitted value. Standardized residuals are internally studentized residuals returned by rstandard for linear models and are the raw residual divided by the standard deviation of the residuals for nonlinear models (as is done by nlsResiduals from nlstools). Studentized residuals are the externally studentized residuals returned by rstudent for linear models and are not available for nonlinear models.

Externally Studentized residuals are not supported for nls or nlme objects.

If outlier.test=TRUE then significant outliers are detected with outlierTest from the car package. See the help for this function for more details.

The user can include the model call as a title to the residual plot by using main="MODEL". This only works for models created with lm().

If the user chooses to add a legend without identifying coordinates for the upper-left corner of the legend (i.e., legend=TRUE) then the R console is suspended until the user places the legend by clicking on the produced graphic at the point where the upper-left corner of the legend should appear. A legend will only be placed if the mdl is an indicator variable regression, even if legend=TRUE.

Value

None. However, a residual plot is produced.

Note

This function is meant to allow newbie students the ability to easily construct residual plots for one-way ANOVA, two-way ANOVA, simple linear regression, and indicator variable regressions. The plots can be constructed by submitting a saved linear model to this function which allows students to interact with and visualize moderately complex linear models in a fairly easy and efficient manner.

Author(s)

Derek H. Ogle, derek@derekogle.com

See Also

See residualPlots in car and nlsResiduals in nlstools) for similar functionality. See outlierTest in car for related methods.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# create year factor variable
Mirex$fyear <- factor(Mirex$year)
Mirex$cyear <- as.character(Mirex$year)
Mirex$cspecies <- as.character(Mirex$species)

## One-way ANOVA
aov1 <- lm(mirex~fyear,data=Mirex)
residPlot(aov1)

## Two-Way ANOVA
aov2 <- lm(mirex~species*fyear,data=Mirex)
residPlot(aov2)

## Simple linear regression
slr1 <- lm(mirex~weight,data=Mirex)
residPlot(slr1)
residPlot(slr1,loess=TRUE,main="MODEL")

## Indicator variable regression with only one factor
ivr1 <- lm(mirex~weight*fyear,data=Mirex)
residPlot(ivr1)
residPlot(ivr1,inclHist=FALSE,pch=19)
residPlot(ivr1,inclHist=FALSE,pch=19,col="black")
residPlot(ivr1,legend=FALSE,loess=TRUE)

## Indicator variable regression (assuming same slope)
ivr2 <- lm(mirex~weight+fyear,data=Mirex)
residPlot(ivr2,legend=FALSE,loess=TRUE)

## Indicator variable regression with two factors
##    Reduce number of years for visual simplicity
Mirex2 <- droplevels(subset(Mirex,fyear %in% c(1977,1992)))

ivr3 <- lm(mirex~weight*fyear*species,data=Mirex2)
residPlot(ivr3)
residPlot(ivr3,loess=TRUE,legend=FALSE)

## IVR w/ factors in different order (notice use of colors and symbols)
ivr4 <- lm(mirex~weight*species*fyear,data=Mirex2)
residPlot(ivr4)


## Nonlinear regression ... from first example in nls()
DNase1 <- subset(DNase,Run==1)
fm1DNase1 <- nls(density~SSlogis(log(conc),Asym,xmid,scal),DNase1)
residPlot(fm1DNase1)
residPlot(fm1DNase1,resid.type="standardized")


## Examples showing outlier detection
x <- c(runif(100))
y <- c(7,runif(98),-5)
lma <- lm(y~x)
residPlot(lma)
residPlot(lma,resid.type="studentized")

droglenc/NCStats documentation built on June 5, 2021, 2:06 p.m.