示例#1
0
 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
plt.legend()

#Hazard function:
from lifelines import NelsonAalenFitter

#Create an object of NelsonAalenFitter:
naf = NelsonAalenFitter()

#Fit our data into the object:
naf.fit(data["time"], event_observed=data["dead"])

#Print the cumulative hazard:
naf.cumulative_hazard_

#Plot the cumulative hazard grpah:
naf.plot_cumulative_hazard()
plt.title("Cumulative Probability for Event of Interest")
plt.xlabel("Number of days")
plt.ylabel("Cumulative Probability of person's death")

#We can predict the value at a certain point :
print("Time = 500 days: ",naf.predict(500))
print("Time = 1022 days: ",naf.predict(1022))

#Cumulative hazard with confidence interval:
naf.confidence_interval_

#Plot cumulative hazard with confidence interval:
confidence_interval = naf.confidence_interval_
plt.plot(confidence_interval["NA_estimate_lower_0.95"],label="Lower")
plt.plot(confidence_interval["NA_estimate_upper_0.95"],label="Upper")
示例#3
0
from lifelines import NelsonAalenFitter

#Fitting the data into objects:
naf_m = NelsonAalenFitter()
naf_f = NelsonAalenFitter()
naf_m.fit(Male["time"], event_observed=Male["dead"])
naf_f.fit(Female["time"], event_observed=Female["dead"])

#Cumulative hazard for male group:
naf_m.cumulative_hazard_

#Cumulative hazard for female group:
naf_f.cumulative_hazard_

#Plot the graph for cumulative hazard:
naf_m.plot_cumulative_hazard(label="Male")
naf_f.plot_cumulative_hazard(label="Female")
plt.title("Cumulative Hazard Plot")
plt.xlabel("Number of Days")
plt.ylabel("Cumulative Hazard")

#Conditional median time to event of interest:
kmf_m.conditional_time_to_event_

#Conditional median time left for event for male group:
median_time_to_event = kmf_m.conditional_time_to_event_
plt.plot(median_time_to_event, label="Median Time left")
plt.title("Medain time to event")
plt.xlabel("Total days")
plt.ylabel("Conditional median time to event")
plt.legend()