-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cox Proportional Model and Random Forest.py
339 lines (249 loc) · 11.5 KB
/
Cox Proportional Model and Random Forest.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Cox Proportional Hazards and Random Survival Forests
# Goal:
# - Cox Proportional Hazards
# - Data Preprocessing for Cox Models.
# - Random Survival Forests
# - Permutation Methods for Interpretation.
# Import Packages
import sklearn # most popular machine learning libraries
import numpy as np # fundamental package for scientific computing in python
import pandas as pd # to manipulate the data
import matplotlib.pyplot as plt # plotting library
from lifelines import CoxPHFitter # open-source survival analysis library
from lifelines.utils import concordance_index as cindex
from sklearn.model_selection import train_test_split
from util import load_data
#
# Run cell to load the data.
df = load_data()
# Be familiarized with the data and the shape of it.
print(df.shape)
# df.head() only outputs the top few rows
df.head()
# Take a minute to examine particular cases.
i = 20
df.iloc[i, :]
# Now, split your dataset into train, validation and test set using 60/20/20 split.
np.random.seed(0)
df_dev, df_test = train_test_split(df, test_size = 0.2)
df_train, df_val = train_test_split(df_dev, test_size = 0.25)
print("Total number of patients:", df.shape[0])
print("Total number of patients in training set:", df_train.shape[0])
print("Total number of patients in validation set:", df_val.shape[0])
print("Total number of patients in test set:", df_test.shape[0])
# Normalize the continuous covariates by using statistics from the train data to make sure they're on the same scale.
continuous_columns = ['age', 'bili', 'chol', 'albumin', 'copper', 'alk.phos', 'ast', 'trig', 'platelet', 'protime']
mean = df_train.loc[:, continuous_columns].mean()
std = df_train.loc[:, continuous_columns].std()
df_train.loc[:, continuous_columns] = (df_train.loc[:, continuous_columns] - mean) / std
df_val.loc[:, continuous_columns] = (df_val.loc[:, continuous_columns] - mean) / std
df_test.loc[:, continuous_columns] = (df_test.loc[:, continuous_columns] - mean) / std
# Check the summary statistics on our training dataset to make sure it's standardized.
df_train.loc[:, continuous_columns].describe()
# Goal is to build a risk score using the survival data obtained and begin by fitting a Cox Proportional Hazards model to the data.
# Implement the `to_one_hot(...)` function.
def to_one_hot(dataframe, columns):
'''
Convert columns in dataframe to one-hot encoding.
Args:
dataframe (dataframe): pandas dataframe containing covariates
columns (list of strings): list categorical column names to one hot encode
Returns:
one_hot_df (dataframe): dataframe with categorical columns encoded
as binary variables
'''
one_hot_df = pd.get_dummies(dataframe, columns = columns, drop_first = True, dtype = np.float64)
return one_hot_df
# Now, use the function coded to transform the training, validation, and test sets.
# List of categorical columns
to_encode = ['edema', 'stage']
one_hot_train = to_one_hot(df_train, to_encode)
one_hot_val = to_one_hot(df_val, to_encode)
one_hot_test = to_one_hot(df_test, to_encode)
print(one_hot_val.columns.tolist())
print(f"There are {len(one_hot_val.columns)} columns")
# Take a peek at one of the transformed data sets for new features.
print(one_hot_train.shape)
one_hot_train.head()
# Run the following cell to fit your Cox Proportional Hazards model using the `lifelines` package.
cph = CoxPHFitter()
cph.fit(one_hot_train, duration_col = 'time', event_col = 'status', step_size=0.1)
# Use `cph.print_summary()` to view the coefficients associated with each covariate as well as confidence intervals.
cph.print_summary()
# Run the next cell to plot survival curves using the `plot_covariate_groups()` function.
cph.plot_covariate_groups('trt', values=[0, 1]); # Can compare the predicted survival curves for treatment variables.
# Write a function to compute the hazard ratio between two individuals given the cox model's coefficients
def hazard_ratio(case_1, case_2, cox_params):
'''
Return the hazard ratio of case_1 : case_2 using
the coefficients of the cox model.
Args:
case_1 (np.array): (1 x d) array of covariates
case_2 (np.array): (1 x d) array of covariates
model (np.array): (1 x d) array of cox model coefficients
Returns:
hazard_ratio (float): hazard ratio of case_1 : case_2
'''
hr = np.exp(cox_params.dot((case_1 - case_2).T))
return hr
# Now, evaluate it on the following pair of indivduals: `i = 1` and `j = 5`
i = 1
case_1 = one_hot_train.iloc[i, :].drop(['time', 'status'])
j = 5
case_2 = one_hot_train.iloc[j, :].drop(['time', 'status'])
print(hazard_ratio(case_1.values, case_2.values, cph.params_.values))
# Inspect different pairs, and figure out which patient is more at risk.
i = 4
case_1 = one_hot_train.iloc[i, :].drop(['time', 'status'])
j = 7
case_2 = one_hot_train.iloc[j, :].drop(['time', 'status'])
print("Case 1\n\n", case_1, "\n")
print("Case 2\n\n", case_2, "\n")
print("Hazard Ratio:", hazard_ratio(case_1.values, case_2.values, cph.params_.values))
# Fill in the function below to compute Harrel's C-index.
def harrell_c(y_true, scores, event):
'''
Compute Harrel C-index given true event/censoring times,
model output, and event indicators.
Args:
y_true (array): array of true event times
scores (array): model risk scores
event (array): indicator, 1 if event occurred at that index, 0 for censorship
Returns:
result (float): C-index metric
'''
n = len(y_true)
assert (len(scores) == n and len(event) == n)
concordant = 0.0
permissible = 0.0
ties = 0.0
result = 0.0
# use double for loop to go through cases
for i in range(n):
# set lower bound on j to avoid double counting
for j in range(i+1, n):
# check if at most one is censored
if event[i] == 1 or event[j] == 1:
# check if neither are censored
if event[i] == 1 or event[j] == 1:
permissible += 1.0
# check if scores are tied
if scores[i] == scores[j]:
ties += 1.0:
# check for concordant
elif y_true[i] < y_true[j] and scores[i] > scores[j]:
concordant += 0
elif y_true[i] > y_true[j] and scores[i] < scores[j]:
concordant += 1.0
# check if one is censored
elif event[i] != event[j]:
# get censored index
censored = j
uncensored = i
if event[i] == 0:
censored = i
uncensored = j
# check if permissible
# Note: in this case, we are assuming that censored at a time
# means that you did NOT die at that time. That is, if you
# live until time 30 and have event = 0, then you lived THROUGH
# time 30.
if y_true[uncensored] <= y_true[censored]:
permissible += 1.0
# check if scores are tied
if scores[uncensored] == scores[censored]:
# update ties
ties += 1.0
# check if scores are concordant
if scores[uncensored] > scores[censored]:
concordant += 1.0
# set result to c-index computed from number of concordant pairs,
# number of ties, and number of permissible pairs (REPLACE 0 with your code)
result = (concordant + 0.5*ties) / permissible
return result
# Test the function on the following test cases:
y_true = [30, 12, 84, 9]
# Case 1
event = [1, 1, 1, 1]
scores = [0.5, 0.9, 0.1, 1.0]
print("Case 1")
print("Expected: 1.0, Output: {}".format(harrell_c(y_true, scores, event)))
# Case 2
scores = [0.9, 0.5, 1.0, 0.1]
print("\nCase 2")
print("Expected: 0.0, Output: {}".format(harrell_c(y_true, scores, event)))
# Case 3
event = [1, 0, 1, 1]
scores = [0.5, 0.9, 0.1, 1.0]
print("\nCase 3")
print("Expected: 1.0, Output: {}".format(harrell_c(y_true, scores, event)))
# Case 4
y_true = [30, 30, 20, 20]
event = [1, 0, 1, 0]
scores = [10, 5, 15, 20]
print("\nCase 4")
print("Expected: 0.75, Output: {}".format(harrell_c(y_true, scores, event)))
# Case 5
y_true = list(reversed([30, 30, 30, 20, 20]))
event = [0, 1, 0, 1, 0]
scores = list(reversed([15, 10, 5, 15, 20]))
print("\nCase 5")
print("Expected: 0.583, Output: {}".format(harrell_c(y_true, scores, event)))
# Case 6
y_true = [10,10]
event = [0,1]
scores = [4,5]
print("\nCase 6")
print(f"Expected: 1.0 , Output:{harrell_c(y_true, scores, event):.4f}")
# Now use the Harrell's C-index function to evaluate the cox model on our data sets.
# Train
scores = cph.predict_partial_hazard(one_hot_train)
cox_train_scores = harrell_c(one_hot_train['time'].values, scores.values, one_hot_train['status'].values)
# Validation
scores = cph.predict_partial_hazard(one_hot_val)
cox_val_scores = harrell_c(one_hot_val['time'].values, scores.values, one_hot_val['status'].values)
# Test
scores = cph.predict_partial_hazard(one_hot_test)
cox_test_scores = harrell_c(one_hot_test['time'].values, scores.values, one_hot_test['status'].values)
print("Train:", cox_train_scores)
print("Val:", cox_val_scores)
print("Test:", cox_test_scores)
# Use a Random Survival Forest by using the `RandomForestSRC` package in R.
# Run the following cell to import the necessary requirements.
get_ipython().run_line_magic('load_ext', 'rpy2.ipython')
get_ipython().run_line_magic('R', 'require(ggplot2)')
from rpy2.robjects.packages import importr
# import R's "base" package
base = importr('base')
# import R's "utils" package
utils = importr('utils')
# import rpy2's package module
import rpy2.robjects.packages as rpackages
forest = rpackages.importr('randomForestSRC', lib_loc='R')
from rpy2 import robjects as ro
R = ro.r
from rpy2.robjects import pandas2ri
pandas2ri.activate()
#
# Run the code cell below to build the forest.
model = forest.rfsrc(ro.Formula('Surv(time, status) ~ .'), data=df_train, ntree=300, nodedepth=5, seed=-1)
print(model)
# Finally, evaluate on the validation and test sets, and compare it with the Cox model.
result = R.predict(model, newdata=df_val)
scores = np.array(result.rx('predicted')[0])
print("Cox Model Validation Score:", cox_val_scores)
print("Survival Forest Validation Score:", harrell_c(df_val['time'].values, scores, df_val['status'].values))
result = R.predict(model, newdata=df_test)
scores = np.array(result.rx('predicted')[0])
print("Cox Model Test Score:", cox_test_scores)
print("Survival Forest Validation Score:", harrell_c(df_test['time'].values, scores, df_test['status'].values))
# The random forest model should be outperforming the Cox model slightly.
# Note: random surival forests come with their own built in variable importance feature (VIMP).
# The higher absolute value of the VIMP, then the variable generally has a larger effect on the model outcome.
# Run the next cell to compute and plot VIMP for the random survival forest.
vimps = np.array(forest.vimp(model).rx('importance')[0])
y = np.arange(len(vimps))
plt.barh(y, np.abs(vimps))
plt.yticks(y, df_train.drop(['time', 'status'], axis=1).columns)
plt.title("VIMP (absolute value)")
plt.show()