Implement yli.logrank

This commit is contained in:
RunasSudo 2023-02-25 17:23:20 +11:00
parent e83aa88b19
commit 88509c71a3
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 16 additions and 2 deletions

View File

@ -22,7 +22,7 @@ from .graphs import init_fonts
from .io import pickle_read_compressed, pickle_read_encrypted, pickle_write_compressed, pickle_write_encrypted
from .regress import OrdinalLogit, PenalisedLogit, logit_then_regress, regress, vif
from .sig_tests import anova_oneway, auto_univariable, chi2, mannwhitney, pearsonr, spearman, ttest_ind
from .survival import kaplanmeier
from .survival import kaplanmeier, logrank
from .utils import as_ordinal
def reload_me():

View File

@ -513,7 +513,7 @@ class ChiSquaredResult:
:rtype: str
"""
return 'χ²({}) = {:.2f}; p {}'.format(self.ct, self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION))
return 'χ²({}) = {:.2f}; p {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION))
class PearsonChiSquaredResult(ChiSquaredResult):
"""

View File

@ -18,6 +18,7 @@ from scipy import stats
import statsmodels.api as sm
from .config import config
from .sig_tests import ChiSquaredResult
from .utils import check_nan
def kaplanmeier(df, time, status, by=None, ci=True, nan_policy='warn'):
@ -102,3 +103,16 @@ def plot_survfunc(ax, time, status, ci):
ax.fill_between(xpoints, ypoints0, ypoints1, alpha=0.3, label='_')
return handle
def logrank(df, time, status, by, nan_policy='warn'):
# TODO: Documentation
# Check for/clean NaNs
df = check_nan(df[[time, status, by]], nan_policy)
if df[time].dtype == '<m8[ns]':
df[time] = df[time].dt.total_seconds()
statistic, pvalue = sm.duration.survdiff(df[time], df[status], df[by])
return ChiSquaredResult(statistic=statistic, dof=1, pvalue=pvalue)