การ Fitting โมเดล Cox Proportional Hazards

Survival Analysis ด้วย Python

Shae Wang

Senior Data Scientist

ฟังก์ชันอันตรายและอัตราอันตราย

ฟังก์ชันอันตราย $h(t)$: อธิบายความน่าจะเป็นที่เหตุการณ์จะเกิดขึ้น ณ เวลาหนึ่ง โดยที่รอดชีวิตมาจนถึงเวลานั้น

อัตราอันตราย: อัตราการเกิดเหตุการณ์แบบทันที

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

ฟังก์ชันอันตราย $h(t)$ และ ฟังก์ชันการรอดชีวิต $S(t)$ สามารถคำนวณจากกันและกันได้

Survival Analysis ด้วย Python

สมมติฐาน Proportional Hazards

สมมติฐาน Proportional Hazards: อันตรายของแต่ละบุคคลเป็นสัดส่วนซึ่งกันและกัน

ในกรณีของบุคคล $A$ และบุคคล $B$: $$h_A(t)=ch_B(t)$$

  1. มี ฟังก์ชันอันตรายพื้นฐาน และอันตรายอื่นระบุด้วย ตัวคูณปรับขนาด
  2. ผลกระทบต่อการรอดชีวิตสัมพัทธ์ ที่เกี่ยวข้องกับ ตัวแปร ไม่เปลี่ยนแปลงตามเวลา (time-invariant)

การเปรียบเทียบสมมติฐาน Proportional Hazard ระหว่างสองเส้นโค้งการรอดชีวิต

Survival Analysis ด้วย Python

โมเดล Cox Proportional Hazards

อิงตามสมมติฐาน Proportional Hazards: $$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)$: ความสัมพันธ์เชิงเส้นระหว่าง covariate กับ log ของอันตราย ซึ่งไม่เปลี่ยนแปลงตามเวลา

  • โมเดล Cox Proportional Hazards (Cox PH) คือ โมเดลการถดถอย ที่ถดถอย covariate บนเวลาจนถึงเหตุการณ์/ระยะเวลา
Survival Analysis ด้วย Python

ข้อกำหนดข้อมูลสำหรับโมเดล Cox PH

  • Durations: ช่วงเวลา/อายุขัยของแต่ละบุคคล
  • Events: ระบุว่าเหตุการณ์ถูกสังเกตหรือไม่ (1=ใช่, 0=ไม่ใช่, censored)
    • หากไม่ระบุ โมเดลจะถือว่าไม่มีผู้ถูก censored
  • Covariates: ตัวแปรต่อเนื่องหรือตัวแปรหมวดหมู่แบบ one-hot encoded สำหรับการถดถอย
Survival Analysis ด้วย Python

การ Fit โมเดล Cox PH

  1. Import และสร้าง instance ของคลาส CoxPHFitter
    from lifelines import CoxPHFitter
    coxph = CoxPHFitter()
    
  2. เรียก .fit() เพื่อ fit estimator กับข้อมูล
    coxph.fit(df, duration_col, event_col)
    
  3. เข้าถึง property อื่นเพื่อตรวจสอบสรุปโมเดล, covariate, coefficients, พยากรณ์, พล็อต ฯลฯ
    coxph.summary()
    coxph.predict()
    
Survival Analysis ด้วย Python

ตัวอย่างโมเดล Cox PH

  • DataFrame: mortgage_df
  • Covariates:
    • 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")
Survival Analysis ด้วย Python

โมเดลแบบกำหนดเอง

กรอง 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")
  • สะดวกและชัดเจนกว่า แต่ไม่เหมาะเมื่อมี covariate จำนวนมาก
Survival Analysis ด้วย Python

การแปลความหมาย coefficients

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
  • Hazard ratio: $e^{coef}$
    • การเพิ่มขึ้นหนึ่งหน่วยของ interest จากค่ามัธยฐาน -> อันตรายเปลี่ยนแปลงด้วยตัวคูณ $e^{0.31}=1.37$ ซึ่งเพิ่มขึ้น 37% เมื่อเทียบกับอันตรายพื้นฐาน
Survival Analysis ด้วย Python

มาฝึกกันเถอะ!

Survival Analysis ด้วย Python

Preparing Video For Download...