scipy-yli/tests/test_mannwhitney.py

46 lines
2.5 KiB
Python

# scipy-yli: Helpful SciPy utilities and recipes
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from pytest import approx
import numpy as np
import pandas as pd
import yli
def test_mannwhitney_ol6_6():
"""Compare yli.mannwhitney for Ott & Longnecker (2016) example 6.6"""
df = pd.DataFrame({
'Sample': ['Before'] * 12 + ['After'] * 12,
'Oxygen': [11.0, 11.2, 11.2, 11.2, 11.4, 11.5, 11.6, 11.7, 11.8, 11.9, 11.9, 12.1, 10.2, 10.3, 10.4, 10.6, 10.6, 10.7, 10.8, 10.8, 10.9, 11.1, 11.1, 11.3]
})
result = yli.mannwhitney(df, 'Oxygen', 'Sample', method='asymptotic', alternative='less')
assert result.pvalue == approx(0.00007, abs=0.00001)
expected_summary = '''Sample After Before
Oxygen
Median (IQR) 10.75 (10.55–10.95) 11.55 (11.20–11.83)
Median (range) 10.75 (11.00–12.10) 11.55 (11.00–12.10)
U = 6.0; p < 0.001*
r = 0.92, Before > After'''
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'