-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathsurvival_analysis_3.py
49 lines (37 loc) · 1.16 KB
/
survival_analysis_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Import required libraries:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lifelines import KaplanMeierFitter
from lifelines import CoxPHFitter
#Read the data file:
data = pd.read_csv("lung.csv")
data = data.drop(["Unnamed: 0"],axis=1)
data.head()
#Columns of dataset:
data.columns
#Drop rows with null values:
data= data.dropna(subset=['inst', 'time', 'status', 'age', 'sex','ph.ecog','ph.karno', 'pat.karno', 'meal.cal', 'wt.loss'])
data.head()
#Create an object:
kmf = KaplanMeierFitter()
#Organize the data:
data.loc[data.status == 1, 'dead'] = 0
data.loc[data.status == 2, 'dead'] = 1
data.head()
#Fit data into our object:
kmf.fit(durations = data["time"], event_observed = data["dead"])
#Get the event table:
kmf.event_table
#Get required columns from the data:
data = data[[ 'time', 'age', 'sex', 'ph.ecog','ph.karno','pat.karno', 'meal.cal', 'wt.loss', 'dead']]
#Get the summary using CoxPHFitter:
cph = CoxPHFitter()
cph.fit(data,"time",event_col="dead")
cph.print_summary()
#Plot the result on graph:
cph.plot()
data.iloc[10:15,:]
#Plotting the data:
d_data = data.iloc[10:15,:]
cph.predict_survival_function(d_data).plot()