为什么拟合值不在拟合线上

时间:2016-11-17 15:06:36

标签: r regression

关于理解线性回归模型,我有一个非常基本的问题。考虑简单的情况,当$ y = a + bx + e $时,其中$ e $是错误术语。我使用OLS估算系数$ a $和$ b $。然后拟合值是$ \ hat y = \ hat a + \ hat b x $。他们不应该躺在同一条线上,因为它是线性关系吗?我问,因为我在R中进行简单的操作并且具有违反直觉的结果

x <- rnorm(20, 3, 1)
y <- 12 + 4*x + rnorm(20, 0, 0.5)
m <- lm(y ~ x)
a <- coef(m)[1]
b = coef(m)[2]
plot(x, y) #plot initial data
abline(a = a, b = b, lwd = 2, col = 2) #plot fitted line
points(x = m$fitted.values, col = 4, pch = 4) #plot fitted values
legend('topleft', c("Actual", "Fitted line", "Fitted values"), col = c(1, 2, 4), pch = c(1, 1, 4), lty = c(0, 1, 0))

enter image description here

为什么拟合值不在拟合线上?

1 个答案:

答案 0 :(得分:5)

替换最后一行
points(x = x, y = m$fitted.values, col = 4, pch = 4) #plot fitted values

拟合值适用于y,而非x