I would like to reproduce the results of the Wilcoxon-Mann-Whitney-test in R wilcox.test(x,y,paired=FALSE, conf.int=TRUE
.
I succeed to get the W-value but didn't succeed to reproduce difference in location.
Help says:
Note that in the two-sample case the estimator for the difference in location parameters does not estimate the difference in medians (a common misconception) but rather the median of the difference between a sample from x and a sample from y.
Sincerly, I don't understand sample from x? Does that mean that the location difference was simulated using samples from x and y? And what is the difference between a sample from x and and a sample from y? That is what is the difference between two vectors?
I prepared an example:
# -- Create data
A <- c(7,14,22,36,40,48,49,52)
n1 <- length(A)
B <- c(3,5,6,10,17,18,20,39)
n2 <- length(B)
# -- Do some processing
All <- c(A,B)
grp <- c(rep(1,n1), rep(2,n2))
rnk <-rank(All)
xdata <- matrix(c(grp,All,rnk), ncol=3)
names <- c("group","value","rank")
dimnames(xdata) <- list(NULL,names)
t(xdata)
Here is the new data structure:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
group 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2
value 7 14 22 36 40 48 49 52 3 5 6 10 17 18 20 39
rank 4 6 10 11 13 14 15 16 1 2 3 5 7 8 9 12
Computing statistics:
data <- as.data.frame(xdata)
# -- sum of ranks
(r1 <- with(data,sum(rank[group==1])))
(r2 <- with(data,sum(rank[group==2])))
# -- statistics
(u1 <- r2-n2*(n2+1)/2) # u1=11
(u2 <- r1-n1*(n1+1)/2) # u2=53
# -- Test
wilcox.test(A,B, paired=FALSE, conf.int = TRUE)
Output of test:
Wilcoxon rank sum test
data: A and B
W = 53, p-value = 0.02813
alternative hypothesis: true location shift is not equal to 0
95 percent confidence interval:
2 35
sample estimates:
difference in location
19.5
W is
x <- wilcox.test(A,B, paired=FALSE, conf.int = TRUE)
x$statistic
The result W=53
I can get manaully from
max(c(u1,u2)) # max of 53 and 11
I'm just wondering how I can get
x$estimate
difference in location = 19.5
from the data above.