Skip to content

Commit

Permalink
Have crossval tests use predict_partial_hazard
Browse files Browse the repository at this point in the history
Since other predict methods are numerically unstable atm, use
predict_partial_hazard in k_fold_crossval tests. Since this method
works, I could remove the outer loop and increase expected result
because results are consistently good now (at .93 to 0.97).

Signed-off-by: Jonas Kalderstam <jonas@kalderstam.se>
  • Loading branch information
spacecowboy committed Nov 26, 2014
1 parent 9e10813 commit 12635b7
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions lifelines/tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,33 +1043,39 @@ def test_crossval_for_cox_ph_with_normalizing_times(self):
times /= np.std(times)
data_norm['t'] = times

mean_scores = []
for repeat in range(20):
scores = k_fold_cross_validation(cf, data_norm,
duration_col='t',
event_col='E', k=3)
mean_scores.append(np.mean(scores))
scores = k_fold_cross_validation(cf, data_norm,
duration_col='t',
event_col='E', k=3,
predictor='predict_partial_hazard')

expected = 0.85
mean_score = np.mean(scores)
# partial_hazard will get inverse concordance
if mean_score < 0.5:
mean_score = 1 - mean_score

expected = 0.9
msg = "Expected min-mean c-index {:.2f} < {:.2f}"
self.assertTrue(np.mean(mean_scores) > expected,
msg.format(expected, np.mean(mean_scores)))
self.assertTrue(mean_score > expected,
msg.format(expected, mean_score))

def test_crossval_for_cox_ph(self):
cf = CoxPHFitter()

for data_pred in [data_pred1, data_pred2]:
mean_scores = []
for repeat in range(20):
scores = k_fold_cross_validation(cf, data_pred,
duration_col='t',
event_col='E', k=3)
mean_scores.append(np.mean(scores))
scores = k_fold_cross_validation(cf, data_pred,
duration_col='t',
event_col='E', k=3,
predictor='predict_partial_hazard')

expected = 0.85
mean_score = np.mean(scores)
# partial_hazard will get inverse concordance
if mean_score < 0.5:
mean_score = 1 - mean_score

This comment has been minimized.

Copy link
@CamDavidsonPilon

CamDavidsonPilon Nov 27, 2014

Owner

This is because partial hazards are negative I have no idea?

This comment has been minimized.

Copy link
@CamDavidsonPilon

CamDavidsonPilon Nov 27, 2014

Owner

nvm got it - larger partial hazard => shorter lifespan duh

This comment has been minimized.

Copy link
@CamDavidsonPilon

CamDavidsonPilon Nov 27, 2014

Owner

Is this if statement necessary - it feels kinda dirty

This comment has been minimized.

Copy link
@spacecowboy

spacecowboy Nov 27, 2014

Author Collaborator

Not strictly. Here it will always be less than 0.5, so could also do directly:

mean_score = 1 - np.mean(scores)

expected = 0.9
msg = "Expected min-mean c-index {:.2f} < {:.2f}"
self.assertTrue(np.mean(mean_scores) > expected,
msg.format(expected, np.mean(mean_scores)))
self.assertTrue(mean_score > expected,
msg.format(expected, mean_score))

def test_crossval_for_cox_ph_normalized(self):
cf = CoxPHFitter()
Expand All @@ -1093,16 +1099,19 @@ def test_crossval_for_cox_ph_normalized(self):
x2 /= np.std(x2)
data_norm['x2'] = x2

mean_scores = []
for repeat in range(20):
scores = k_fold_cross_validation(cf, data_norm,
duration_col='t',
event_col='E', k=3)
mean_scores.append(np.mean(scores))
expected = 0.85
scores = k_fold_cross_validation(cf, data_norm,
duration_col='t',
event_col='E', k=3,
predictor='predict_partial_hazard')

mean_score = np.mean(scores)
# partial_hazard will get inverse concordance
if mean_score < 0.5:
mean_score = 1 - mean_score
expected = 0.9
msg = "Expected min-mean c-index {:.2f} < {:.2f}"
self.assertTrue(np.mean(mean_scores) > expected,
msg.format(expected, np.mean(mean_scores)))
self.assertTrue(mean_score > expected,
msg.format(expected, mean_score))

def test_output_against_R(self):
# from http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-cox-regression.pdf
Expand Down

0 comments on commit 12635b7

Please sign in to comment.