Regresi ke rerata

Pengantar Regresi dengan statsmodels di Python

Maarten Van den Broeck

Content Developer at DataCamp

Konsepnya

  • Nilai respons = nilai terpasang + residual
  • "Yang Anda jelaskan" + "yang tidak dapat Anda jelaskan"
  • Residual muncul karena masalah model dan juga keacakan dasar
  • Kasus ekstrem sering terjadi karena kebetulan
  • Regresi ke rerata berarti kasus ekstrem tidak bertahan lama
Pengantar Regresi dengan statsmodels di Python

Dataset ayah-anak Pearson

  • 1078 pasangan ayah/anak
  • Apakah ayah tinggi punya anak tinggi?
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 Adaptasi dari https://www.rdocumentation.org/packages/UsingR/topics/father.son
Pengantar Regresi dengan statsmodels di Python

Scatter plot

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()

Scatter plot tinggi anak vs tinggi ayah, dengan garis tinggi ayah=anak. Saat ayah lebih tinggi, anak juga cenderung lebih tinggi.

Pengantar Regresi dengan statsmodels di Python

Menambahkan garis regresi

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()

Scatter plot tinggi anak vs tinggi ayah, diberi garis tren linier. Garis tren kurang curam dibanding garis tinggi ayah=anak.

Pengantar Regresi dengan statsmodels di Python

Menjalankan regresi

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
Pengantar Regresi dengan statsmodels di Python

Membuat prediksi

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
Pengantar Regresi dengan statsmodels di Python

Ayo berlatih!

Pengantar Regresi dengan statsmodels di Python

Preparing Video For Download...