-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcox_hazard.py
90 lines (74 loc) · 3.83 KB
/
cox_hazard.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/local/bin/python3.9
import pandas as pd
from lifelines import CoxPHFitter, KaplanMeierFitter
import numpy as np
import matplotlib.pyplot as plt
# Assuming you have your dataset in a DataFrame called 'data'
# 'Time' represents the time to event or censoring
# 'Event' represents whether the event occurred (1) or not (0)
# 'Treatment' represents the treatment group (0 for Medication A, 1 for Medication B)
# 'Covariates' represent other variables that may influence the hazard
# Generating sample data
np.random.seed(0)
# Sample data for Medication A (Treatment = 0)
data_med_a = pd.DataFrame({
'Time': np.random.randint(10, 100, size=100),
'Event': np.random.choice([0, 1], size=100, p=[0.8, 0.2]), # Higher event probability for Medication A
'Treatment': np.zeros(100),
'Age': np.random.randint(50, 80, size=100),
'Smoking': np.random.choice([0, 1], size=100),
'BloodPressure': np.random.randint(120, 180, size=100),
'Cholesterol': np.random.randint(180, 250, size=100)
})
# Sample data for Medication B (Treatment = 1)
data_med_b = pd.DataFrame({
'Time': np.random.randint(10, 100, size=100),
'Event': np.random.choice([0, 1], size=100),
'Treatment': np.ones(100),
'Age': np.random.randint(50, 80, size=100),
'Smoking': np.random.choice([0, 1], size=100),
'BloodPressure': np.random.randint(120, 180, size=100),
'Cholesterol': np.random.randint(180, 250, size=100)
})
# Concatenate the two datasets
data = pd.concat([data_med_a, data_med_b], ignore_index=True)
# Display the DataFrame
print(data)
# Fit Cox proportional hazards model
cph = CoxPHFitter()
cph.fit(data, duration_col='Time', event_col='Event', formula='Treatment + Age + Smoking + BloodPressure + Cholesterol')
# Print summary of the model
print(cph.summary)
hr=round(np.exp(cph.summary.loc['Treatment', 'coef']), 4)
hr_p = (hr - 1) * 100
if cph.summary.loc['Treatment', 'coef'] > 0 and cph.summary.loc['Treatment', 'p'] < 0.05:
print("The coefficient is positive")
print("The hazard ratio for treatment (Medication A vs. Medication B) is {:.2f}, which is significant (p < 0.05).".format(np.exp(cph.summary.loc['Treatment', 'coef'])))
print("This indicates that Medication A has a significantly increased hazard risk of experiencing the event compared to Medication B")
print("The event is {:.1f}% more likely to occur in Medication A.".format(hr_p))
elif cph.summary.loc['Treatment', 'coef'] < 0 and cph.summary.loc['Treatment', 'p'] < 0.05:
print("The coefficient is negative")
print("The hazard ratio for treatment (Medication A vs. Medication B) is {:.2f}, which is significant (p < 0.05).".format(np.exp(cph.summary.loc['Treatment', 'coef'])))
print("This indicates that Medication B has a significantly increased hazard risk of experiencing the event compared to Medication A")
print("The event is {:.1f}% more likely to occur in Medication B.".format(hr_p))
elif cph.summary.loc['Treatment', 'p'] >= 0.05:
print("The hazard ratio for treatment (Medication A vs. Medication B) is {:.2f}, which is not significant (p > 0.05).".format(np.exp(cph.summary.loc['Treatment', 'coef'])))
# Get hazard ratios and confidence intervals
print(cph.summary['coef'])
print(cph.confidence_intervals_)
# Plot the coefficients of the Cox model
cph.plot()
plt.title('Cox Proportional Hazards Model')
plt.show()
# Plot survival curves using Kaplan-Meier estimator for different treatments
kmf = KaplanMeierFitter()
# Fit the Kaplan-Meier model for Medication A
kmf.fit(data_med_a['Time'], event_observed=data_med_a['Event'], label='Medication A')
ax = kmf.plot_survival_function()
# Fit the Kaplan-Meier model for Medication B
kmf.fit(data_med_b['Time'], event_observed=data_med_b['Event'], label='Medication B')
kmf.plot_survival_function(ax=ax)
plt.title('Survival Function by Treatment')
plt.xlabel('Time')
plt.ylabel('Survival Probability')
plt.show()