Question

Example:

x <- c(1,2,5,6)
y <- c(3,5,2,9)
m <- lm(y ~ x)
plot(m)

plot(m) will spit out a series of plots. My question is, how do I know what plots it will spit out? How does Paul Teetor's book know to write plot(m, which=1) to select the residuals plot? ls.str(m) appears useless here.

My guess is that the lm class has some kind of interface defined for the plot() function, but I have no idea how to get any information about how that works or what plots are available (aside from just typing plot(m) and writing down what the black box spits out).

Was it helpful?

Solution

You mentioned "the lm class has some kind of interface defined for the plot() function". In fact, this is a S3 mechanism in R, which follows "method.class" naming conventions. Here, the method is plot and the class is lm. You don't have to type plot.lm in order to get these plots. When you call plot, R will first examine the class type of the first argument, and fount it (m in this case) to be of class lm; then R automatically calls the plot.lm function.

For the plot method, you can see that it applies to more classes by typing methods(plot) in R:

 [1] plot.acf*           plot.data.frame*    plot.decomposed.ts* plot.default        plot.dendrogram*   
 [6] plot.density        plot.ecdf           plot.factor*        plot.formula*       plot.function      
[11] plot.gofm*          plot.gofv*          plot.hap.score*     plot.hclust*        plot.histogram*    
[16] plot.HoltWinters*   plot.isoreg*        plot.lm             plot.md             plot.medpolish*    
[21] plot.mlm            plot.ppr*           plot.prcomp*        plot.princomp*      plot.profile.nls*  
[26] plot.spec           plot.spline*        plot.stepfun        plot.stl*           plot.table*        
[31] plot.ts             plot.tskernel*      plot.TukeyHSD       plot.xyVector*     

   Non-visible functions are asterisked

You see plot.lm is one of them. To learn any one of these, you may use fix(plot.lm). Then you will notice at the first lines:

caption = list("Residuals vs Fitted", 
    "Normal Q-Q", "Scale-Location", "Cook's distance", "Residuals vs Leverage", 
    expression("Cook's dist vs Leverage  " * h[ii]/(1 - h[ii])))

Obviously you'll know which plots are constructed when you call plot on an object of class lm :) Hope this helps!

OTHER TIPS

This page

http://stat.ethz.ch/R-manual/R-patched/library/stats/html/plot.lm.html

explains what the 6 plots are.

The first is residuals, which corresponds to which=1 from your example. Here is a quote from that page:

Six plots (selectable by which) are currently available: a plot of residuals against fitted values, a Scale-Location plot of sqrt(| residuals |) against fitted values, a Normal Q-Q plot, a plot of Cook's distances versus row labels, a plot of residuals against leverages, and a plot of Cook's distances against leverage/(1-leverage). By default, the first three and 5 are provided.

I hope that webpage is a step in the right direction for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top