scipy-yli/tests/test_ttest.py

46 lines
1.6 KiB
Python

# scipy-yli: Helpful SciPy utilities and recipes
# Copyright © 2022 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 = '''t(18) = 4.24; p < 0.001*
Δμ (95% CI) = 0.54 (0.27–0.81), Fresh > Stored'''
assert result.summary() == expected_summary