数据展示
导包:
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 )
df = df. reset_index( drop= True )
"""Kaplan-Meier曲线"""
kmf = KaplanMeierFitter( )
kmf. fit( df. futime, df. fustat)
kmf. plot_survival_function( )
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( )