带有R中截距的回归模型的abline线不正确

时间:2016-11-20 13:22:11

标签: r plot regression linear-regression lm

(给出了可重复的例子)在下面,我得到一个y轴截距约为30的abline线,但回归说y-intercept应该是37.2851我错在哪里?

mtcars$mpg   # 21.0 21.0 22.8 ... 21.4 (32 obs)
mtcars$wt # 2.620 2.875 2.320 ... 2.780 (32 obs)
regression1 <- lm(mtcars$mpg ~ mtcars$wt)
coef(regression1) # mpg ~ 37.2851 - 5.3445wt
plot(mtcars$mpg ~ mtcars$wt, pch=19, col='gray50') # pch: shape of points
abline(h=mean(mtcars$mpg), lwd=2, col ='darkorange') # The y-coordinate of hor'l line: 20,09062
abline(lm(mtcars$mpg ~ mtcars$wt), lwd=2, col ='sienna')

enter image description here

我查看了SOF中所有类似的abline问题。不过,我无法弄清楚课程中出了什么问题。

1 个答案:

答案 0 :(得分:1)

使用

plot(mtcars$mpg ~ mtcars$wt, pch=19, col='gray50', xlim = c(0, 6), ylim = c(0, 40))

注意,您当前的代码会生成xlim不从0开始的图。在查看截距时,您需要x = 0。不要忘记设置ylim以查看完整的一行。

enter image description here

相关问题