다변량 로지스틱 회귀

Python으로 배우는 Generalized Linear Models

Ita Cirovic Donev

Data Science Consultant

다변량 설정

  • 모형식 $$ \text{logit}(y) = \beta_0+\beta_1\color{red}{x_1} $$
Python으로 배우는 Generalized Linear Models

다변량 설정

  • 모형식 $$ \text{logit}(y) = \color{blue}{\beta_0}+\color{blue}{\beta_1}\color{red}{x_1} $$
Python으로 배우는 Generalized Linear Models

다변량 설정

  • 모형식 $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \beta_2\color{red}{x_2} + ... + \beta_p \color{red}{x_p} $$
Python으로 배우는 Generalized Linear Models

다변량 설정

  • 모형식 $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \color{blue}{\beta_2}\color{red}{x_2} + ... + \color{blue}{\beta_p}\color{red}{x_p} $$

  • Python에서

    model = glm('y ~ x1 + x2 + x3 + x4', 
              data = my_data, 
              family = sm.families.Binomial()).fit()
    
Python으로 배우는 Generalized Linear Models

예시 - 우물 전환

formula = 'switch ~ distance100 + arsenic'
wells_fit = glm(formula = formula, data = wells, 
                family = sm.families.Binomial()).fit()
===============================================================================
                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       0.0027      0.079      0.035      0.972      -0.153       0.158
distance100    -0.8966      0.104     -8.593      0.000      -1.101      -0.692
arsenic         0.4608      0.041     11.134      0.000       0.380       0.542
===============================================================================
Python으로 배우는 Generalized Linear Models

예시 - 우물 전환

                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       0.0027      0.079      0.035      0.972      -0.153       0.158
distance100    -0.8966      0.104     -8.593      0.000      -1.101      -0.692
arsenic         0.4608      0.041     11.134      0.000       0.380       0.542
  • 두 계수 모두 통계적으로 유의함
  • 계수의 부호가 논리적임
  • distance100 1단위 증가 → 로짓이 0.89 감소
  • arsenic 1단위 증가 → 로짓이 0.46 증가
Python으로 배우는 Generalized Linear Models

변수 추가의 영향

  • arsenic 변수의 영향
  • distance100가 -0.62에서 -0.89로 변화
  • 안전한 우물에서 멀수록
    • 비소 수치가 높을 가능성이 큼
                  coef    std err 
---------------------------------
Intercept       0.0027      0.079
distance100    -0.8966      0.104
arsenic         0.4608      0.041  
                  coef    std err
---------------------------------
Intercept       0.6060      0.060
distance100    -0.6291      0.097  
Python으로 배우는 Generalized Linear Models

다중공선성

  • 다른 설명변수와 상관이 있는 변수들

상관이 0.8, 0.4, 0, -0.4, -0.8일 때 두 변수의 구조.

  • 계수의 표준오차 증가
    • 계수가 통계적으로 유의하지 않을 수 있음
1 https://en.wikipedia.org/wiki/Correlation_and_dependence
Python으로 배우는 Generalized Linear Models

다중공선성 존재 여부?

확인 포인트

  • 계수는 유의하지 않지만, 변수가 y와 높은 상관
  • 변수 추가/제거 시 계수가 크게 변함
  • 계수 부호가 비논리적임
  • 변수 간 쌍대 상관이 높음
Python으로 배우는 Generalized Linear Models

분산 팽창 계수(VIF)

  • 다중공선성 진단 중 가장 널리 사용
    • 각 설명변수별로 계산
    • 계수 분산이 얼마나 팽창했는지 표시
  • 권장 임계값 VIF > 2.5
  • Python에서
from statsmodels.stats.outliers_influence import variance_inflation_factor
Python으로 배우는 Generalized Linear Models

연습해 봅시다!

Python으로 배우는 Generalized Linear Models

Preparing Video For Download...