Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Wilcoxon Signed Rank Test | Mann-Whitney-Wilcoxon Test - differences to R #389

@hkoestin

Description

@hkoestin

Dear all,

recently, I decided to use the Accord.net framework, because it comes with implementations for different tests I need in one of my projects. Namely, these are the Wilcoxon Signed Rank Test for comparing a sample against a median value and the Mann-Whitney-Wilcoxon Test to compare two samples.

Now, as I started implementing things, I figured out, that the Accord.net results do not match the results coming from R.

Is there an explanation for the differences?
So far, I just found one hint about this, that R may calculate the ranks differently. See here: http://stats.stackexchange.com/questions/65844/wilcoxon-rank-sum-test-in-r

Thank you so much for any help concerning this topic!

See below my examples. Excuse the bad code, but it is just for demonstration purpose of the differences.

Cheers, Harald


C# Testing stuff

var s1 = new double[] { 35, 15, 25, 10, 45, 20, 21, 22, 30, 17 };
var s2 = new double[] { 20, 17, 23, 15, 49, 19, 24, 26, 33, 18 };

Console.WriteLine("C# Tests");

var mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond);
Console.WriteLine("MannWWT s1 > s2:  " + mannWhitneyWilcoxonTest.PValue + 
    " Stat1: " + mannWhitneyWilcoxonTest.Statistic1 + "; Stat2: " + mannWhitneyWilcoxonTest.Statistic2 +
    " - Significant: " + mannWhitneyWilcoxonTest.Significant + " - Hyp: " + mannWhitneyWilcoxonTest.Hypothesis);
mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.ValuesAreDifferent);
Console.WriteLine("MannWWT s1 <> s2: " + mannWhitneyWilcoxonTest.PValue +
    " Stat1: " + mannWhitneyWilcoxonTest.Statistic1 + "; Stat2: " + mannWhitneyWilcoxonTest.Statistic2 + 
    " - Significant: " + mannWhitneyWilcoxonTest.Significant + " - Hyp: " + mannWhitneyWilcoxonTest.Hypothesis);
mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond);
Console.WriteLine("MannWWT s1 < s2:  " + mannWhitneyWilcoxonTest.PValue +
    " Stat1: " + mannWhitneyWilcoxonTest.Statistic1 + "; Stat2: " + mannWhitneyWilcoxonTest.Statistic2 + 
    " - Significant: " + mannWhitneyWilcoxonTest.Significant + " - Hyp: " + mannWhitneyWilcoxonTest.Hypothesis);

var wilcoxonSignedRankTest = new WilcoxonSignedRankTest(s1, 13, OneSampleHypothesis.ValueIsGreaterThanHypothesis);
Console.WriteLine("WSRT > 13:  " + wilcoxonSignedRankTest.PValue +
    " Stat: " + wilcoxonSignedRankTest.Statistic +" - Significant: " +
    wilcoxonSignedRankTest.Significant + " - Hyp: " + wilcoxonSignedRankTest.Hypothesis);
wilcoxonSignedRankTest = new WilcoxonSignedRankTest(s1, 13, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
Console.WriteLine("WSRT <> 13: " + wilcoxonSignedRankTest.PValue +
    " Stat: " + wilcoxonSignedRankTest.Statistic + " - Significant: " +
    wilcoxonSignedRankTest.Significant + " - Hyp: " + wilcoxonSignedRankTest.Hypothesis);
wilcoxonSignedRankTest = new WilcoxonSignedRankTest(s1, 13, OneSampleHypothesis.ValueIsSmallerThanHypothesis);
Console.WriteLine("WSRT < 13: " + wilcoxonSignedRankTest.PValue +
    " Stat: " + wilcoxonSignedRankTest.Statistic + " - Significant: " +
    wilcoxonSignedRankTest.Significant + " - Hyp: " + wilcoxonSignedRankTest.Hypothesis);

This produces

C# Tests
MannWWT s1 > s2:  0,507350234904414 Stat1: 50,5; Stat2: 49,5 - Significant: False 
MannWWT s1 <> s2: 0,985299530191171 Stat1: 50,5; Stat2: 49,5 - Significant: False 
MannWWT s1 < s2:  0,492649765095586 Stat1: 50,5; Stat2: 49,5 - Significant: False


WSRT > 13: 0,0029296875 Stat: 53 - Significant: True
WSRT <> 13: 0,005859375 Stat: 53 - Significant: True 
WSRT < 13:  0,9970703125 Stat: 53 - Significant: False 

And here the R code (and output) for testing:

a <- c(35, 15, 25, 10, 45, 20, 21, 22, 30, 17)
b <- c(20, 17, 23, 15, 49, 19, 24, 26, 33, 18)

wilcox.test(a,b, paired=FALSE, alternative="greater")     # W = 49.5, p-value = 0.5302  
wilcox.test(a,b, paired=FALSE, alternative="two.sided") # W = 49.5, p-value = 1 
wilcox.test(a,b, paired=FALSE, alternative="less")           # W = 49.5, p-value = 0.5 

wilcox.test(a, mu=13, alternative="greater")     # V = 53, p-value = 0.00293
wilcox.test(a, mu=13, alternative="two.sided") # V = 53, p-value = 0.005859
wilcox.test(a, mu=13, alternative="less")          # V = 53, p-value = 0.998

So the one-sample Wilcoxon Signed Rank Test produces quite similar results in R and C#.
But the Mann-W-W-Test not really. There, the difference is at about 3% some time. Why?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions