statannotations icon indicating copy to clipboard operation
statannotations copied to clipboard

Order of pair not preserved for one-sided MWU test

Open tuanpham96 opened this issue 4 months ago • 0 comments

I'm trying to plot the test of the one-sided MWU tests but it seems the order defined in pairs is not preserved.

I used one of the demo examples below to illustrate this:

First, two-sided MWU is as expected:

PAIRS = [(("Sat", tip_bucket_list[2]), ("Fri", tip_bucket_list[0]))]
TEST  = 'Mann-Whitney'

Full code

df = sns.load_dataset("tips")
df['tip_bucket'] = pd.cut(df['tip'], 3)

tip_bucket_list = df['tip_bucket'].unique()

PAIRS = [(("Sat", tip_bucket_list[2]), ("Fri", tip_bucket_list[0]))]
TEST  = 'Mann-Whitney'

x = "day"
y = "total_bill"
hue = "tip_bucket"
data = df
ax = sns.boxplot(data=df, x=x, y=y, hue=hue, fliersize=0)

(annot
 .reset_configuration()
 .new_plot(
     ax, PAIRS, 
     data=df, x=x, y=y, hue=hue
 )
 .configure(test=TEST)
 .apply_test()
 .annotate())

plt.legend(loc='upper left', bbox_to_anchor=(1.03, 1))

gives:

Fri_(0.991, 4.0] vs. Sat_(7.0, 10.0]: Mann-Whitney-Wilcoxon test two-sided, P_val:8.114e-03 U_stat=0.000e+00
Image

Now, testing for Sat > Fri

PAIRS = [(("Sat", tip_bucket_list[2]), ("Fri", tip_bucket_list[0]))]
TEST  = 'Mann-Whitney-gt'

gives:

Fri_(0.991, 4.0] vs. Sat_(7.0, 10.0]: Mann-Whitney-Wilcoxon test greater, P_val:9.970e-01 U_stat=0.000e+00
Image

I assumed that specifying the order in PAIRS would perform test against the alternative H1: Sat > Fri but it seems that the order was resorted.

Only when I changed the test that it would turn to significant

PAIRS = [(("Sat", tip_bucket_list[2]), ("Fri", tip_bucket_list[0]))]
TEST  = 'Mann-Whitney-ls'  

gives

Fri_(0.991, 4.0] vs. Sat_(7.0, 10.0]: Mann-Whitney-Wilcoxon test smaller, P_val:4.057e-03 U_stat=0.000e+00
Image

Is this expected behavior? And is there a way to use the specified order in PAIRS for testing? This would matter for one-sided test only.

Thanks

tuanpham96 avatar Aug 15 '25 15:08 tuanpham96