Learn R Programming

FSA (version 0.8.11)

residPlot: Construct a residual plot from 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

residPlot(object, ...)
"residPlot"(object, ...)
"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 = TRUE, lty.loess = 2, lwd.loess = 1, col.loess = "black", trans.loess = 4, inclHist = TRUE, ...)
"residPlot"(object, ...)
"residPlot"(object, legend = "topright", cex.leg = 1, box.lty.leg = 0, ...)
"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 = TRUE, lty.loess = 2, lwd.loess = 1, col.loess = "black", trans.loess = 4, inclHist = TRUE, ...)
"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 = TRUE, lty.loess = 2, lwd.loess = 1, col.loess = "black", trans.loess = 4, inclHist = TRUE, ...)
"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 = TRUE, lty.loess = 2, lwd.loess = 1, col.loess = "black", trans.loess = 4, inclHist = TRUE, ...)
"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 = TRUE, lty.loess = 2, lwd.loess = 1, col.loess = "black", trans.loess = 4, inclHist = TRUE, ...)

Arguments

object
An lm or nls object (i.e., returned from fitting a model with either lm or nls).
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. See details.
pch
A numeric that indicates the plotting charachter to be used or a vector of numerics that indicates what plotting charachters 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 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.
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.
...
Other arguments to the generic plot function.

Value

None. However, a residual plot is produced.

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. The

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 wtih 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.

See Also

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

Examples

Run this code
data(Mirex)
# create year as a factor variable
Mirex$fyear <- factor(Mirex$year)
# reduce number of years for visual simplicity
Mirex2 <- filterD(Mirex,fyear %in% c(1977,1992))

## Indicator variable regression with two factors
lm1 <- lm(mirex~weight*fyear*species,data=Mirex2)
# defaults
residPlot(lm1)
# remove the histogram
residPlot(lm1,inclHist=FALSE)
# remove the loess line
residPlot(lm1,loess=FALSE,inclHist=FALSE)
# modify colors used
residPlot(lm1,col="rainbow",inclHist=FALSE)
# use only one point type -- notice that all points are of same type
residPlot(lm1,pch=16,inclHist=FALSE)
# use only one point and one color (might as well not use legend also)
residPlot(lm1,pch=16,col="black",legend=FALSE,inclHist=FALSE)
# can accomplish same thing just by removing the legend
residPlot(lm1,legend=FALSE,inclHist=FALSE)
# modify the reference line
residPlot(lm1,col.ref="blue",lwd.ref=5,inclHist=FALSE)
# include model in the title
residPlot(lm1,main="MODEL")
# use Studentized residuals
residPlot(lm1,resid.type="studentized",inclHist=FALSE)
# use Standardized residuals
residPlot(lm1,resid.type="standardized",inclHist=FALSE)

## Indicator variable regression with same two factors but in different order
##   (notice use of colors and symbols)
lm1a <- lm(mirex~weight*species*fyear,data=Mirex2)
residPlot(lm1a)

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

## Indicator variable regression (assuming same slope)
lm3 <- lm(mirex~weight+fyear,data=Mirex)
residPlot(lm3)

## Simple linear regression
lm4 <- lm(mirex~weight,data=Mirex)
residPlot(lm4)

## One-way ANOVA
lm5 <- lm(mirex~fyear,data=Mirex)
# default (uses boxplots)
residPlot(lm5)
# use points rather than boxplot
residPlot(lm5,bp=FALSE)

## Two-Way ANOVA
lm6 <- lm(mirex~species*fyear,data=Mirex)
# default (uses boxplots)
residPlot(lm6)
# No boxplots
residPlot(lm6,bp=FALSE)

## Example showing outlier detection
x <- c(runif(100))
y <- c(7,runif(99))
lma <- lm(y~x)
residPlot(lma)
# with studentized residuals
residPlot(lma,resid.type="studentized")

## 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")

Run the code above in your browser using DataLab