2016-11-17 23 views
-1

我对理解线性回归模型有一个非常基本的问题。考虑简单的情况:$ 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

为什么拟合值不趴在拟合直线?

回答

5

points(x = x, y = m$fitted.values, col = 4, pch = 4) #plot fitted values 

拟合值是y更换的最后一行,而不是x