平均への回帰

Pythonで学ぶstatsmodelsによる回帰入門

Maarten Van den Broeck

Content Developer at DataCamp

概念

  • 応答値 = 当てはめ値 + 残差
  • 「説明できた部分」+「説明できなかった部分」
  • 残差はモデルの問題と本質的なランダム性の双方で生じる
  • 極端な事例はしばしば偶然による
  • 平均への回帰とは、極端な事例が時間とともに持続しないこと
Pythonで学ぶstatsmodelsによる回帰入門

ピアソンの父子データセット

  • 父子ペア 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
Pythonで学ぶstatsmodelsによる回帰入門

散布図

fig = plt.figure()
sns.scatterplot(x="father_height_cm",
                y="son_height_cm",
                data=father_son)
plt.axline(xy1=(150, 150),
           slope=1,
           linewidth=2,
           color="green")
plt.axis("equal")
plt.show()

父の身長に対する息子の身長の散布図。父子が同じ身長になる直線を表示。父が高くなるほど、息子も高くなる。

Pythonで学ぶstatsmodelsによる回帰入門

回帰直線の追加

fig = plt.figure()

sns.regplot(x="father_height_cm",
            y="son_height_cm",
            data=father_son,
            ci = None, 
            line_kws={"color": "black"})

plt.axline(xy1 = (150, 150),
           slope=1,
           linewidth=2,
           color="green")

plt.axis("equal")
plt.show()

父の身長に対する息子の身長の散布図。回帰直線が注記されており、父子が同じ身長になる直線よりも傾きが緩やか。

Pythonで学ぶstatsmodelsによる回帰入門

回帰の実行

mdl_son_vs_father = ols("son_height_cm ~ father_height_cm",
                        data = father_son).fit()
print(mdl_son_vs_father.params)
Intercept           86.071975
father_height_cm     0.514093
dtype: float64
Pythonで学ぶstatsmodelsによる回帰入門

予測の作成

really_tall_father = pd.DataFrame(
  {"father_height_cm": [190]})

mdl_son_vs_father.predict(
  really_tall_father)
183.7
really_short_father = pd.DataFrame(
  {"father_height_cm": [150]})

mdl_son_vs_father.predict(
  really_short_father)
163.2
Pythonで学ぶstatsmodelsによる回帰入門

Let's practice!

Pythonで学ぶstatsmodelsによる回帰入門

Preparing Video For Download...