Is it expected to (always?) obtain identical p-values from the the Wilcoxon rank sum test and from the Kruskal-Wallis test ?

4 views (last 30 days)
In the examples here below I compare two datasets, "r1" and "r2", by using both the Wilcoxon rank sum test and the Kruskal-Wallis test.
As you can see in both examples, the p-values resulting from two different tests are the same.
Therefore, is it expected to (always?) obtain identical p-values from the the Wilcoxon rank sum test and from the Kruskal-Wallis test ?
% Example 1: log-normal distributed data
clear r1 r2 % rng(0,'twister');
par1 = 0;
par2 = 100;
r1 = (par2-par1).*round(lognrnd(1,1,1000,1)) + par1;
r2 = (par2-par1).*round(lognrnd(0.88,1.1,1000,1)) + par1;
group = [1 + zeros(size(r1)); 2 + zeros(size(r2))];
p_W = ranksum(r1,r2);
p_KW = kruskalwallis([r1;r2],group,'off');
disp('---------------------------')
disp('Log-normal distributed data')
disp(['Wilcoxon test p-value: ', num2str(p_W)])
disp(['Kruskal Wallis test p-value: ', num2str(p_KW)])
hold on; histogram(r1);histogram(r2)
---------------------------
Log-normal distributed data
Wilcoxon test p-value: 0.29949
Kruskal Wallis test p-value: 0.29947
% Example 2: bimodal distributed data (created with the Gaussian Mixture Model)
clear r1 r2
gmd1 = gmdistribution([-1; 2],cat(3,0.5,1.0),(ones(1,2) / 2));
r1 = random(gmd1,1000);
gmd2 = gmdistribution([-0.8; 1.9],cat(3,0.4,1.2),(ones(1,2) / 2));
r2 = random(gmd2,1000);
group = [1 + zeros(size(r1)); 2 + zeros(size(r2))];
p_W = ranksum(r1,r2);
p_KW = kruskalwallis([r1;r2],group,'off');
disp('---------------------------')
disp('Bimodal distributed data')
disp(['Wilcoxon test p-value: ', num2str(p_W)])
disp(['Kruskal Wallis test p-value: ', num2str(p_KW)])
hold on; histogram(r1);histogram(r2)
---------------------------
Bimodal distributed data
Wilcoxon test p-value: 0.73858
Kruskal Wallis test p-value: 0.73855
  2 Comments
dpb
dpb on 27 Aug 2024
In the absence of ties and for two samples, yes...KW is an extenstion of rank sum that uses ordered ranks instead of the values directly; for no ties the order vector and the vector will always coincide.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!