Python에서의 Survival Analysis
Shae Wang
Senior Data Scientist
위험함수 $h(t)$: 해당 시점까지 생존했을 때, 그 시점에 사건이 발생할 조건부 확률을 나타냅니다.
위험률: 사건 발생의 순간율
$$h(t)=-\frac{d}{dt}logS(t)$$
위험함수 $h(t)$와 생존함수 $S(t)$는 상호 유도 가능합니다.
비례 위험 가정: 모든 개인의 위험은 서로 비례합니다.
개인 $A$와 $B$의 경우: $$h_A(t)=ch_B(t)$$

비례 위험 가정에 따르면: $$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)$: 공변량과 로그 위험의 선형 관계로, 시간에 따라 변하지 않음
CoxPHFitter 클래스를 임포트하고 인스턴스화from lifelines import CoxPHFitter
coxph = CoxPHFitter()
.fit()으로 데이터에 적합coxph.fit(df, duration_col, event_col)
coxph.summary()
coxph.predict()
mortgage_dfhouseprincipal interest property_taxcredit_scoreduration, paid_offfrom lifelines import CoxPHFittercoxph = CoxPHFitter() coxph.fit(df=mortgage_df, duration_col="duration", event_col="paid_off")
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")
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
interest가 중앙값에서 1단위 증가하면 위험은 $e^{0.31}=1.37$배, 즉 기준 위험 대비 37% 증가합니다.Python에서의 Survival Analysis