statannotations icon indicating copy to clipboard operation
statannotations copied to clipboard

Test against a fixed population mean

Open arteymix opened this issue 3 years ago • 2 comments

The rendering should be the same as the one you obtain when testing a column against itself (i.e. pairs=[('col1', 'col1')]), but testing against a fixed population mean.

I think this can be done neatly by reusing the pairs positional argument and introducing objects for representing the intended tests.

from statsannotations import Pair, Population

Annotator(ax, [Pair('col1', 'col2'), Population('col1', popmean=0, alternative='two-sided'), ('col2', 'col3')])

arteymix avatar May 05 '22 17:05 arteymix

Without supporting different tests on the same "run", and while we are discussing using different parameters passed to statistical tests in #71, which could possibly lead to something even more powerful, You could already achieve part of what you aim for with something like

import scipy.stats as stats
import seaborn as sns
import matplotlib.pyplot as plt
from statannotations.Annotator import Annotator
from statannotations.stats.StatTest import StatTest


def statannotations_to_scipy_ttest_1samp(group1, group2, **stats_params):
    return stats.ttest_1samp(group1, **stats_params)


ttest_1samp = StatTest(
    statannotations_to_scipy_ttest_1samp, "1-sample t-test", 'T-test', 't'
)

x = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]
y = [4, 5, 1.3, 2.6, 6, 6.6, 7, 7.7, 8, 8.8, 9, 9.9]

ax = sns.boxplot(x=x, y=y)

pairs = [(1, 1), (2, 2), (3, 3)]
annot = Annotator(ax=ax, pairs=pairs, x=x, y=y)
annot.configure(test=ttest_1samp)
annot.apply_test(popmean=5)
annot.annotate()
plt.show()

Which returns

p-value annotation legend:
      ns: p <= 1.00e+00
       *: 1.00e-02 < p <= 5.00e-02
      **: 1.00e-03 < p <= 1.00e-02
     ***: 1.00e-04 < p <= 1.00e-03
    ****: p <= 1.00e-04

1 vs. 1: 1-sample t-test, P_val:1.157e-01 t=-2.195e+00
2 vs. 2: 1-sample t-test, P_val:1.446e-02 t=5.115e+00
3 vs. 3: 1-sample t-test, P_val:2.093e-03 t=1.006e+01

myplot

trevismd avatar Oct 18 '22 20:10 trevismd