Implement deviance chi-squared test for Poisson regression

This commit is contained in:
RunasSudo 2023-04-21 15:27:42 +10:00
parent 503519c9c0
commit aa88239cb1
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 17 additions and 1 deletions

View File

@ -237,6 +237,7 @@ class RegressionModel:
self.rsquared = None self.rsquared = None
self.ll_model = None self.ll_model = None
self.ll_null = None self.ll_null = None
self.ll_saturated = None
self.f_statistic = None self.f_statistic = None
# Parameters # Parameters
@ -480,6 +481,18 @@ class RegressionModel:
else: else:
return bf01.invert() 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): def ftest(self):
""" """
Perform the *F* test that all slopes are 0 Perform the *F* test that all slopes are 0
@ -1131,6 +1144,9 @@ class Poisson(RegressionModel):
result.ll_model = raw_result.llf result.ll_model = raw_result.llf
result.ll_null = raw_result.llnull 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.terms = raw_terms_from_statsmodels_result(raw_result)
result.vcov = raw_result.cov_params() result.vcov = raw_result.cov_params()

View File

@ -583,7 +583,7 @@ class ChiSquaredResult:
""" """
Result of a generic test with *χ*:sup:`2`-distributed test statistic 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`. See also :class:`yli.regress.BrantResult`, :class:`yli.regress.LikelihoodRatioTestResult`, :class:`PearsonChiSquaredResult`.
""" """