From aa88239cb11a6da546c821254a71c90b75995812 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Fri, 21 Apr 2023 15:27:42 +1000 Subject: [PATCH] Implement deviance chi-squared test for Poisson regression --- yli/regress.py | 16 ++++++++++++++++ yli/sig_tests.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/yli/regress.py b/yli/regress.py index 289b810..4470ae0 100644 --- a/yli/regress.py +++ b/yli/regress.py @@ -237,6 +237,7 @@ class RegressionModel: self.rsquared = None self.ll_model = None self.ll_null = None + self.ll_saturated = None self.f_statistic = None # Parameters @@ -480,6 +481,18 @@ class RegressionModel: else: return bf01.invert() + def deviance_chi2(self): + """ + Perform the deviance *χ*:sup:`2` test for goodness of fit + + :rtype: :class:`yli.sig_tests.ChiSquaredResult` + """ + + deviance_model = 2 * (self.ll_saturated - self.ll_model) + pvalue = 1 - stats.chi2.cdf(deviance_model, df=self.dof_resid) + + return ChiSquaredResult(deviance_model, int(self.dof_resid), pvalue) + def ftest(self): """ Perform the *F* test that all slopes are 0 @@ -1131,6 +1144,9 @@ class Poisson(RegressionModel): result.ll_model = raw_result.llf result.ll_null = raw_result.llnull + # Compute saturated log-likelihood + result.ll_saturated = float(sm.families.Poisson().loglike(data_dep + 1e-10, data_dep + 1e-10)) + result.terms = raw_terms_from_statsmodels_result(raw_result) result.vcov = raw_result.cov_params() diff --git a/yli/sig_tests.py b/yli/sig_tests.py index 24b6d98..cd17aef 100644 --- a/yli/sig_tests.py +++ b/yli/sig_tests.py @@ -583,7 +583,7 @@ class ChiSquaredResult: """ Result of a generic test with *χ*:sup:`2`-distributed test statistic - See :meth:`yli.logrank`. + See :meth:`yli.logrank`, :meth:`yli.regress.RegressionModel.deviance_chi2`. See also :class:`yli.regress.BrantResult`, :class:`yli.regress.LikelihoodRatioTestResult`, :class:`PearsonChiSquaredResult`. """