简体   繁体   English

在R中绘制线性模型(lm)对象的机制?

[英]Mechanics of plotting a linear model (lm) object in R?

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. plot(m)将吐出一系列情节。 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? Paul Teetor的书如何知道编写plot(m, which=1)来选择残差图? ls.str(m) appears useless here. ls.str(m)在这里显得毫无用处。

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). 我的猜测是lm类有一些为plot()函数定义的接口,但是我不知道如何获取有关它是如何工作的或有什么图可用的信息(除了输入plot(m)和写入之外)落下黑盒吐出的东西)。

This page 这一页

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

explains what the 6 plots are. 解释了6个情节。

The first is residuals, which corresponds to which=1 from your example. 第一个是残差,对应于你的例子中的which=1 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 QQ 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. 我希望网页是朝着正确的方向迈出的一步。

You mentioned "the lm class has some kind of interface defined for the plot() function". 你提到“ lm类有一些为plot()函数定义的接口”。 In fact, this is a S3 mechanism in R, which follows "method.class" naming conventions. 实际上,这是R中的S3机制,它遵循“method.class”命名约定。 Here, the method is plot and the class is lm . 这里,方法是plot ,类是lm You don't have to type plot.lm in order to get these plots. 您不必键入plot.lm以获取这些图。 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 ; 当你调用plot ,R将首先检查第一个参数的类类型,并将它(在这种情况下为m )赋予类lm ; then R automatically calls the plot.lm function. 然后R自动调用plot.lm函数。

For the plot method, you can see that it applies to more classes by typing methods(plot) in R: 对于plot方法,您可以通过在R中键入methods(plot)来看到它适用于更多类:

 [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. 你看plot.lm就是其中之一。 To learn any one of these, you may use fix(plot.lm) . 要了解其中任何一个,您可以使用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! 当你在类lm的对象上调用plot时,显然你会知道构造了哪些图:)希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM