Exemplo n.º 1
0
 def test_qq_plot_left_censoring2(self, block):
     df = load_lcd()
     fig, axes = self.plt.subplots(2, 2, figsize=(9, 5))
     axes = axes.reshape(4)
     for i, model in enumerate([WeibullFitter(), LogNormalFitter(), LogLogisticFitter(), ExponentialFitter()]):
         model.fit_left_censoring(df["T"], df["E"])
         ax = qq_plot(model, ax=axes[i])
         assert ax is not None
     self.plt.suptitle("test_qq_plot_left_censoring2")
     self.plt.show(block=block)
Exemplo n.º 2
0
    def test_qq_plot_with_weights_and_entry(self, block):
        from lifelines.utils import survival_events_from_table

        df = pd.DataFrame(index=[60, 171, 263, 427, 505, 639])
        df["death"] = [1, 1, 1, 0, 1, 0]
        df["censored"] = [0, 0, 0, 3, 0, 330]
        T, E, W = survival_events_from_table(df, observed_deaths_col="death", censored_col="censored")
        wf = WeibullFitter().fit(T, E, weights=W, entry=0.0001 * np.ones_like(T))
        ax = qq_plot(wf)
        self.plt.suptitle("test_qq_plot_with_weights_and_entry")
        self.plt.show(block=block)
Exemplo n.º 3
0
    def test_qq_plot_right_censoring_with_known_distribution(self, block):
        N = 3000
        T_actual = scipy.stats.fisk(8, 0, 1).rvs(N)
        C = scipy.stats.fisk(8, 0, 1).rvs(N)
        E = T_actual < C
        T = np.minimum(T_actual, C)

        fig, axes = self.plt.subplots(2, 2, figsize=(9, 5))
        axes = axes.reshape(4)
        for i, model in enumerate([WeibullFitter(), LogNormalFitter(), LogLogisticFitter(), ExponentialFitter()]):
            model.fit(T, E)
            ax = qq_plot(model, ax=axes[i])
            assert ax is not None
        self.plt.suptitle("test_qq_plot_right_censoring_with_known_distribution")
        self.plt.show(block=block)
Exemplo n.º 4
0
 def test_qq_plot_left_censoring(self, block):
     df = load_nh4()
     fig, axes = self.plt.subplots(2, 2, figsize=(9, 5))
     axes = axes.reshape(4)
     for i, model in enumerate([
             WeibullFitter(),
             LogNormalFitter(),
             LogLogisticFitter(),
             ExponentialFitter()
     ]):
         model.fit(df["NH4.mg.per.L"],
                   ~df["Censored"],
                   left_censorship=True)
         ax = qq_plot(model, ax=axes[i])
         assert ax is not None
     self.plt.suptitle("test_qq_plot_left_censoring")
     self.plt.show(block=block)
Exemplo n.º 5
0
    def test_qq_plot_left_censoring_with_known_distribution(self, block):
        N = 300
        T_actual = scipy.stats.fisk(8, 0, 1).rvs(N)

        MIN_0 = np.percentile(T_actual, 5)
        MIN_1 = np.percentile(T_actual, 10)

        T = T_actual.copy()
        ix = np.random.randint(3, size=N)

        T = np.where(ix == 0, np.maximum(T, MIN_0), T)
        T = np.where(ix == 1, np.maximum(T, MIN_1), T)
        E = T_actual == T

        fig, axes = self.plt.subplots(2, 2, figsize=(9, 5))
        axes = axes.reshape(4)
        for i, model in enumerate([WeibullFitter(), LogNormalFitter(), LogLogisticFitter(), ExponentialFitter()]):
            model.fit_left_censoring(T, E)
            ax = qq_plot(model, ax=axes[i])
            assert ax is not None
        self.plt.suptitle("test_qq_plot_left_censoring_with_known_distribution")
        self.plt.show(block=block)
T = T_actual.copy()
ix = np.random.randint(4, size=N)

T = np.where(ix == 0, np.maximum(T, MIN_0), T)
T = np.where(ix == 1, np.maximum(T, MIN_1), T)
T = np.where(ix == 2, np.maximum(T, MIN_2), T)
T = np.where(ix == 3, np.maximum(T, MIN_3), T)
E = T_actual == T

fig, axes = plt.subplots(2, 2, figsize=(9, 5))
axes = axes.reshape(4)

for i, model in enumerate([WeibullFitter(), KaplanMeierFitter(), LogNormalFitter(), LogLogisticFitter()]):
    if isinstance(model, KaplanMeierFitter):
        model.fit(T, E, left_censorship=True, label=model.__class__.__name__)
    else:
        model.fit(T, E, left_censorship=True, label=model.__class__.__name__)

    model.plot_cumulative_density(ax=axes[i])
plt.tight_layout()

for i, model in enumerate([WeibullFitter(), LogNormalFitter(), LogLogisticFitter()]):
    model.fit(T, E, left_censorship=True)
    fig, axes = plt.subplots(2, 1, figsize=(8, 6))

    left_censorship_cdf_plot(model, ax=axes[0])
    qq_plot(model, ax=axes[1])


plt.show()