trevismd / statannotations

add statistical significance annotations on seaborn plots. Further development of statannot, with bugfixes, new features, and a different API.
Other
624 stars 67 forks source link

Test against a fixed population mean #58

Open arteymix opened 2 years ago

arteymix commented 2 years ago

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')])
trevismd commented 1 year ago

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