本文介绍了R中具有截距的回归模型的不正确的背线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(给出可重现的示例)在下面,我得到一条y截距大约为30的绝对线,但是回归表明y截距应该为37.2851我在哪里错了?

(reproducible example given) In the following, I get an abline line with y-intercept is about 30, but the regression says y-intercept should be 37.2851 Where am I wrong?

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')

我研究了SOF中所有类似的退位问题.尽管如此,我仍无法弄清楚课程中到底出了什么问题.

I looked at all the similar abline problems in SOF. Still, I could not figure out what is wrong in the course.

推荐答案

使用

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

请注意,您当前的代码会生成一个xlim而不是从0开始的图.要查看截距,您需要x = 0.不要忘记同时设置ylim来看到完整的一行.

Note, your current code produces a plot with xlim not starting from 0. While to see the intercept, you need x = 0. Don't forget to set ylim as well to see a complete line.

这篇关于R中具有截距的回归模型的不正确的背线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:27