含协变量的 Weibull 模型

Python 中的生存分析

Shae Wang

Senior Data Scientist

比较生存函数

使用 Kaplan–Meier 估计比较组别:

公寓 vs. 独栋房屋的 Kaplan–Meier 生存曲线。

使用 log-rank 检验比较组别:

<lifelines.StatisticalResult: logrank_test>
 null_distribution = chi squared
degrees_of_freedom = 1
         test_name = logrank_test
 test_statistic    p  -log2(p)
           0.09 0.77      0.38

   

问:如何评估一个或多个连续变量对生存函数的影响?

Python 中的生存分析

生存回归

  • 一种可将协变量纳入的生存函数建模方法
  • 量化各协变量对生存函数的影响

$$Y_i=f(X_i,\beta)$$

$$Y_i:\text{持续时间},\ X_i:\text{协变量}$$

  • 协变量示例:年龄、体重、国家
Python 中的生存分析

加速失效时间(AFT)模型

$$\text{群体 A}: S_A(t)$$

$$\text{群体 B}: S_B(t)$$

$$S_A(t)=S_B(t*\lambda)$$

  • $S_B(t)$ 相对 $S_A(t)$ 按因子 $\lambda$ 加速或减速。

  • AFT 基于协变量建模此加速/减速关系。

  • 当协变量由 $a$ 变为 $b$,事件时间按加速失效率 $\lambda$ 加速或减速。
  • 例:$S_{dog}(t)=S_{human}(t*7)$
Python 中的生存分析

用于生存回归的数据

数据示例:mortgage_df

id property_type principal interest property_tax credit_score duration paid_off
1 house 1275 0.035 0.019 780 25 0
2 apartment 756 0.028 0.020 695 17 1
3 apartment 968 0.029 0.017 810 5 0
... ... ... ... ... ... ... ...
1000 house 1505 0.041 0.023 750 30 1
Python 中的生存分析

将 Weibull 与 AFT 结合:Weibull AFT 模型

  • DataFrame:mortgage_df
  • 协变量:
    • property_type 替换为虚拟变量:
      • house:房屋为"house"记为 1,"apartment"记为 0
    • principal
    • interest
    • property_tax
    • credit_score
  1. 导入并实例化 WeibullAFTFitter
    from lifelines import WeibullAFTFitter
    aft = WeibullAFTFitter()
    
  2. 调用 .fit() 拟合模型
    aft.fit(df=mortgage_df,
         duration_col="duration",
         event_col="paid_off")
    
Python 中的生存分析

解读模型输出

print(aft.summary)
<lifelines.WeibullAFTFitter: fitted with 1808 observations, 340 censored>
                      coef  exp(coef)  se(coef)      z       p
lambda_ house         0.04       1.04      0.01   0.99    0.32  
        principal    -0.03       0.97      0.22  -1.04    0.30  
        interest      0.11       1.11      0.15   1.96    0.05  
        property_tax  0.31       1.36      0.27   1.15    0.25  
        credit_score -0.16       0.85      0.14  -2.33    0.02  
        Intercept     3.99      54.06      0.41   9.52 <0.0005   
rho_    Intercept     0.34       1.40      0.08   3.80 <0.0005
Python 中的生存分析

WeibullAFTFitter 与自定义公式

使用 formula 定义自定义协变量:

aft.fit(df=mortgage_df,
        duration_col="duration",
        event_col="paid_off",
        formula="principal + interest * house")

类比含交互项的线性模型:

$\beta_1$principal$+\beta_2$interest$+\beta_3$house$+\beta_4$interest$\cdot$house

Python 中的生存分析

解读模型输出

print(aft.summary)
<lifelines.WeibullAFTFitter: fitted with 1808 observations, 340 censored>
                       coef  exp(coef)  se(coef)      z       p
lambda_ principal     -0.03       0.97      0.22  -1.04    0.30     
        interest       0.11       1.11      0.15   1.96    0.05  
        house          0.04       1.04      0.01   0.99    0.32  
        interest:house 0.06       1.06      0.14   0.42    0.64
        Intercept      3.99      54.06      0.41   9.52 <0.0005   
rho_    Intercept      0.34       1.40      0.08   3.80 <0.0005
Python 中的生存分析

Vamos praticar!

Python 中的生存分析

Preparing Video For Download...