What is the practical difference between wilcox.test(x,y, paired=F)
and wilcox.test(x~y, paired=F)
(i.e. using comma vs. tilde sign) in R, and how to interpret the resulting W-statistic? This should be the same statistical test, but the two methods produce different results.
I have a data frame with 24 rows, each containing information about the sex and the length of an individual:
mydata<-structure(list(ID = 1:24, Sex = structure(c(2L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 2L, 1L,1L, 2L, 1L, 1L, 2L,2L, 2L, 2L, 1L, 1L,2L, 2L, 2L, 2L), .Label = c("F", "M"), class = "factor"),Length = c(63.8,79.6, 58, 140, 293, 28.6, 147, 31.3, 33.2, 4.55, 16.4, 19.5, 26.4, 3.34, 29.3, 42.9, 55.6, 122, 30.3, 48.4, 130, 64.7, 93.3, 76.1)), .Names = c("ID", "Sex", "Length"), class = "data.frame", row.names = c(NA, -24L))
I want to explore differences in length between the two sexes using Mann-Whitney U test.
Version 1:
wilcox.test(mydata$Length[mydata$Sex == 'M'], mydata$Length[mydata$Sex == 'F'], paired=F)
Wilcoxon rank sum test
data: mydata$Length[mydata$Sex == "M"] and mydata$Length[mydata$Sex == "F"]
W = 118, p-value = 0.0003698
alternative hypothesis: true location shift is not equal to 0
Version 2:
wilcox.test(mydata$Length ~ mydata$Sex, paired=F)
Wilcoxon rank sum test
data: mydata$Length by mydata$Sex
W = 10, p-value = 0.0003698
alternative hypothesis: true location shift is not equal to 0
They both give me the same P-value, but drastically different W statistics (118 vs. 10). I can't see why this is, or know which one to use for inferences or reporting. Should I not expect to get the same answer from both methods? And how would one go about interpreting the resulting W-statistics?
stats:::wilcox.test.formula
will show you how the formula interface is reduced to a call to the default method. $\endgroup$wilcox.test(mydata$Length[mydata$Sex == 'F'], mydata$Length[mydata$Sex == 'M'], paired=F)
(i.e. switching the F and the M) produces the same result as the formula call. $\endgroup$