scipy-yli/tests/test_ttest.py

51 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_ttest_ind_ol6_1():
"""Compare yli.ttest_ind for Ott & Longnecker (2016) example 6.1"""
df = pd.DataFrame({
'Type': ['Fresh'] * 10 + ['Stored'] * 10,
'Potency': [10.2, 10.5, 10.3, 10.8, 9.8, 10.6, 10.7, 10.2, 10.0, 10.6, 9.8, 9.6, 10.1, 10.2, 10.1, 9.7, 9.5, 9.6, 9.8, 9.9]
})
result = yli.ttest_ind(df, 'Potency', 'Type')
t_expected = 0.54/(0.285*np.sqrt(1/10+1/10))
assert result.statistic == approx(t_expected, abs=0.01)
assert result.dof == 18
assert result.delta.point == approx(0.54, abs=0.01)
assert result.delta.ci_lower == approx(0.272, abs=0.01)
assert result.delta.ci_upper == approx(0.808, abs=0.01)
expected_summary = '''Type Fresh Stored
Potency
μ (SD) 10.37 (0.32) 9.83 (0.24)
t(18) = 4.24; p < 0.001*
Δμ (95% CI) = 0.54 (0.27–0.81), Fresh > Stored'''
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'