
Last chance! 50% off unlimited learning
Sale ends in
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.
residPlot(object, ...)# S3 method for lm
residPlot(object, ...)
# S3 method for 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 POLY
residPlot(object, ...)
# S3 method for IVR
residPlot(object, legend = "topright", cex.leg = 1,
box.lty.leg = 0, ...)
# S3 method for 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 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 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 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, ...)
An lm
or nls
object (i.e., returned from fitting a model with either lm
or nls
).
Other arguments to the generic plot
function.
A string for labeling the x-axis.
A string for labeling the y-axis.
A string for the main label to the plot. See details.
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
.
A vector of color names that indicates what color of points and lines to use for the levels of the first factor. See par
.
A numeric that indicates the line type to use for the reference line at residual=0. See par
.
A numeric that indicates the line width to use for the reference line at residual=0. See par
.
A numeric or character that indicates the line color to use for the reference line at residual=0. See par
.
The type of residual to use. ‘Raw’ residuals are used by default. See details.
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.
A numeric that indicates the alpha level to use for the outlier test (only used if outlier.test=TRUE
).
A logical that indicates if a loess smoother line and approximate confidence interval band is fit to and shown on the residual plot (TRUE
).
A numeric that indicates the line type to use for loess fit line. See par
.
A numeric that indicates the line width to use for loess fit line. See par
.
A numeric or character that indicates the line color to use for loess fit line. See par
.
A single numeric that indicates how transparent the loess band should be (larger numbers are more transparent).
A logical that indicates if a second pane that includes the histogram of residuals should be constructed.
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.
A single numeric values used to represent the character expansion value for the legend. Ignored if legend=FALSE
.
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.
A logical that indicates if the plot for the one-way and two-way ANOVA will be a boxplot (TRUE
; default) or not.
None. However, a residual plot is produced.
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
.
See residualPlots
in car and nlsResiduals
in nlstools) for similar functionality and fitPlot
and outlierTest
in car for related methods.
# NOT RUN {
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)
# add the loess line
residPlot(lm1,loess=TRUE,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)
## Examples 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")
# multiple outliers
y <- c(7,runif(98),-5)
lmb <- lm(y~x)
residPlot(lmb)
# check that NAs are handled properly ... label should be 100
y <- c(NA,NA,runif(97),7)
lmc <- lm(y~x)
residPlot(lmc)
## 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