Add unit test for RegressionResult.brant

This commit is contained in:
RunasSudo 2022-12-02 21:53:07 +11:00
parent ea2d04ada1
commit b2a72a8ef8
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 41 additions and 2 deletions

View File

@ -62,3 +62,38 @@ somewhat likely/very likely 4.30 (2.72 - 5.88) <0.001*
------------------------------------------------------------'''.format(result.fitted_dt)
assert result.summary() == expected_summary
def test_brant_ucla():
"""Compare RegressionResult.brant with R brant library for UCLA example at https://stats.oarc.ucla.edu/r/dae/ordinal-logistic-regression/"""
df = pd.read_stata('tests/data/ucla_ologit.dta')
result = yli.regress(yli.OrdinalLogit, df, 'apply', 'pared + public + gpa', exp=False)
brant_result = result.brant()
# df <- read.dta("https://stats.idre.ucla.edu/stat/data/ologit.dta")
# m <- polr(apply ~ pared + public + gpa, data=df, Hess=TRUE)
# brant(m)
assert brant_result.tests['Omnibus'].statistic == approx(4.34, abs=0.01)
assert brant_result.tests['Omnibus'].dof == 3
assert brant_result.tests['Omnibus'].pvalue == approx(0.23, abs=0.01)
assert brant_result.tests['pared'].statistic == approx(0.13, abs=0.01)
assert brant_result.tests['pared'].dof == 1
assert brant_result.tests['pared'].pvalue == approx(0.72, abs=0.01)
assert brant_result.tests['public'].statistic == approx(3.44, abs=0.01)
assert brant_result.tests['public'].dof == 1
assert brant_result.tests['public'].pvalue == approx(0.06, abs=0.01)
assert brant_result.tests['gpa'].statistic == approx(0.18, abs=0.01)
assert brant_result.tests['gpa'].dof == 1
assert brant_result.tests['gpa'].pvalue == approx(0.67, abs=0.01)
expected_summary = ''' χ² df p
Omnibus 4.34 3 0.23
pared 0.13 1 0.72
public 3.44 1 0.06
gpa 0.18 1 0.67 '''
assert brant_result.summary() == expected_summary

View File

@ -1097,5 +1097,9 @@ class BrantResult:
:rtype: str
"""
# FIXME
return 'FIXME'
table = pd.DataFrame([
['{:.2f}'.format(test.statistic), '{:.0f}'.format(test.dof), fmt_p(test.pvalue, PValueStyle.TABULAR)]
for test in self.tests.values()
], index=self.tests.keys(), columns=['χ² ', 'df', 'p '])
return str(table)