均值回歸

R 迴歸入門

Richie Cotton

Data Evangelist

概念

  • 反應值 = 擬合值 + 殘差
  • 「你解釋的部分」+「你無法解釋的部分」
  • 殘差來自模型問題,亦來自根本的隨機性
  • 極端情況多半出於隨機
  • 「均值回歸」意指極端不會長久持續
R 迴歸入門

Pearson 的父子資料集

  • 1078 對父子
  • 高個爸爸會有高個兒子嗎?
father_height_cm son_height_cm
165.2 151.8
160.7 160.6
165.0 160.9
167.0 159.5
155.3 163.3
... ...
1 Adapted from https://www.rdocumentation.org/packages/UsingR/topics/father.son
R 迴歸入門

散佈圖

plt_son_vs_father <- ggplot(
  father_son, 
  aes(father_height_cm, son_height_cm)
) +
  geom_point() +
  geom_abline(color = "green", size = 1) +
  coord_fixed()

兒子身高對上爸爸身高的散佈圖,並有一條父子同高的參考線。爸爸越高,兒子也越高。

R 迴歸入門

加入回歸線

plt_son_vs_father +
  geom_smooth(method = "lm", se = FALSE)

加上線性趨勢線的父子身高散佈圖。該趨勢線比父子同高的參考線還要平緩。

R 迴歸入門

執行回歸

mdl_son_vs_father <- lm(
  son_height_cm ~ father_height_cm, 
  data = father_son
)
Call:
lm(formula = son_height_cm ~ father_height_cm, data = father_son)

Coefficients:
     (Intercept)  father_height_cm  
          86.072             0.514
R 迴歸入門

進行預測

really_tall_father <- tibble(
  father_height_cm = 190
)
predict(mdl_son_vs_father, really_tall_father)
183.7
really_short_father <- tibble(
  father_height_cm = 150
)
predict(mdl_son_vs_father, really_short_father)
163.2
R 迴歸入門

一起來練習吧!

R 迴歸入門

Preparing Video For Download...