Regression to the mean

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Maarten Van den Broeck

Content Developer at DataCamp

แนวคิด

  • ค่าตอบสนอง = ค่าที่ fit + ส่วนเหลือ
  • "สิ่งที่อธิบายได้" + "สิ่งที่อธิบายไม่ได้"
  • ส่วนเหลือเกิดจากปัญหาในโมเดล และ ความสุ่มโดยธรรมชาติ
  • กรณีสุดขีดมักเกิดจากความสุ่ม
  • Regression to the mean หมายความว่ากรณีสุดขีดไม่คงอยู่ตลอดไป
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

ชุดข้อมูลส่วนสูงพ่อ-ลูกชายของ Pearson

  • คู่พ่อ/ลูกชาย 1,078 คู่
  • พ่อที่สูงมีลูกชายที่สูงด้วยหรือไม่?
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 ดัดแปลงจาก https://www.rdocumentation.org/packages/UsingR/topics/father.son
การถดถอยเบื้องต้นด้วย statsmodels ใน 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 แสดงส่วนสูงของลูกชายเทียบกับพ่อ พร้อมเส้นที่แสดงจุดที่ส่วนสูงพ่อและลูกชายเท่ากัน โดยเมื่อพ่อสูงขึ้น ลูกชายก็สูงขึ้นด้วย

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

เพิ่มเส้น regression

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 แสดงส่วนสูงของลูกชายเทียบกับพ่อ พร้อมเส้น trend linear เส้น trend มีความชันน้อยกว่าเส้นที่แสดงจุดที่ส่วนสูงพ่อและลูกชายเท่ากัน

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

รัน regression

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
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

การทำนายค่า

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
การถดถอยเบื้องต้นด้วย statsmodels ใน Python

มาฝึกกันเถอะ!

การถดถอยเบื้องต้นด้วย statsmodels ใน Python

Preparing Video For Download...