# Mann-Whitney U test or Kruskal Wallis test for comparing median of two groups?

I know that Kruskal Wallis test can be used to compare median of two or more groups (e.g. see this link: https://www.statisticshowto.datasciencecentral.com/kruskal-wallis/ ), and that Mann-Whitney U test can be used for two groups.

My question is: Can we use Kruskal Wallis all the time? It can do what Mann-Whitney U test does (when comparing two groups). If the answer is yes, then why don't we just discard Mann-Whitney U test, i.e. why does Mann-Whitney U test exist?

• Kruskal Wallis does not compare medians. Neither does the Mann-Whitney. Feb 4, 2019 at 15:09
• @Glen_b : What if we assume that all groups have the same shape distribution? Then the rank sums comparison is equal to comparing medians, right? Feb 4, 2019 at 15:21
• Yes, but if you assume that much, it would also work equally well for comparing means (and any number of other location measures); it would be then best be described as a test for location shift alternatives. Feb 4, 2019 at 21:34
• The site you link to says having the same shapes is an assumption of the test; this is not a necessary assumption; you need the same shape under the null for exchangeability, but you could for example have cases with a sequence of alternatives that have increasingly different shapes as the locations become different (as with gamma distributions with changing shape parameter, or beta distributions with constant sum-of-parameters). There are perfectly reasonable situations where different shapes under the alternative presents no difficulties of interpretation. Feb 4, 2019 at 22:03

1. With two samples a Kruskal-Wallis is equivalent to a Wilcoxon-Mann-Whitney but without the direction information; so you lose the ability to do a one-sided test.

2. Some implementations use the exact distribution for small samples with the Wilcoxon-Mann-Whitney but not for the Kruskal-Wallis (yielding not-so-accurate p-values with small samples). R is one example; if both sample sizes are below 50 (and there are no ties) it uses the exact null distribution Wilcoxon-Mann-Whitney, but it uses the chi-squared approximation for the Kruskal-Wallis, sometimes leading to rejection when you should not reject (as in the example below) or failure to reject when you should.

wilcox.test(a,b)

Wilcoxon rank sum test

data:  a and b
W = 90, p-value = 0.05032
alternative hypothesis: true location shift is not equal to 0


kruskal.test(values~ind,stack(list(a=a,b=b)))

Kruskal-Wallis rank sum test

data:  values by ind
Kruskal-Wallis chi-squared = 3.913, df = 1, p-value = 0.04791

3. Even at larger samples the results may differ if the Wilcoxon-Mann-Whitney implements a continuity correction (as in R) but the Kruskal-Wallis does not I don't recall offhand seeing any packages that implement a continuity correction with the Kruskal-Wallis (nor is it quite clear how to do this for more than 2 groups nor that it would be a good idea to do so). This is less important, since they're both approximations - neither is the 'correct' answer - but it's still a potential difference in their decisions in cases where the p-value is near the significance level.

• thanks for the great answer! Regarding your first point, may I ask you how you know this: with two samples a Kruskal-Wallis is equivalent to a Wilcoxon-Mann-Whitney. I am a newbie in Statistics and I tried to see this by looking into the math formula of the two tests, but I saw nothing... Do you have any suggestion for a newbie like me when facing this kind of situation? Is reading paper the best way? or..? Thank you! Feb 5, 2019 at 11:53
• The WMW can be rewritten in terms of a difference in group mean rank; a constant multiple of the squared difference in group mean rank will be a variance of group mean rank. The KW is a scaled variance of group mean rank about overall mean rank; so the square of a standardized WMW should correspond to KW -- and indeed they reject the same cases when you treat them the same way. If you compute the z-score for a Wilcoxon-Mann-Whitney and square it, you should get the statistic for a Kruskal-Wallis. If you do a two tailed test in the first instance, then ... ctd Feb 6, 2019 at 0:14
• ctd... either using the exact null distribution for both, or using the z approximation for the WMW and the chi-squared for the KW will have corresponding critical values and should reject the same cases and fail to reject the same cases (i.e. they are equivalent tests). It's possible Conover has the algebraic proof, but it's probably already on site here somewhere. You can verify for yourself that a squared z-score for a WMW corresponds to the KW test statistic by trying an example or two. Feb 6, 2019 at 0:26