3
$\begingroup$

One-sided test

Lets say I want to compute one-sided wilcoxon test:

R:

> wilcox.test(B,A, alternative="greater", paired=TRUE, correct=FALSE, exact=FALSE)

    Wilcoxon signed rank test

data:  B and A
V = 28, p-value = 0.00898
alternative hypothesis: true location shift is greater than 0

> 
> wilcox.test(A, B, alternative="greater", paired=TRUE, correct=FALSE, exact=FALSE)

    Wilcoxon signed rank test

data:  A and B
V = 0, p-value = 0.991
alternative hypothesis: true location shift is greater than 0

but if I do it in python (I need to divide the p-value by 2): wilcoxon(B, A) and wilcoxon(A, B) give the same statistics, so there is no way how to identify the direction of rejection.

>>> wilcoxon(A,B)
WilcoxonResult(statistic=0.0, pvalue=0.011412036386001651)
>>> wilcoxon(B,A)
WilcoxonResult(statistic=0.0, pvalue=0.011412036386001651)

Is there any alternatives for one-sided Wilcoxon test in python that will give also the direction of rejection?

EDIT, Old question: (answered by Robin Ryder)

I found some differences in test statistics in python and R and I cannot figure out what is the difference.

Python:

from scipy.stats import wilcoxon
A = [0.19826790, 1.36836629, 1.37950911, 1.46951540, 1.48197798, 0.07532846,1]
B = [10,10,10,10,10,10,10]
wilcoxon(B, A)
WilcoxonResult(statistic=0.0, pvalue=0.017960477526078766)

R:

A = c(0.19826790, 1.36836629, 1.37950911, 1.46951540, 1.48197798, 0.07532846,1)
B = c(10,10,10,10,10,10,10)

wilcox.test(B,A, alternative="two.sided", paired=TRUE, correct=FALSE, exact=FALSE)


    Wilcoxon signed rank test

data:  B and A
V = 28, p-value = 0.01796
alternative hypothesis: true location shift is not equal to 0

The p-values are the same but the test statistics are completely different. I checked the script code for it and it wasn't so clear how to resemble R test statistics and why there eis a difference.

$\endgroup$
3
  • $\begingroup$ Many posts on site already discuss a number of different (but equivalent) definitions of these statistics $\endgroup$
    – Glen_b
    Commented Jul 14, 2018 at 10:07
  • 1
    $\begingroup$ @Glen_b that is amazing! However, I didnt find any which would tell how to recognise if B is bigger than A if wilcoxon(A,B)==wilcoxon(B, A) $\endgroup$
    – Alina
    Commented Jul 14, 2018 at 10:13
  • $\begingroup$ Yep, I see now. You should probably focus the question on that. $\endgroup$
    – Glen_b
    Commented Jul 14, 2018 at 10:36

2 Answers 2

1
$\begingroup$

A and B play symmetric roles in the Wilcoxon test. The issue is simply which is specified as sample 1, and which is sample 2.

R will give you the same answer as Python if you switch the order of the two samples:

wilcox.test(A,B, alternative="two.sided", paired=TRUE, correct=FALSE, exact=FALSE)

Wilcoxon signed rank test

data:  A and B
V = 0, p-value = 0.01796
alternative hypothesis: true location shift is not equal to 0
$\endgroup$
1
  • $\begingroup$ If change the argument order of the python function wilcoxon(B, A), I get exactly the same result as wilcoxon(A,B). The test statistic doesnt change. This arises another issue I mentioned din the question. $\endgroup$
    – Alina
    Commented Jul 14, 2018 at 9:51
0
$\begingroup$

As mentioned here the last few lines of def wilcoxon() (~python_directory\site-packages\scipy\stats\morestats.py) are:

se = sqrt(se / 24)
z = (T - mn) / se
prob = 2. * distributions.norm.sf(abs(z))
return T, prob

Also according to the comments of this post the apropriate one-sided hypotesis can be decided by the sign of T -mn hence the sign of z

So spending some hours searching for a solution, I have not found the better way than writing my own version of one-sided Wilcoxon test. It is basicly the same as def wilcoxon() from ~python_directory\site-packages\scipy\stats\morestats.py, just returning proper p-value for one-sided test.

def wilcoxon(x, y=None, zero_method="wilcox", correction=False, alternative="two-sided"):
    ...
    <code from the morestats.py until return statement>
    ...
    if alternative == "two-sided":
        return WilcoxonResult(T, prob)
    elif alternative == "greater":
        return WilcoxonResult(T, prob/2) if z > 0 else WilcoxonResult(T, 1 - prob/2)
    elif alternative == "less":
        return WilcoxonResult(T, prob/2) if z < 0 else WilcoxonResult(T, 1 - prob/2)
    else:
        raise ValueError("Alternative should be either 'two-sided' "
                         "or 'less' or 'greater'")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.