로지스틱 회귀를 이용한 확률 예측

R로 하는 Supervised Learning: 회귀

Nina Zumel and John Mount

Win-Vector LLC

확률 예측

  • 사건 발생 여부 예측 (예/아니오): 분류
  • 사건 발생 확률 예측: 회귀
  • 선형 회귀: [$-\infty$, $\infty$] 범위의 값 예측
  • 확률: [0,1] 구간으로 제한
    • 따라서 비선형으로 분류
R로 하는 Supervised Learning: 회귀

예시: 뒤센 근이영양증(DMD) 예측

  • 결과: has_dmd    입력: CK, H
R로 하는 Supervised Learning: 회귀

선형 회귀 모델

model <- lm(has_dmd ~ CK + H, 
            data = train)

test$pred <- predict(
    model, 
    newdata = test
)

결과: has_dmd $\in$ {0,1}

  • 0: FALSE
  • 1: TRUE

모델이 [0:1] 범위를 벗어난 값을 예측함

R로 하는 Supervised Learning: 회귀

로지스틱 회귀

$$ log(\frac{p}{1-p}) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... $$

glm(formula, data, family = binomial)
  • 일반화 선형 모델
  • 입력이 로그 오즈에서 가산적·선형임을 가정: $log( p/(1-p) )$
  • family: 모델의 오차 분포 정의
    • 로지스틱 회귀: family = binomial
R로 하는 Supervised Learning: 회귀

DMD 모델

model <- glm(has_dmd ~ CK + H, data = train, family = binomial)
  • 결과: 두 클래스, 예) $a$와 $b$
  • 모델이 $Prob(b)$를 반환
    • 권장: 0/1 또는 FALSE/TRUE
R로 하는 Supervised Learning: 회귀

로지스틱 회귀 모델 해석

model
Call:  glm(formula = has_dmd ~ CK + H, family = binomial, data = train)

Coefficients:
(Intercept)           CK            H  
  -16.22046      0.07128      0.12552  

Degrees of Freedom: 86 Total (i.e. Null);  84 Residual
Null Deviance:       110.8 
Residual Deviance: 45.16     AIC: 51.16
R로 하는 Supervised Learning: 회귀

glm() 모델로 예측하기

predict(model, newdata, type = "response")
  • newdata: 기본값은 훈련 데이터
  • 확률 반환: type = "response" 사용
    • 기본값: 로그 오즈 반환
R로 하는 Supervised Learning: 회귀

DMD 모델

model <- glm(has_dmd ~ CK + H, data = train, family = binomial)
test$pred <- predict(model, newdata = test, type = "response")

R로 하는 Supervised Learning: 회귀

로지스틱 회귀 모델 평가: pseudo-$R^2$

$$ R^2 = 1 - \frac{RSS}{SS_{Tot}} $$

$$ pseudo R^2 = 1 - \frac{deviance}{null.deviance} $$

  • 이탈도(Deviance): 분산(RSS)에 대응
  • 영 이탈도(Null deviance): $SS_{Tot}$에 대응
  • pseudo R^2: 설명된 이탈도
R로 하는 Supervised Learning: 회귀

훈련 데이터의 Pseudo-$R^2$

broom::glance() 사용

glance(model) %>% 
  summarize(pR2 = 1 - deviance/null.deviance)
   pseudoR2
1 0.5922402

sigr::wrapChiSqTest() 사용

wrapChiSqTest(model)
"... pseudo-R2=0.59 ..."
R로 하는 Supervised Learning: 회귀

테스트 데이터의 Pseudo-$R^2$

# Test data
test %>% 
  mutate(pred = predict(model, newdata = test, type = "response")) %>%
  wrapChiSqTest("pred", "has_dmd", TRUE)

인수:

  • 데이터 프레임
  • 예측 열 이름
  • 결과 열 이름
  • 목표값 (목표 이벤트)
R로 하는 Supervised Learning: 회귀

게인 커브 플롯

GainCurvePlot(test, "pred","has_dmd", "DMD model on test")

R로 하는 Supervised Learning: 회귀

연습해 봅시다!

R로 하는 Supervised Learning: 회귀

Preparing Video For Download...