평균으로의 회귀

Python에서 statsmodels로 살펴보는 회귀 소개

Maarten Van den Broeck

Content Developer at DataCamp

개념

  • 반응값 = 적합값 + 잔차
  • "설명된 부분" + "설명되지 않은 부분"
  • 잔차는 모델의 문제 근본적인 무작위성으로 인해 발생합니다
  • 극단적인 경우는 대개 무작위성 때문입니다
  • 평균으로의 회귀란 극단적인 경우가 시간이 지나도 지속되지 않음을 의미합니다
Python에서 statsmodels로 살펴보는 회귀 소개

피어슨의 아버지-아들 데이터셋

  • 아버지-아들 쌍 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 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로 살펴보는 회귀 소개

연습해 봅시다!

Python에서 statsmodels로 살펴보는 회귀 소개

Preparing Video For Download...