심슨의 역설

Python의 statsmodels로 배우는 중급 회귀

Maarten Van den Broeck

Content Developer at DataCamp

매우 교묘한 역설!

심슨의 역설은 전체 데이터셋에서의 모델 추세가 데이터셋의 하위 집합에서의 추세와 매우 다를 때 발생합니다.

trend = 기울기 계수

Python의 statsmodels로 배우는 중급 회귀

합성 심슨 데이터

x y group
62.24344 70.60840 D
52.33499 14.70577 B
56.36795 46.39554 C
66.80395 66.17487 D
66.53605 89.24658 E
62.38129 91.45260 E
  • 데이터는 A~E의 5개 그룹으로 라벨링됨
1 https://www.rdocumentation.org/packages/datasauRus/topics/simpsons_paradox
Python의 statsmodels로 배우는 중급 회귀

선형 회귀

전체 데이터셋

mdl_whole = ols("y ~ x", 
                 data=simpsons_paradox).fit()

print(mdl_whole.params)
Intercept           -38.554  
x                     1.751  

그룹별

mdl_by_group = ols("y ~ group + group:x + 0",
                   data = simpsons_paradox).fit()

print(mdl_by_group.params)
  groupA    groupB    groupC    groupD    groupE  
 32.5051   67.3886   99.6333  132.3932  123.8242  
groupA:x  groupB:x  groupC:x  groupD:x  groupE:x  
 -0.6266   -1.0105   -0.9940   -0.9908   -0.5364
Python의 statsmodels로 배우는 중급 회귀

전체 데이터셋 시각화

sns.regplot(x="x",
            y="y",
            data=simpsons_paradox,
            ci=None)

심슨의 역설 데이터셋 산점도, 모든 그룹 결합. 추세는 양의 방향임.png

Python의 statsmodels로 배우는 중급 회귀

그룹별 시각화

sns.lmplot(x="x",
           y="y",
           data=simpsons_paradox,
           hue="group",
           ci=None)

심슨의 역설 데이터셋 산점도, 그룹별 색상. 각 그룹의 추세는 이제 음의 방향임.png

Python의 statsmodels로 배우는 중급 회귀

차이를 해석하기

좋은 조언

가능하면 데이터셋을 시각화하십시오.

일반적인 조언

최적의 모델은 일반적으로 정해져 있지 않습니다. 데이터와 해결하려는 질문에 따라 달라집니다.

더 좋은 조언

모델링 전에 질문을 명확히 하십시오.

Python의 statsmodels로 배우는 중급 회귀

시험 점수 예시

scatter-video-games-whole.png

scatter-video-games-by-group.png

Python의 statsmodels로 배우는 중급 회귀

감염병 예시

scatter-cities-whole.png

scatter-cities-by-group.png

Python의 statsmodels로 배우는 중급 회귀

차이를 해석하기

  • 보통(항상은 아님) 그룹별 모델이 더 많은 통찰을 줍니다.
  • 설명 변수를 빠뜨렸습니까?
  • 맥락이 중요합니다.
Python의 statsmodels로 배우는 중급 회귀

실제 데이터의 심슨의 역설

  • 실제 데이터에서는 역설이 덜 뚜렷합니다.
  • 방향이 완전히 바뀌기보다 기울기가 0에 가까울 수 있습니다.
  • 모든 그룹에서 나타나지 않을 수 있습니다.
Python의 statsmodels로 배우는 중급 회귀

연습해 봅시다!

Python의 statsmodels로 배우는 중급 회귀

Preparing Video For Download...