From 642d0d4e4f53ca935ef5707d9c5b574f830f6932 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 25 Feb 2023 15:07:42 +1100 Subject: [PATCH] Implement likelihood ratio test for Cox regression --- yli/regress.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/yli/regress.py b/yli/regress.py index 98a3459..a259a36 100644 --- a/yli/regress.py +++ b/yli/regress.py @@ -1,5 +1,5 @@ # scipy-yli: Helpful SciPy utilities and recipes -# Copyright © 2022 Lee Yingtong Li (RunasSudo) +# Copyright © 2022–2023 Lee Yingtong Li (RunasSudo) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -520,16 +520,18 @@ class RegressionResult: else: right_col.append(('F:', format(f_result.statistic, '.2f'))) right_col.append(('p (F):', fmt_p(f_result.pvalue, PValueStyle.VALUE_ONLY))) - elif self.ll_null: + else: # Otherwise report likelihood ratio test as overall test - lrtest_result = self.lrtest_null() - right_col.append(('LL-Model:', format(self.ll_model, '.2f'))) - right_col.append(('LL-Null:', format(self.ll_null, '.2f'))) - if html: - right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, PValueStyle.VALUE_ONLY | PValueStyle.HTML))) - else: - right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, PValueStyle.VALUE_ONLY))) + + if self.ll_null: + lrtest_result = self.lrtest_null() + + right_col.append(('LL-Null:', format(self.ll_null, '.2f'))) + if html: + right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, PValueStyle.VALUE_ONLY | PValueStyle.HTML))) + else: + right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, PValueStyle.VALUE_ONLY))) return left_col, right_col @@ -917,7 +919,7 @@ def regress( elif hasattr(result, 'llnull'): ll_null = result.llnull elif model_class is sm.PHReg: - ll_null = None + ll_null = model.loglike([0 for _ in result.params]) else: # Construct null (intercept-only) model #result_null = model_class.from_formula(formula=dep + ' ~ 1', data=df).fit()