MATLAB: Different p-values for ranksum and kruskalwallis on two groups

boxplotkruskal-wallis test”notchranksumwilcoxon rank sum test

Why do I get different p-values when I’m using “Wilcoxon rank sum test” or “Kruskal-Wallis test” on two groups. Which test shoud I prefer?
[p_kruskal,t,stats] = kruskalwallis(X, gender);
[p_wilcoxon,h,stats] = ranksum(X (gender==1), X (gender==2));
I’d like to compare the distributions of this two groups using boxplot with the option of ‘notch’ on. Why even if the notches in the box plot overlap Wilcoxon suggest me that the medians of the two groups differ?

Best Answer

They are different tests, with different hypotheses. The ranksum test compares medians between two groups, assuming both share the same continuous distribution. The kruskalwallis test is to determine if the data from the groups are from the same distribution.
Use the test that addresses the problem you want to solve. If you want to determine if the medians differ, use ranksum. If you want to know if they are from the same distribution, use kruskallwallis.
‘Why even if the notches in the box plot overlap Wilcoxon suggest me that the medians of the two groups differ?’
That could be due to the sample sizes. Small absolute differences can become statistically significant if the sample sizes are large enough.
Related Question