2016-11-17 50 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