Exemple #1
0
def cdf_plot(model, timeline=None, ax=None, **plot_kwargs):
    """


    """
    from lifelines import KaplanMeierFitter

    if ax is None:
        ax = plt.gca()

    if timeline is None:
        timeline = model.timeline

    COL_EMP = "empirical CDF"

    if CensoringType.is_left_censoring(model):
        empirical_kmf = KaplanMeierFitter().fit_left_censoring(
            model.durations, model.event_observed, label=COL_EMP, timeline=timeline
        )
    elif CensoringType.is_right_censoring(model):
        empirical_kmf = KaplanMeierFitter().fit_right_censoring(
            model.durations, model.event_observed, label=COL_EMP, timeline=timeline
        )
    elif CensoringType.is_interval_censoring(model):
        raise NotImplementedError("lifelines does not have a non-parametric interval model yet.")

    empirical_kmf.plot_cumulative_density(ax=ax, **plot_kwargs)

    dist = get_distribution_name_of_lifelines_model(model)
    dist_object = create_scipy_stats_model_from_lifelines_model(model)
    ax.plot(timeline, dist_object.cdf(timeline), label="fitted %s" % dist, **plot_kwargs)
    ax.legend()
    return ax
Exemple #2
0
def cdf_plot(model, timeline=None, **plot_kwargs):
    from lifelines import KaplanMeierFitter

    set_kwargs_ax(plot_kwargs)
    ax = plot_kwargs.pop("ax")

    if timeline is None:
        timeline = model.timeline

    kmf = KaplanMeierFitter().fit(
        model.durations,
        model.event_observed,
        left_censorship=model.left_censorship,
        label="empirical CDF",
        timeline=timeline,
    )
    if model.left_censorship:
        kmf.plot_cumulative_density(ax=ax, **plot_kwargs)
    else:
        (1 - kmf.survival_function_).plot(ax=ax, **plot_kwargs)

    dist = get_distribution_name_of_lifelines_model(model)
    dist_object = create_scipy_stats_model_from_lifelines_model(model)
    ax.plot(timeline,
            dist_object.cdf(timeline),
            label="fitted %s" % dist,
            **plot_kwargs)
    ax.legend()
    return ax
Exemple #3
0
def cdf_plot(model, timeline=None, **plot_kwargs):
    from lifelines import KaplanMeierFitter

    set_kwargs_ax(plot_kwargs)
    ax = plot_kwargs.pop("ax")

    if timeline is None:
        timeline = model.timeline

    COL_EMP = "empirical quantiles"

    if CensoringType.is_left_censoring(model):
        kmf = KaplanMeierFitter().fit_left_censoring(model.durations,
                                                     model.event_observed,
                                                     label=COL_EMP,
                                                     timeline=timeline)
    elif CensoringType.is_right_censoring(model):
        kmf = KaplanMeierFitter().fit_right_censoring(model.durations,
                                                      model.event_observed,
                                                      label=COL_EMP,
                                                      timeline=timeline)
    elif CensoringType.is_interval_censoring(model):
        raise NotImplementedError()

    kmf.plot_cumulative_density(ax=ax, **plot_kwargs)

    dist = get_distribution_name_of_lifelines_model(model)
    dist_object = create_scipy_stats_model_from_lifelines_model(model)
    ax.plot(timeline,
            dist_object.cdf(timeline),
            label="fitted %s" % dist,
            **plot_kwargs)
    ax.legend()
    return ax
Exemple #4
0
def cdf_plot(model, timeline=None, ax=None, **plot_kwargs):
    """
    This plot compares the empirical CDF (derived by KaplanMeier) vs the model CDF.

    Parameters
    ------------
    model: lifelines univariate model
    timeline: iterable
    ax: matplotlib axis

    """
    from lifelines import KaplanMeierFitter
    from matplotlib import pyplot as plt

    if ax is None:
        ax = plt.gca()

    if timeline is None:
        timeline = model.timeline

    COL_EMP = "empirical CDF"

    if CensoringType.is_left_censoring(model):
        empirical_kmf = KaplanMeierFitter().fit_left_censoring(
            model.durations,
            model.event_observed,
            label=COL_EMP,
            timeline=timeline,
            weights=model.weights,
            entry=model.entry)
    elif CensoringType.is_right_censoring(model):
        empirical_kmf = KaplanMeierFitter().fit_right_censoring(
            model.durations,
            model.event_observed,
            label=COL_EMP,
            timeline=timeline,
            weights=model.weights,
            entry=model.entry)
    elif CensoringType.is_interval_censoring(model):
        empirical_kmf = KaplanMeierFitter().fit_interval_censoring(
            model.lower_bound,
            model.upper_bound,
            label=COL_EMP,
            timeline=timeline,
            weights=model.weights,
            entry=model.entry)

    empirical_kmf.plot_cumulative_density(ax=ax, **plot_kwargs)

    dist = get_distribution_name_of_lifelines_model(model)
    dist_object = create_scipy_stats_model_from_lifelines_model(model)
    ax.plot(timeline,
            dist_object.cdf(timeline),
            label="fitted %s" % dist,
            **plot_kwargs)
    ax.legend()
    return ax
    ax1.step(kmf.cumulative_density_.index.values,
             kmf.cumulative_density_.values,
             color=lab_colors[i],
             lw=2)
kmf.fit(training_time['sessions'].values,
        event_observed=training_time['trained'])
ax1.step(kmf.cumulative_density_.index.values,
         kmf.cumulative_density_.values,
         color='black',
         lw=2)
ax1.set(ylabel='Cumulative probability of\nreaching trained criterion',
        xlabel='Training day',
        xlim=[0, 60],
        ylim=[0, 1.02])

kmf.fit(training_time['sessions'].values,
        event_observed=training_time['trained'])
kmf.plot_cumulative_density(ax=ax2)
ax2.set(ylabel='Cumulative probability of\nreaching trained criterion',
        xlabel='Training day',
        title='All labs',
        xlim=[0, 60],
        ylim=[0, 1.02])
ax2.get_legend().set_visible(False)

sns.despine(trim=True, offset=5)
plt.tight_layout(pad=2)
seaborn_style()
plt.savefig(join(fig_path, 'figure2c_probability_trained.pdf'), dpi=300)
plt.savefig(join(fig_path, 'figure2c_probability_trained.png'), dpi=300)
#Plot survival function with confidence interval:
confidence_surv_func = kmf.confidence_interval_survival_function_
plt.plot(confidence_surv_func["KM_estimate_lower_0.95"],label="Lower")
plt.plot(confidence_surv_func["KM_estimate_upper_0.95"],label="Upper")
plt.title("Survival Function With Confidence Interval")
plt.xlabel("Number of days")
plt.ylabel("Survival Probability")
plt.legend()

#Probabaility of a subject dying:
#p(1022) = p(0) +......+p(1022)
kmf.cumulative_density_

#Plot the cumulative density graph:
kmf.plot_cumulative_density()
plt.title("Cumulative Density Plot")
plt.xlabel("Number of days")
plt.ylabel("Probability of person's death")

#Cumulative density with confidence interval:
kmf.confidence_interval_cumulative_density_

#Plot cumulative density with confidence interval:
confidence_cumulative_density = kmf.confidence_interval_cumulative_density_
plt.plot(kmf.confidence_interval_cumulative_density_["KM_estimate_lower_0.95"],label="Lower")
plt.plot(kmf.confidence_interval_cumulative_density_["KM_estimate_upper_0.95"],label="Upper")
plt.title("Cumulative Density With Confidence Interval")
plt.xlabel("Number of days")
plt.ylabel("Cumulative Density")
plt.legend()
Exemple #7
0
#Plot the survival_function data:
kmf_m.plot()
kmf_f.plot()
plt.xlabel("Days Passed")
plt.ylabel("Survival Probability")
plt.title("KMF")

#Cumulative density for male group:
kmf_m.cumulative_density_

#Cumulative density for female group:
kmf_f.cumulative_density_

#PLot the graph for cumulative density for both groups:
kmf_m.plot_cumulative_density()
kmf_f.plot_cumulative_density()
plt.title("Cumulative Density")
plt.xlabel("Number of days")
plt.ylabel("Probability")

#Hazard Function:
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: