def test_smoothing_hazard_with_spike_at_time_0(self): T = np.random.binomial(20, 0.7, size=300) T[np.random.binomial(1, 0.3, size=300).astype(bool)] = 0 naf = NelsonAalenFitter() naf.fit(T) df = naf.smoothed_hazard_(bandwidth=0.1) assert df.iloc[0].values[0] > df.iloc[1].values[0]
def test_smoothing_hazard_nontied(self): T = np.random.exponential(20, size=300) ** 2 C = np.random.binomial(1, 0.8, size=300) naf = NelsonAalenFitter() naf.fit(T, C) naf.smoothed_hazard_(1.) naf.fit(T) naf.smoothed_hazard_(1.)
def test_naf_plot_cumulative_hazard_bandwidth_2(self): data1 = np.random.exponential(5, size=(2000, 1)) naf = NelsonAalenFitter() naf.fit(data1) naf.plot_hazard(bandwidth=1., ix=slice(0, 7.)) self.plt.title('test_naf_plot_cumulative_hazard_bandwidth_2') self.plt.show() return
def test_naf_plotting_slice(self, block): data1 = np.random.exponential(5, size=(200, 1)) data2 = np.random.exponential(1, size=(200, 1)) naf = NelsonAalenFitter() naf.fit(data1) ax = naf.plot(ix=slice(0, None)) naf.fit(data2) naf.plot(ax=ax, ci_force_lines=True, iloc=slice(100, 180)) self.plt.title('test_naf_plotting_slice') self.plt.show(block=block) return
def test_naf_plotting_with_custom_colours(self, block): data1 = np.random.exponential(5, size=(200, 1)) data2 = np.random.exponential(1, size=(500)) naf = NelsonAalenFitter() naf.fit(data1) ax = naf.plot(color="r") naf.fit(data2) naf.plot(ax=ax, c="k") self.plt.title('test_naf_plotting_with_custom_coloirs') self.plt.show(block=block) return
def test_naf_plot_cumulative_hazard(self, block): data1 = np.random.exponential(5, size=(200, 1)) naf = NelsonAalenFitter() naf.fit(data1) ax = naf.plot() naf.plot_cumulative_hazard(ax=ax, ci_force_lines=True) self.plt.title("I should have plotted the same thing, but different styles + color!") self.plt.show(block=block) return
def test_naf_plot_cumulative_hazard_bandwidth_2(self, block): data1 = np.random.exponential(5, size=(2000, 1)) naf = NelsonAalenFitter() naf.fit(data1) naf.plot_hazard(bandwidth=1.0, loc=slice(0, 7.0)) self.plt.title("test_naf_plot_cumulative_hazard_bandwidth_2") self.plt.show(block=block) return
def test_naf_plot_cumulative_hazard_bandwith_1(self, block): data1 = np.random.exponential(5, size=(2000, 1)) ** 2 naf = NelsonAalenFitter() naf.fit(data1) naf.plot_hazard(bandwidth=5., iloc=slice(0, 1700)) self.plt.title('test_naf_plot_cumulative_hazard_bandwith_1') self.plt.show(block=block) return
def test_iloc_slicing(self, waltons_dataset): naf = NelsonAalenFitter().fit(waltons_dataset['T']) assert naf.cumulative_hazard_.iloc[0:10].shape[0] == 10 assert naf.cumulative_hazard_.iloc[0:-1].shape[0] == 32
def test_smoothing_hazard_ties_all_events_observed(self): T = np.random.binomial(20, 0.7, size=300) naf = NelsonAalenFitter() naf.fit(T) naf.smoothed_hazard_(1.)
def test_smoothing_hazard_ties(self): T = np.random.binomial(20, 0.7, size=300) C = np.random.binomial(1, 0.8, size=300) naf = NelsonAalenFitter() naf.fit(T, C) naf.smoothed_hazard_(1.)
def test_exponential_data_sets_fit(): N = 20000 T, C = exponential_survival_data(N, 0.2, scale=10) naf = NelsonAalenFitter() naf.fit(T, C).plot() plt.title("Should be a linear with slope = 0.1")
def test_censor_nelson_aalen(self, sample_lifetimes): T, C = sample_lifetimes naf = NelsonAalenFitter(nelson_aalen_smoothing=False) naf.fit(T, C) npt.assert_almost_equal(naf.cumulative_hazard_.values, self.nelson_aalen(T, C))
# In[23]: get_ipython().magic(u'R p <- plot.ly("https://plot.ly/~rmdk/185/cumulativehazard-vs-time/")') # pass object to python kernel get_ipython().magic(u'R -o p') # Render HTML HTML(p[0]) # ### Using Python # In[26]: from lifelines.estimation import NelsonAalenFitter naf = NelsonAalenFitter() naf.fit(T, event_observed=C) naf.plot(title='Nelson-Aalen Estimate') # In[27]: naf.plot(ci_force_lines=True, title='Nelson-Aalen Estimate') py_p = plt.gcf() pyplot(py_p, legend=False)