From ea2bf53ace58eac7da92e9fd527805d20f38a121 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 9 Nov 2022 18:18:47 +1100 Subject: [PATCH] Display t test comparison of groups as table --- tests/test_ttest.py | 6 ++++-- yli/sig_tests.py | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/test_ttest.py b/tests/test_ttest.py index cf2e60b..46a211f 100644 --- a/tests/test_ttest.py +++ b/tests/test_ttest.py @@ -39,8 +39,10 @@ def test_ttest_ind_ol6_1(): 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* -μ(Fresh) (SD) = 10.37 (0.32), μ(Stored) (SD) = 9.83 (0.24) + expected_summary = ''' Fresh Stored +μ (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 diff --git a/yli/sig_tests.py b/yli/sig_tests.py index d2a7f03..f559c7a 100644 --- a/yli/sig_tests.py +++ b/yli/sig_tests.py @@ -59,13 +59,29 @@ class TTestResult: #: Description of the direction of the effect (*str*) self.delta_direction = delta_direction + def _comparison_table(self, html): + """Return a table showing the means/SDs for each group""" + + table_data = { + self.group1: '{:.2f} ({:.2f})'.format(self.mu1, self.sd1), + self.group2: '{:.2f} ({:.2f})'.format(self.mu2, self.sd2), + } + + if html: + table = pd.DataFrame(table_data, index=['\ue000 (SD)']) # U+E000 is in Private Use Area, mark μ symbol + table_str = table._repr_html_() + return table_str.replace('\ue000', 'μ') + else: + table = pd.DataFrame(table_data, index=['μ (SD)']) + return str(table) + def __repr__(self): if config.repr_is_summary: return self.summary() return super().__repr__() def _repr_html_(self): - return 't({:.0f}) = {:.2f}; p {}
μ{} (SD) = {:.2f} ({:.2f}), μ{} (SD) = {:.2f} ({:.2f})
Δμ ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION | PValueStyle.HTML), self.group1, self.mu1, self.sd1, self.group2, self.mu2, self.sd2, (1-config.alpha)*100, self.delta.summary(), self.delta_direction) + return '{}
t({:.0f}) = {:.2f}; p {}
Δμ ({:g}% CI) = {}, {}'.format(self._comparison_table(True), self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION | PValueStyle.HTML), (1-config.alpha)*100, self.delta.summary(), self.delta_direction) def summary(self): """ @@ -74,7 +90,7 @@ class TTestResult: :rtype: str """ - return 't({:.0f}) = {:.2f}; p {}\nμ({}) (SD) = {:.2f} ({:.2f}), μ({}) (SD) = {:.2f} ({:.2f})\nΔμ ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION), self.group1, self.mu1, self.sd1, self.group2, self.mu2, self.sd2, (1-config.alpha)*100, self.delta.summary(), self.delta_direction) + return '{}\n\nt({:.0f}) = {:.2f}; p {}\nΔμ ({:g}% CI) = {}, {}'.format(self._comparison_table(False), self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION), (1-config.alpha)*100, self.delta.summary(), self.delta_direction) def ttest_ind(df, dep, ind, *, nan_policy='warn'): """ @@ -102,6 +118,8 @@ def ttest_ind(df, dep, ind, *, nan_policy='warn'): yli.ttest_ind(df, 'Potency', 'Type') .. code-block:: text + Fresh Stored + μ (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