利用ovarian数据集进行生存分析

数据展示

在这里插入图片描述

导包:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from lifelines import KaplanMeierFitter
from lifelines import CoxPHFitter

读入数据:

df = pd.read_csv(r"C:\Users\guo\Desktop\ovarian(1).csv")
"""
futime:生存或删失的时间
fustat:删失状态(确定参与者生存时间是否发生缺失)
age:年龄
resid_ds:评估肿瘤的消退情况,(1=否,2=是)
rx:接受两种治疗方案中的一种
ecog_ps:依据ECOG评估的患者表现
"""

生存率估计:Kaplan-Meier估计

df.sort_values('futime', inplace=True)  # 按照futime进行排序
df = df.reset_index(drop=True)  # 更新索引

"""Kaplan-Meier曲线"""
kmf = KaplanMeierFitter()  # 创建KaplanMeierFitter对象
kmf.fit(df.futime, df.fustat)  # 拟合数据
kmf.plot_survival_function()  # 画图,也可以使用kmf.plot()
plt.show()

在这里插入图片描述

df_rx1 = df[df['rx']==1]
df_rx2 = df[df['rx']==2]
df_rx1 = df_rx1.reset_index(drop=True) #更新索引
df_rx2 = df_rx2.reset_index(drop=True) #更新索引

kmf_rx1 = KaplanMeierFitter()
kmf_rx1.fit(df_rx1.futime,df_rx1.fustat)
kmf_rx2 = KaplanMeierFitter()
kmf_rx2.fit(df_rx2.futime,df_rx2.fustat)

kmf_rx1.plot(label='rx=1')
kmf_rx2.plot(label='rx=2')
plt.show()

在这里插入图片描述

Cox回归分析

"""Cox回归"""
cph = CoxPHFitter()
cph.fit(df,duration_col='futime',event_col='fustat')

cph.print_summary()  # 打印详细信息

cph.plot()  # 画图
plt.show()

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Re:从零开始的代码生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值