Report group means/SD for t test
This commit is contained in:
parent
bae93b2c8e
commit
ab90cfc0e4
@ -40,6 +40,7 @@ def test_ttest_ind_ol6_1():
|
|||||||
assert result.delta.ci_upper == approx(0.808, abs=0.01)
|
assert result.delta.ci_upper == approx(0.808, abs=0.01)
|
||||||
|
|
||||||
expected_summary = '''t(18) = 4.24; p < 0.001*
|
expected_summary = '''t(18) = 4.24; p < 0.001*
|
||||||
|
μ(Fresh) (SD) = 10.37 (0.32), μ(Stored) (SD) = 9.83 (0.24)
|
||||||
Δμ (95% CI) = 0.54 (0.27–0.81), Fresh > Stored'''
|
Δμ (95% CI) = 0.54 (0.27–0.81), Fresh > Stored'''
|
||||||
|
|
||||||
assert result.summary() == expected_summary
|
assert result.summary() == expected_summary
|
||||||
|
@ -35,13 +35,25 @@ class TTestResult:
|
|||||||
See :func:`yli.ttest_ind`.
|
See :func:`yli.ttest_ind`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, statistic, dof, pvalue, delta, delta_direction):
|
def __init__(self, statistic, dof, pvalue, group1, group2, mu1, mu2, sd1, sd2, delta, delta_direction):
|
||||||
#: *t* statistic (*float*)
|
#: *t* statistic (*float*)
|
||||||
self.statistic = statistic
|
self.statistic = statistic
|
||||||
#: Degrees of freedom of the *t* distribution (*int*)
|
#: Degrees of freedom of the *t* distribution (*int*)
|
||||||
self.dof = dof
|
self.dof = dof
|
||||||
#: *p* value for the *t* statistic (*float*)
|
#: *p* value for the *t* statistic (*float*)
|
||||||
self.pvalue = pvalue
|
self.pvalue = pvalue
|
||||||
|
#: Name of the first group (*str*)
|
||||||
|
self.group1 = group1
|
||||||
|
#: Name of the second group (*str*)
|
||||||
|
self.group2 = group2
|
||||||
|
#: Mean of the first group (*float*)
|
||||||
|
self.mu1 = mu1
|
||||||
|
#: Mean of the second group (*float*)
|
||||||
|
self.mu2 = mu2
|
||||||
|
#: Standard deviation of the first group (*float*)
|
||||||
|
self.sd1 = sd1
|
||||||
|
#: Standard deviation of the second group (*float*)
|
||||||
|
self.sd2 = sd2
|
||||||
#: Absolute value of the mean difference (:class:`yli.utils.Estimate`)
|
#: Absolute value of the mean difference (:class:`yli.utils.Estimate`)
|
||||||
self.delta = delta
|
self.delta = delta
|
||||||
#: Description of the direction of the effect (*str*)
|
#: Description of the direction of the effect (*str*)
|
||||||
@ -53,7 +65,7 @@ class TTestResult:
|
|||||||
return super().__repr__()
|
return super().__repr__()
|
||||||
|
|
||||||
def _repr_html_(self):
|
def _repr_html_(self):
|
||||||
return '<i>t</i>({:.0f}) = {:.2f}; <i>p</i> {}<br>Δ<i>μ</i> ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=True), (1-config.alpha)*100, self.delta.summary(), self.delta_direction)
|
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, html=True), self.group1, self.mu1, self.sd1, self.group2, self.mu2, self.sd2, (1-config.alpha)*100, self.delta.summary(), self.delta_direction)
|
||||||
|
|
||||||
def summary(self):
|
def summary(self):
|
||||||
"""
|
"""
|
||||||
@ -62,7 +74,7 @@ class TTestResult:
|
|||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return 't({:.0f}) = {:.2f}; p {}\nΔμ ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=False), (1-config.alpha)*100, self.delta.summary(), self.delta_direction)
|
return 't({:.0f}) = {:.2f}; p {}\nμ({}) (SD) = {:.2f} ({:.2f}), μ({}) (SD) = {:.2f} ({:.2f})\nΔμ ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=False), self.group1, self.mu1, self.sd1, self.group2, self.mu2, self.sd2, (1-config.alpha)*100, self.delta.summary(), self.delta_direction)
|
||||||
|
|
||||||
def ttest_ind(df, dep, ind, *, nan_policy='warn'):
|
def ttest_ind(df, dep, ind, *, nan_policy='warn'):
|
||||||
"""
|
"""
|
||||||
@ -106,8 +118,8 @@ def ttest_ind(df, dep, ind, *, nan_policy='warn'):
|
|||||||
|
|
||||||
# Do t test
|
# Do t test
|
||||||
# Use statsmodels rather than SciPy because this provides the mean difference automatically
|
# Use statsmodels rather than SciPy because this provides the mean difference automatically
|
||||||
d1 = sm.stats.DescrStatsW(data1)
|
d1 = sm.stats.DescrStatsW(data1, ddof=1)
|
||||||
d2 = sm.stats.DescrStatsW(data2)
|
d2 = sm.stats.DescrStatsW(data2, ddof=1)
|
||||||
|
|
||||||
cm = sm.stats.CompareMeans(d1, d2)
|
cm = sm.stats.CompareMeans(d1, d2)
|
||||||
statistic, pvalue, dof = cm.ttest_ind()
|
statistic, pvalue, dof = cm.ttest_ind()
|
||||||
@ -115,11 +127,18 @@ def ttest_ind(df, dep, ind, *, nan_policy='warn'):
|
|||||||
delta = d1.mean - d2.mean
|
delta = d1.mean - d2.mean
|
||||||
ci0, ci1 = cm.tconfint_diff(config.alpha)
|
ci0, ci1 = cm.tconfint_diff(config.alpha)
|
||||||
|
|
||||||
# t test is symmetric so take absolute values
|
# t test is symmetric so take absolute value
|
||||||
|
if d2.mean > d1.mean:
|
||||||
|
delta, ci0, ci1 = -delta, -ci1, -ci0
|
||||||
|
d1, d2 = d2, d1
|
||||||
|
group1, group2 = group2, group1
|
||||||
|
# Now group1 > group2
|
||||||
|
|
||||||
return TTestResult(
|
return TTestResult(
|
||||||
statistic=abs(statistic), dof=dof, pvalue=pvalue,
|
statistic=abs(statistic), dof=dof, pvalue=pvalue,
|
||||||
delta=abs(Estimate(delta, ci0, ci1)),
|
group1=group1, group2=group2, mu1=d1.mean, mu2=d2.mean, sd1=d1.std, sd2=d2.std,
|
||||||
delta_direction=('{0} > {1}' if d1.mean > d2.mean else '{1} > {0}').format(group1, group2))
|
delta=Estimate(delta, ci0, ci1),
|
||||||
|
delta_direction='{} > {}'.format(group1, group2))
|
||||||
|
|
||||||
# -------------
|
# -------------
|
||||||
# One-way ANOVA
|
# One-way ANOVA
|
||||||
|
Loading…
Reference in New Issue
Block a user