residPlot | R Documentation |
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 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, ... )
object |
An |
... |
Other arguments to the generic |
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 |
col |
A vector of color names that indicates what color of points and lines to use for the levels of the first factor. See |
lty.ref |
A numeric that indicates the line type to use for the reference line at residual=0. See |
lwd.ref |
A numeric that indicates the line width to use for the reference line at residual=0. See |
col.ref |
A numeric or character that indicates the line color to use for the reference line at residual=0. See |
resid.type |
The type of residual to use. ‘Raw’ residuals are used by default. See details. |
outlier.test |
A logical that indicates if an |
alpha |
A numeric that indicates the alpha level to use for the outlier test (only used if |
loess |
A logical that indicates if a loess smoother line and approximate confidence interval band is fit to and shown on the residual plot ( |
lty.loess |
A numeric that indicates the line type to use for loess fit line. See |
lwd.loess |
A numeric that indicates the line width to use for loess fit line. See |
col.loess |
A numeric or character that indicates the line color to use for loess fit line. See |
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 |
cex.leg |
A single numeric values used to represent the character expansion value for the legend. Ignored if |
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 ( |
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
.
None. However, a residual plot is produced.
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.
Derek H. Ogle, derek@derekogle.com
See residualPlots
in car and nlsResiduals
in nlstools) for similar functionality. See outlierTest
in car for related methods.
data(Mirex,package="FSA") # 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.