Update unit tests

This commit is contained in:
RunasSudo 2023-04-17 15:16:09 +10:00
parent 847ec85083
commit 65f3a2601d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
10 changed files with 31 additions and 5 deletions

4
.coveragerc Normal file
View File

@ -0,0 +1,4 @@
[run]
command_line = -m pytest
omit =
tests/*.py

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
__pycache__ __pycache__
*.pyc *.pyc
/.coverage
/docs/_build /docs/_build

View File

@ -52,3 +52,4 @@ def test_regress_ftest_ol8_2():
expected_summary = 'F(2, 21) = 29.57; p < 0.001*' expected_summary = 'F(2, 21) = 29.57; p < 0.001*'
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<i>F</i>(2, 21) = 29.57; <i>p</i> &lt; 0.001*'

View File

@ -42,7 +42,19 @@ def test_afbf_logit_beta_zero():
assert bf.num_desc == 'Fibrinogen ≠ 0' assert bf.num_desc == 'Fibrinogen ≠ 0'
assert bf.denom_desc == 'Fibrinogen = 0' assert bf.denom_desc == 'Fibrinogen = 0'
expected_summary = '''BF10 = 1.23, Anecdotal evidence in favour of H1
H1: Fibrinogen 0
H0: Fibrinogen = 0'''
assert bf.summary() == expected_summary
assert bf._repr_html_() == 'BF<sub>10</sub> = 1.23, Anecdotal evidence in favour of H<sub>1</sub><br>H<sub>1</sub>: Fibrinogen ≠ 0<br>H<sub>0</sub>: Fibrinogen = 0'
bf = result.bayesfactor_beta_zero('GammaGlobulin') bf = result.bayesfactor_beta_zero('GammaGlobulin')
assert bf.factor == approx(2.417, abs=0.001) assert bf.factor == approx(2.417, abs=0.001)
assert bf.num_desc == 'GammaGlobulin = 0' assert bf.num_desc == 'GammaGlobulin = 0'
assert bf.denom_desc == 'GammaGlobulin ≠ 0' assert bf.denom_desc == 'GammaGlobulin ≠ 0'
expected_summary = '''BF01 = 2.42, Anecdotal evidence in favour of H0
H0: GammaGlobulin = 0
H1: GammaGlobulin 0'''
assert bf.summary() == expected_summary
assert bf._repr_html_() == 'BF<sub>01</sub> = 2.42, Anecdotal evidence in favour of H<sub>0</sub><br>H<sub>0</sub>: GammaGlobulin = 0<br>H<sub>1</sub>: GammaGlobulin ≠ 0'

View File

@ -78,3 +78,4 @@ OR (95% CI) = 1.33 (1.11–1.60)
RR (95% CI) = 1.11 (1.031.18)''' RR (95% CI) = 1.11 (1.031.18)'''
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th>Stress</th>\n <th>False</th>\n <th>True</th>\n </tr>\n <tr>\n <th>Response</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>False</th>\n <td>250</td>\n <td>400</td>\n </tr>\n <tr>\n <th>True</th>\n <td>750</td>\n <td>1600</td>\n </tr>\n </tbody>\n</table>\n</div><br><i>χ</i><sup>2</sup>(1) = 9.82; <i>p</i> = 0.002*<br>OR (95% CI) = 1.33 (1.11–1.60)<br>RR (95% CI) = 1.11 (1.03–1.18)'

View File

@ -1,5 +1,5 @@
# scipy-yli: Helpful SciPy utilities and recipes # 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 # 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 # it under the terms of the GNU Affero General Public License as published by
@ -35,6 +35,7 @@ def test_pearsonr_ol11_15():
expected_summary = 'r (95% CI) = 0.65 (0.11–0.89); p = 0.02*' expected_summary = 'r (95% CI) = 0.65 (0.11–0.89); p = 0.02*'
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<i>r</i> (95% CI) = 0.65 (0.11–0.89); <i>p</i> = 0.02*'
def test_pearsonr_ol11_16(): def test_pearsonr_ol11_16():
"""Compare yli.pearsonr for Ott & Longnecker (2016) example 11.16""" """Compare yli.pearsonr for Ott & Longnecker (2016) example 11.16"""
@ -64,3 +65,4 @@ def test_spearman_ol11_17():
expected_summary = 'ρ (95% CI) = 0.87 (0.60–0.96); p < 0.001*' # NB: The confidence intervals are unvalidated expected_summary = 'ρ (95% CI) = 0.87 (0.60–0.96); p < 0.001*' # NB: The confidence intervals are unvalidated
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<i>ρ</i> (95% CI) = 0.87 (0.60–0.96); <i>p</i> &lt; 0.001*'

View File

@ -1,5 +1,5 @@
# scipy-yli: Helpful SciPy utilities and recipes # 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 # 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 # it under the terms of the GNU Affero General Public License as published by
@ -42,3 +42,4 @@ U = 6.0; p < 0.001*
r = 0.92, Before > After''' r = 0.92, Before > After'''
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th>Sample</th>\n <th>After</th>\n <th>Before</th>\n </tr>\n <tr>\n <th>Oxygen</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Median (IQR)</th>\n <td>10.75 (10.55–10.95)</td>\n <td>11.55 (11.20–11.83)</td>\n </tr>\n <tr>\n <th>Median (range)</th>\n <td>10.75 (11.00–12.10)</td>\n <td>11.55 (11.00–12.10)</td>\n </tr>\n </tbody>\n</table>\n</div><br><i>U</i> = 6.0; <i>p</i> &lt; 0.001*<br><i>r</i> = 0.92, Before > After'

View File

@ -62,6 +62,7 @@ somewhat likely/very likely 4.30 (2.72 - 5.88) <0.001*
------------------------------------------------------------'''.format(result.fitted_dt) ------------------------------------------------------------'''.format(result.fitted_dt)
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<table><caption>Ordinal Logistic Regression Results</caption><tr><th>Dep. Variable:</th><td>apply</td><th>No. Observations:</th><td>400</td></tr><tr><th>Model:</th><td>Ordinal Logit</td><th>Df. Model:</th><td>5</td></tr><tr><th>Date:</th><td>{0:%Y-%m-%d}</td><th>Df. Residuals:</th><td>395</td></tr><tr><th>Time:</th><td>{0:%H:%M:%S}</td><th>Pseudo <i>R</i><sup>2</sup>:</th><td>0.03</td></tr><tr><th>Std. Errors:</th><td>Non-Robust</td><th>LL-Model:</th><td>-358.51</td></tr><tr><th></th><td></td><th>LL-Null:</th><td>-370.60</td></tr><tr><th></th><td></td><th><i>p</i> (LR):</th><td>&lt;0.001*</td></tr></table><table><tr><th></th><th style="text-align:center"><i>β</i></th><th colspan="3" style="text-align:center">(95% CI)</th><th style="text-align:center"><i>p</i></th></tr><tr><th>pared</th><td>1.05</td><td style="padding-right:0">(0.53</td><td>–</td><td style="padding-left:0">1.57)</td><td style="text-align:left">&lt;0.001*</td></tr><tr><th>public</th><td>-0.06</td><td style="padding-right:0">(-0.64</td><td>–</td><td style="padding-left:0">0.53)</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.84</td></tr><tr><th>gpa</th><td>0.62</td><td style="padding-right:0">(0.10</td><td>–</td><td style="padding-left:0">1.13)</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.02*</td></tr><tr><th>(Cutoffs)</th><td></td><td style="padding-right:0"></td><td></td><td style="padding-left:0"></td><td></td></tr><tr><td style="text-align:right;font-style:italic">unlikely/somewhat likely</td><td>2.20</td><td style="padding-right:0">(0.68</td><td>–</td><td style="padding-left:0">3.73)</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.005*</td></tr><tr><td style="text-align:right;font-style:italic">somewhat likely/very likely</td><td>4.30</td><td style="padding-right:0">(2.72</td><td>–</td><td style="padding-left:0">5.88)</td><td style="text-align:left">&lt;0.001*</td></tr></table>'.format(result.fitted_dt)
def test_brant_ucla(): def test_brant_ucla():
"""Compare RegressionModel.brant with R brant library for UCLA example at https://stats.oarc.ucla.edu/r/dae/ordinal-logistic-regression/""" """Compare RegressionModel.brant with R brant library for UCLA example at https://stats.oarc.ucla.edu/r/dae/ordinal-logistic-regression/"""
@ -97,3 +98,4 @@ public 3.44 1 0.06
gpa 0.18 1 0.67 ''' gpa 0.18 1 0.67 '''
assert brant_result.summary() == expected_summary assert brant_result.summary() == expected_summary
assert brant_result._repr_html_() == '<table><caption>Brant Test Results</caption><thead><tr><th></th><th style="text-align:center"><i>χ</i><sup>2</sup></th><th style="text-align:center">df</th><th style="text-align:center"><i>p</i></th></thead><tbody><tr><th>Omnibus</th><td>4.34</td><td>3</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.23</td></tr><tr><th>pared</th><td>0.13</td><td>1</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.72</td></tr><tr><th>public</th><td>3.44</td><td>1</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.06</td></tr><tr><th>gpa</th><td>0.18</td><td>1</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.67</td></tr></tbody></table>'

View File

@ -63,6 +63,7 @@ Dep. Variable: GrowthRet | No. Observations: 20
----------------------------------------------'''.format(result.fitted_dt) ----------------------------------------------'''.format(result.fitted_dt)
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<table><caption>Ordinary Least Squares Regression Results</caption><tr><th>Dep. Variable:</th><td>GrowthRet</td><th>No. Observations:</th><td>20</td></tr><tr><th>Model:</th><td>OLS</td><th>Df. Model:</th><td>1</td></tr><tr><th>Date:</th><td>{0:%Y-%m-%d}</td><th>Df. Residuals:</th><td>18</td></tr><tr><th>Time:</th><td>{0:%H:%M:%S}</td><th><i>R</i><sup>2</sup>:</th><td>0.74</td></tr><tr><th>Std. Errors:</th><td>Non-Robust</td><th><i>F</i>:</th><td>52.01</td></tr><tr><th></th><td></td><th><i>p</i> (<i>F</i>):</th><td>&lt;0.001*</td></tr></table><table><tr><th></th><th style="text-align:center"><i>β</i></th><th colspan="3" style="text-align:center">(95% CI)</th><th style="text-align:center"><i>p</i></th></tr><tr><th>(Intercept)</th><td>47.48</td><td style="padding-right:0">(38.17</td><td>–</td><td style="padding-left:0">56.78)</td><td style="text-align:left">&lt;0.001*</td></tr><tr><th>SoilPh</th><td>-7.86</td><td style="padding-right:0">(-10.15</td><td>–</td><td style="padding-left:0">-5.57)</td><td style="text-align:left">&lt;0.001*</td></tr></table>'.format(result.fitted_dt)
@pytest.mark.skip('Not implemented in refactored regression implementation') @pytest.mark.skip('Not implemented in refactored regression implementation')
def test_regress_bootstrap_ols_ol11_4(): def test_regress_bootstrap_ols_ol11_4():
@ -184,6 +185,7 @@ GammaGlobulin 1.17 (0.92 - 1.48) 0.19
-----------------------------------------------'''.format(result.fitted_dt) -----------------------------------------------'''.format(result.fitted_dt)
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<table><caption>Logistic Regression Results</caption><tr><th>Dep. Variable:</th><td>Unhealthy</td><th>No. Observations:</th><td>32</td></tr><tr><th>Model:</th><td>Logit</td><th>Df. Model:</th><td>2</td></tr><tr><th>Date:</th><td>{0:%Y-%m-%d}</td><th>Df. Residuals:</th><td>29</td></tr><tr><th>Time:</th><td>{0:%H:%M:%S}</td><th>Pseudo <i>R</i><sup>2</sup>:</th><td>0.26</td></tr><tr><th>Std. Errors:</th><td>Non-Robust</td><th>LL-Model:</th><td>-11.47</td></tr><tr><th></th><td></td><th>LL-Null:</th><td>-15.44</td></tr><tr><th></th><td></td><th><i>p</i> (LR):</th><td>0.02*</td></tr></table><table><tr><th></th><th style="text-align:center">exp(<i>β</i>)</th><th colspan="3" style="text-align:center">(95% CI)</th><th style="text-align:center"><i>p</i></th></tr><tr><th>(Intercept)</th><td>0.00</td><td style="padding-right:0">(0.00</td><td>–</td><td style="padding-left:0">0.24)</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.03*</td></tr><tr><th>Fibrinogen</th><td>6.80</td><td style="padding-right:0">(1.01</td><td>–</td><td style="padding-left:0">45.79)</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.049*</td></tr><tr><th>GammaGlobulin</th><td>1.17</td><td style="padding-right:0">(0.92</td><td>–</td><td style="padding-left:0">1.48)</td><td style="text-align:left"><span style="visibility:hidden">=</span>0.19</td></tr></table>'.format(result.fitted_dt)
def test_regress_logit_ol10_18(): def test_regress_logit_ol10_18():
"""Compare odds ratios via yli.regress for Ott & Longnecker (2016) example 10.18""" """Compare odds ratios via yli.regress for Ott & Longnecker (2016) example 10.18"""
@ -251,5 +253,4 @@ Dep. Variable: Outcome | No. Observations: 240
---------------------------------------------'''.format(result.fitted_dt) ---------------------------------------------'''.format(result.fitted_dt)
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<table><caption>Penalised Logistic Regression Results</caption><tr><th>Dep. Variable:</th><td>Outcome</td><th>No. Observations:</th><td>240</td></tr><tr><th>Model:</th><td>Penalised Logit</td><th>Df. Model:</th><td>1</td></tr><tr><th>Date:</th><td>{0:%Y-%m-%d}</td><th>Pseudo <i>R</i><sup>2</sup>:</th><td>0.37</td></tr><tr><th>Time:</th><td>{0:%H:%M:%S}</td><th>LL-Model:</th><td>-66.43</td></tr><tr><th>Std. Errors:</th><td>Non-Robust</td><th>LL-Null:</th><td>-105.91</td></tr><tr><th></th><td></td><th><i>p</i> (LR):</th><td>&lt;0.001*</td></tr></table><table><tr><th></th><th style="text-align:center"><i>β</i></th><th colspan="3" style="text-align:center">(95% CI)</th><th style="text-align:center"><i>p</i></th></tr><tr><th>(Intercept)</th><td>-2.28</td><td style="padding-right:0">(-2.77</td><td>–</td><td style="padding-left:0">-1.85)</td><td style="text-align:left">&lt;0.001*</td></tr><tr><th>Pred</th><td>5.99</td><td style="padding-right:0">(3.95</td><td>–</td><td style="padding-left:0">10.85)</td><td style="text-align:left">&lt;0.001*</td></tr></table>'.format(result.fitted_dt)
# TODO: Test for logit_then_regress

View File

@ -1,5 +1,5 @@
# scipy-yli: Helpful SciPy utilities and recipes # 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 # 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 # it under the terms of the GNU Affero General Public License as published by
@ -47,3 +47,4 @@ t(18) = 4.24; p < 0.001*
Δμ (95% CI) = 0.54 (0.270.81), Fresh > Stored''' Δμ (95% CI) = 0.54 (0.270.81), Fresh > Stored'''
assert result.summary() == expected_summary assert result.summary() == expected_summary
assert result._repr_html_() == '<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th>Type</th>\n <th>Fresh</th>\n <th>Stored</th>\n </tr>\n <tr>\n <th>Potency</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th><i>μ</i> (SD)</th>\n <td>10.37 (0.32)</td>\n <td>9.83 (0.24)</td>\n </tr>\n </tbody>\n</table>\n</div><br><i>t</i>(18) = 4.24; <i>p</i> &lt; 0.001*<br>Δ<i>μ</i> (95% CI) = 0.54 (0.27–0.81), Fresh > Stored'