Cox 비례 위험 모형 적합하기

Python에서의 Survival Analysis

Shae Wang

Senior Data Scientist

위험함수와 위험률

위험함수 $h(t)$: 해당 시점까지 생존했을 때, 그 시점에 사건이 발생할 조건부 확률을 나타냅니다.

위험률: 사건 발생의 순간율

$$h(t)=-\frac{d}{dt}logS(t)$$

위험함수 $h(t)$와 생존함수 $S(t)$는 상호 유도 가능합니다.

Python에서의 Survival Analysis

비례 위험 가정

비례 위험 가정: 모든 개인의 위험은 서로 비례합니다.

개인 $A$와 $B$의 경우: $$h_A(t)=ch_B(t)$$

  1. 기준 위험함수가 있으며, 다른 위험은 스케일링 계수로 정의됩니다.
  2. 변수의 상대적 생존 영향은 시간에 따라 변하지 않습니다(시간 불변).

두 생존 곡선 간 비례 위험 가정 비교

Python에서의 Survival Analysis

Cox 비례 위험 모형

비례 위험 가정에 따르면: $$h(t|x)=b_0(t)exp\bigg(\sum^{n}_{i=1}b_i(x_i-\overline{x_i}\bigg)$$

$b_0(t)$: 시간에 따라 변하는 모집단 기준 위험함수

$exp\bigg(\sum^{n}_{i=1}b_i(x_i-\overline{x_i}\bigg)$: 공변량과 로그 위험의 선형 관계로, 시간에 따라 변하지 않음

  • Cox 비례 위험(Cox PH) 모형은 회귀 모형으로, 공변량을 사건 발생 시간/지속시간에 회귀합니다.
Python에서의 Survival Analysis

Cox PH 모형의 데이터 요건

  • 지속시간(Durations): 개인의 생존/지속 시간
  • 사건(Events): 사건 관측 여부(1=예, 0=아니오, 검열)
    • 제공하지 않으면, 모든 대상이 비검열로 가정합니다.
  • 공변량(Covariates): 연속형 또는 원-핫 인코딩된 범주형 변수
Python에서의 Survival Analysis

Cox PH 모형 적합

  1. CoxPHFitter 클래스를 임포트하고 인스턴스화
    from lifelines import CoxPHFitter
    coxph = CoxPHFitter()
    
  2. .fit()으로 데이터에 적합
    coxph.fit(df, duration_col, event_col)
    
  3. 요약, 공변량, 계수, 예측, 플롯 등 속성 확인
    coxph.summary()
    coxph.predict()
    
Python에서의 Survival Analysis

Cox PH 모형 예시

  • DataFrame: mortgage_df
  • 공변량:
    • house
    • principal
    • interest
    • property_tax
    • credit_score
  • 기타 컬럼: duration, paid_off
from lifelines import CoxPHFitter

coxph = CoxPHFitter() coxph.fit(df=mortgage_df, duration_col="duration", event_col="paid_off")
Python에서의 Survival Analysis

사용자 지정 모형

DataFrame 필터링:

new_df = mortgage_df.loc[:, 
          mortgage_df.columns!="house"]
coxph.fit(df=new_df,
          duration_col="duration",
          event_col="paid_off")

formula 매개변수 사용:

coxph.fit(df=mortgage_df,
          duration_col="duration",
          event_col="paid_off",
          formula="principal + interest 
          + property_tax + credit_score")
  • 더 간편하고 명확하나, 공변량이 매우 많을 때는 확장성 낮음
Python에서의 Survival Analysis

계수 해석

print(coxph.summary)
<lifelines.CoxPHFitter: fitted with 1808 observations, 340 censored>
                        coef  exp(coef)  se(coef)      z       p
covariate house        -0.38       0.68      0.19. -1.98    0.05
          principal    -0.06       0.94      0.02  -2.61    0.01
          interest      0.31       1.37      0.31   1.02    0.31
          property_tax -0.15       0.86      0.21  -0.71    0.48
          credit_score -0.43       0.65      0.38  -1.14.   0.26
  • 위험비: $e^{coef}$
    • interest가 중앙값에서 1단위 증가하면 위험은 $e^{0.31}=1.37$배, 즉 기준 위험 대비 37% 증가합니다.
Python에서의 Survival Analysis

연습해 봅시다!

Python에서의 Survival Analysis

Preparing Video For Download...