# [R] Weighted Mann-Whitney-Wilcoxon-Test

Alexander Sommer Alexander.Sommer at tu-dortmund.de
Tue Aug 26 15:28:38 CEST 2014

```On Tuesday, August 19, 2014 9:46 PM Thomas Lumley <tlumley at uw.edu> wrote:

>> Is anyone aware of an(other) implementation in R?
> survey::svyranktest

Oh, that easy. Thanks a lot.

Actually, in my case the weights do not derive from some selection probabilities, but your test works anyway.

For completeness (and this time with a working example):

set.seed(seed = 123)
count.x <- NULL
count.y <- NULL
j <- 1
for (i in sample(x = 10:20, size = 20, replace = TRUE)){
count.x[j] <- sample(x = 0:i, size = 1)
count.y[j] <- i - count.x[j]
j          <- j + 1
}
data <- data.frame(x.portion = (count.x/(count.x + count.y)),
y.portion = (count.y/(count.x + count.y)),
group     = c(rep("A", 12), rep("B", 8)),
weight    = (count.x + count.y)
)

I first considered the unweighted case with

library(package = survey)
design <- svydesign(ids = ~0, data = data)
svyranktest(formula = x.portion ~ group, design = design)

and compared it to the default Wilcoxon test by

wilcox.test(formula = x.portion ~ group, data = data, exact = FALSE, correct = FALSE)

Or, if you prefer

library(package = coin)
wilcox_test(formula = x.portion ~ group, data = data)

The resulting p-values differ, as I understood due to an approximation in package /survey/.

Now, finally, the weighted case:

design <- svydesign(ids = ~0, data = data, weights = ~weight)
svyranktest(formula = x.portion ~ group, design)

And, by the way, package /survey/ seems also to be the preferable way, if you want to go for a parametric test. Once again, the unweighted case first:

design <- svydesign(ids = ~0, data = data)
svyttest(formula = x.portion ~ group, design)

And, yet again, the results differ from the default t test

t.test(formula = x.portion ~ group, data = data)

This time, I guess, it is due to the way standard errors are computed.

Finally (this time for real), the weighted case:

design <- svydesign(ids = ~0, data = data, weights = ~weight)
svyttest(x.portion ~ group, design)

Note that function /wtd.t.test/ from package /weights/ depends on the scale of the weights, /svyttest/ not.

Thomas, one more time: thank you for your help.

Cheers,

Alex

--
Alexander Sommer
wissenschaftlicher Mitarbeiter

Technische Universität Dortmund
Fakultät Erziehungswissenschaft, Psychologie und Soziologie
Forschungsverbund Deutsches Jugendinstitut/Technische Universität Dortmund
Vogelpothsweg 78
44227 Dortmund

Telefon: +49 231 755-8189
Fax:     +49 231 755-6553
E-Mail:  Alexander.Sommer at tu-dortmund.de
WWW:     http://www.forschungsverbund.tu-dortmund.de/

```