Skip to content

Commit

Permalink
Merge pull request #179 from CamDavidsonPilon/fix-left-censorship
Browse files Browse the repository at this point in the history
fixing left censorship + improved test
  • Loading branch information
CamDavidsonPilon committed Aug 1, 2015
2 parents 0994494 + b401593 commit 75a051e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lifelines/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,12 @@ def _additive_estimate(events, timeline, _additive_f, _additive_var, reverse):
"""
if reverse:
events = events.sort_index(ascending=False)
at_risk = events['at_risk']
deaths = events['observed'].shift(1).fillna(0)
estimate_ = np.cumsum(_additive_f(at_risk, deaths)).ffill().sort_index()
var_ = np.cumsum(_additive_var(at_risk, deaths)).ffill().sort_index()
at_risk = events['entrance'].sum() - events['removed'].cumsum().shift(1).fillna(0)

deaths = events['observed']

estimate_ = np.cumsum(_additive_f(at_risk, deaths)).sort_index().shift(-1).fillna(0)
var_ = np.cumsum(_additive_var(at_risk, deaths)).sort_index().shift(-1).fillna(0)
else:
deaths = events['observed']
at_risk = events['at_risk']
Expand Down
8 changes: 5 additions & 3 deletions tests/test_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,14 @@ def test_passing_in_left_censorship_creates_a_cumulative_density(self, sample_li
assert not hasattr(kmf, 'survival_function_')

def test_kmf_left_censorship_stats(self):
# from http://www.public.iastate.edu/~pdixon/stat505/Chapter%2011.pdf
T = [3, 5, 5, 5, 6, 6, 10, 12]
C = [1, 0, 0, 1, 1, 1, 0, 1]
C = [1, 0, 0, 1, 1, 1, 0, 1]
kmf = KaplanMeierFitter()
kmf.fit(T, C, left_censorship=True)
assert kmf.cumulative_density_[kmf._label].ix[0] == 0.0
assert kmf.cumulative_density_[kmf._label].ix[12] == 1.0

actual = kmf.cumulative_density_[kmf._label].values
npt.assert_almost_equal(actual, np.array([0, 0.437500, 0.5833333, 0.875, 0.875, 1]))

def test_shifting_durations_doesnt_affect_survival_function_values(self):
T = np.random.exponential(10, size=100)
Expand Down

0 comments on commit 75a051e

Please sign in to comment.