# [R] Wilcoxon Test

peter dalgaard pdalgd at gmail.com
Fri Apr 21 15:05:40 CEST 2017

```Also, as far as I know just for historical consistency, the test statistic in R is the rank sum of the first group MINUS its minimum possible value: W = 110.5 - sum(1:13) = 19.5

-pd

> On 21 Apr 2017, at 14:54 , Achim Zeileis <Achim.Zeileis at uibk.ac.at> wrote:
>
> On Fri, 21 Apr 2017, Tripoli Massimiliano wrote:
>
>> Dear R users,
>> Why the result of Wilcoxon sum rank test by R is different from sas
>>
>> https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_npar1way_sect022.htm
>>
>> The code is next:
>>
>> sampleA <- c(1.94, 1.94, 2.92, 2.92, 2.92, 2.92, 3.27, 3.27, 3.27, 3.27,
>>       3.7, 3.7, 3.74)
>>
>> sampleB <- c(3.27, 3.27, 3.27, 3.7, 3.7, 3.74)
>> wilcox.test(A,B,paired = F)
>
> There are different ways how to compute or approximate the asymptotic or exact conditional distribution of the test statistic:
>
> SAS reports an asymptotic normal approximation (apparently without continuity correction along with an asymptotic t approximation and the exact conditional distribution.
>
> Base R's stats::wilcox.test can either report the exact conditional distribution (but only if there are no ties) or the asymptotic normal distribution (with or without continuity correction). In small samples the default is to use the former but a warning is issued when there are ties (as in your case).
>
> Furthermore, coin::wilcox_test can report either the asymptotic normal distribution (without continuity correction) or the exact conditional distribution (even in the presence of ties).
>
> Thus:
>
> ## collect data in data.frame
> d <- data.frame(
>  y = c(sampleA, sampleB),
>  x = factor(rep(0:1, c(length(sampleA), length(sampleB))))
> )
>
> ## asymptotic normal distribution without continuity correction
> ## (p = 0.0764)
> stats::wilcox.test(y ~ x, data = d, exact = FALSE, correct = FALSE)
> coin::wilcox_test(y ~ x, data = d, distribution = "asymptotic")
>
> ## exact conditional distribution (p = 0.1054)
> coin::wilcox_test(y ~ x, data = d, distribution = "exact")
>
> These match SAS's results. The default result of stats::wilcox.test is different as explained by the warning issued.
>
> hth,
> Z
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

```