From f2987bfd2d5a54acbbb4a3785c7f96754251b150 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 31 Dec 2023 18:08:19 +1100 Subject: [PATCH] Bring yli.turnbull up to date with hpstat 82f4a54 --- yli/survival.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/yli/survival.py b/yli/survival.py index 046068e..23457e5 100644 --- a/yli/survival.py +++ b/yli/survival.py @@ -135,7 +135,7 @@ def calc_survfunc_kaplanmeier(time, status, ci, transform_x=None, transform_y=No return xpoints, ypoints, None, None -def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, maxiter=None, fail_prob_tolerance=None, se_method=None, zero_tolerance=None, transform_x=None, transform_y=None, nan_policy='warn', fig=None, ax=None): +def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, maxiter=None, ll_tolerance=None, se_method=None, zero_tolerance=None, ci_precision=None, transform_x=None, transform_y=None, nan_policy='warn', fig=None, ax=None): """ Generate a Turnbull estimator plot, which extends the Kaplan–Meier estimator to interval-censored observations @@ -159,12 +159,14 @@ def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, maxit :type step_loc: float :param maxiter: Maximum number of iterations to attempt :type maxiter: int - :param fail_prob_tolerance: Terminate algorithm when the absolute change in failure probability in each interval is less than this tolerance - :type fail_prob_tolerance: float + :param ll_tolerance: Terminate algorithm when the absolute change in log-likelihood is less than this tolerance + :type ll_tolerance: float :param se_method: Method for computing standard error or survival probabilities (see hpstat *turnbull* documentation) :type se_method: str :param zero_tolerance: Threshold for dropping failure probability when se_method is "oim-drop-zeros" :type zero_tolerance: float + :param ci_precision: Desired precision of confidence limits when se-method is "likelihood-ratio" + :type ci_precision: float :param transform_x: Function to transform x axis by :type transform_x: callable :param transform_y: Function to transform y axis by @@ -195,11 +197,17 @@ def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, maxit for group in groups.groups: subset = groups.get_group(group) - handle = plot_survfunc_turnbull(ax, subset[time_left], subset[time_right], ci, step_loc, maxiter, fail_prob_tolerance, se_method, zero_tolerance, transform_x, transform_y) + handle = plot_survfunc_turnbull( + ax, subset[time_left], subset[time_right], + ci=ci, step_loc=step_loc, maxiter=maxiter, ll_tolerance=ll_tolerance, se_method=se_method, zero_tolerance=zero_tolerance, ci_precision=ci_precision, transform_x=transform_x, transform_y=transform_y + ) handle.set_label('{} = {}'.format(by, group)) else: # No grouping - plot_survfunc_turnbull(ax, df[time_left], df[time_right], ci, step_loc, maxiter, fail_prob_tolerance, se_method, zero_tolerance, transform_x, transform_y) + plot_survfunc_turnbull( + ax, df[time_left], df[time_right], + ci=ci, step_loc=step_loc, maxiter=maxiter, ll_tolerance=ll_tolerance, se_method=se_method, zero_tolerance=zero_tolerance, ci_precision=ci_precision, transform_x=transform_x, transform_y=transform_y + ) if time_units: ax.set_xlabel('{} + {} ({})'.format(time_left, time_right, time_units)) @@ -214,8 +222,11 @@ def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, maxit return fig, ax -def plot_survfunc_turnbull(ax, time_left, time_right, ci, step_loc=0.5, maxiter=None, fail_prob_tolerance=None, se_method=None, zero_tolerance=None, transform_x=None, transform_y=None): - xpoints, ypoints, ypoints0, ypoints1 = calc_survfunc_turnbull(time_left, time_right, ci, step_loc, maxiter, fail_prob_tolerance, se_method, zero_tolerance, transform_x, transform_y) +def plot_survfunc_turnbull(ax, time_left, time_right, *, ci=True, step_loc=0.5, maxiter=None, ll_tolerance=None, se_method=None, zero_tolerance=None, ci_precision=None, transform_x=None, transform_y=None): + xpoints, ypoints, ypoints0, ypoints1 = calc_survfunc_turnbull( + time_left, time_right, + ci=ci, step_loc=step_loc, maxiter=maxiter, ll_tolerance=ll_tolerance, se_method=se_method, zero_tolerance=zero_tolerance, ci_precision=ci_precision, transform_x=transform_x, transform_y=transform_y + ) handle = ax.plot(xpoints, ypoints)[0] @@ -224,7 +235,7 @@ def plot_survfunc_turnbull(ax, time_left, time_right, ci, step_loc=0.5, maxiter= return handle -def calc_survfunc_turnbull(time_left, time_right, ci, step_loc=0.5, maxiter=None, ll_tolerance=None, se_method=None, zero_tolerance=None, transform_x=None, transform_y=None): +def calc_survfunc_turnbull(time_left, time_right, *, ci=True, step_loc=0.5, maxiter=None, ll_tolerance=None, se_method=None, zero_tolerance=None, ci_precision=None, transform_x=None, transform_y=None): # Estimate the survival function # Prepare arguments @@ -241,6 +252,9 @@ def calc_survfunc_turnbull(time_left, time_right, ci, step_loc=0.5, maxiter=None if zero_tolerance: hpstat_args.append('--zero-tolerance') hpstat_args.append(str(zero_tolerance)) + if ci_precision: + hpstat_args.append('--ci-precision') + hpstat_args.append(str(ci_precision)) # Export data to CSV csv_buf = io.StringIO()