residual.plot {NCStats}R Documentation

Residual plot for lm or nls objects.

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

  residual.plot(object,...)
  ## S3 method for class 'lm':
  residual.plot(object,...)
  ## S3 method for class 'SLR':
  residual.plot(object,student=TRUE,outlier.test=TRUE,loess=FALSE,
                alpha=0.05,xlab="Fitted Values",ylab="Residuals",main=NULL,pch=16,
                col="black",lty.ref=3,lwd.ref=1,col.ref="black",lty.loess=2,
                lwd.loess=2,col.loess="red",loess.f=2/3,...)
  ## S3 method for class 'IVR':
  residual.plot(object,student=TRUE,outlier.test=TRUE,loess=FALSE,
                alpha=0.05,xlab="Fitted Values",ylab="Residuals",main=NULL,
                pch=c(16,21,15,22,17,24,c(3:14)),col="rich",lty.ref=3,lwd.ref=1,
                col.ref="black",lty.loess=2,lwd.loess=2,col.loess="red",loess.f=2/3,
                legend="topright",...)
  ## S3 method for class 'POLY':
  residual.plot(object,...)
  ## S3 method for class 'ONEWAY':
  residual.plot(object,student=TRUE,bp=TRUE,outlier.test=TRUE,
                loess=FALSE,alpha=0.05,xlab="Fitted Values",ylab="Residuals",
                main=NULL,pch=16,col="black",lty.ref=3,lwd.ref=1,col.ref="black",
                lty.loess=2,lwd.loess=2,col.loess="red",loess.f=2/3,...)
  ## S3 method for class 'TWOWAY':
  residual.plot(object,student=TRUE,bp=TRUE,outlier.test=TRUE,
                loess=FALSE,alpha=0.05,xlab="Fitted Values",ylab="Residuals",
                main=NULL,pch=16,col="black",lty.ref=3,lwd.ref=1,col.ref="black",
                lty.loess=2,lwd.loess=2,col.loess="red",loess.f=2/3,...)
  ## S3 method for class 'nls':
  residual.plot(object,loess=FALSE,xlab="Fitted Values",
                ylab="Residuals",main="",pch=16,col="black",lty.ref=3,lwd.ref=1,
                col.ref="black",lty.loess=2,lwd.loess=2,col.loess="red",loess.f=2/3,
                ...)

Arguments

object an lm or nls object (i.e., returned from fitting a model with either lm or nls).
student logical; if TRUE (default) then studentized residuals are plotted.
outlier.test logical; if TRUE (default) then an outlier.test will be performed and if the indivdiual with the largest studentized residual is deemed to be a significant outlier it will be noted on the residual plot by its observation number.
loess logical; if TRUE then a loess smoother line is fit to and shown on the residual plot.
bp logical; if TRUE (default) then the plot for the one-way and two-way ANOVA will be a boxplot. If FALSE then the plot will be a plot.
alpha a numeric indicating the alpha level to use for the outlier test (only used if outlier.test=TRUE).
xlab a string for labelling the x-axis.
ylab a string for labelling the y-axis.
main a string for the main label to the plot. Defaults to the model call.
pch a numeric indicating the plotting charachter to be used or a vector of numerics indicating what plotting charachters codes to use for the levels of the second factor. See par.
col a vector of color names indicating what color of points and lines to use for the levels of the first factor. See par.
lty.ref a numeric indicating the line type to use for the reference line at residual=0. See par.
lwd.ref a numeric indicating the line width to use for the reference line at residual=0. See par.
col.ref a numeric or character indicating the line color to use for the reference line at residual=0. See par.
lty.loess a numeric indicating the line type to use for loess fit line. See par.
lwd.loess a numeric indicating the line width to use for loess fit line. See par.
col.loess a numeric or character indicating the line color to use for loess fit line. See par.
loess.f a numeric for the smoother span. This gives the proportion of points in the plot which influence the smooth at each value. Larger values give more smoothness.
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.
... Other arguments to the generic plot function.

Details

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.

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

The loess line is fitted with the lowess function in the gplot package.

Value

None. However, a residual plot is produced.

Author(s)

Derek H. Ogle, dogle@northland.edu

See Also

fit.plot, diag.plot, outlier.test in car, and lowess in gplots.

Examples

data(Mirex)
Mirex$year <- factor(Mirex$year)
attach(Mirex)
lm1 <- lm(mirex~weight*year*species)
residual.plot(lm1)
residual.plot(lm1,loess=TRUE)
residual.plot(lm1,col="rainbow")
residual.plot(lm1,pch=16)
residual.plot(lm1,pch=16,col="black")
residual.plot(lm1,col="jet",col.ref="blue",lwd.ref=5)
residual.plot(lm1,legend=FALSE)

lm1a <- lm(mirex~weight*species*year)
residual.plot(lm1a)

lm2 <- lm(mirex~weight*year)
residual.plot(lm2)

lm3 <- lm(mirex~weight+year)
residual.plot(lm3)

lm4 <- lm(mirex~weight)
residual.plot(lm4)

lm5 <- lm(mirex~year)
residual.plot(lm5)
residual.plot(lm5,bp=FALSE)
residual.plot(lm5,bp=FALSE,loess=FALSE)

lm6 <- lm(mirex~species*year)
residual.plot(lm6)
residual.plot(lm6,bp=FALSE)

detach(Mirex)

## Example showing outlier detection
x <- c(runif(100))
y <- c(runif(100))
y[1] <- 7
lma <- lm(y~x)
residual.plot(lma)

[Package NCStats version 0.0-12 Index]