Display t test comparison of groups as table

This commit is contained in:
RunasSudo 2022-11-09 18:18:47 +11:00
parent d8dcece09d
commit ea2bf53ace
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 24 additions and 4 deletions

View File

@ -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.270.81), Fresh > Stored'''
assert result.summary() == expected_summary

View File

@ -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', '<i>μ</i>')
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 '<i>t</i>({:.0f}) = {:.2f}; <i>p</i> {}<br><i>μ</i><sub>{}</sub> (SD) = {:.2f} ({:.2f}), <i>μ</i><sub>{}</sub> (SD) = {:.2f} ({:.2f})<br>Δ<i>μ</i> ({: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 '{}<br><i>t</i>({:.0f}) = {:.2f}; <i>p</i> {}<br>Δ<i>μ</i> ({: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.270.81), Fresh > Stored