wilcoxtest: Calibrated Wilcoxon rank sum and signed rank tests

Description Usage Arguments Details Value Note See Also Examples

View source: R/wilcoxtest.R

Description

Compares the distribution between two random variables by testing if one variable tends to take larger (or smaller) values than the other. The test works for independant and paired variables by using corrected versions of the Wilcoxon (or equivalently Mann-Whitney) one and two-sample tests.

Usage

1
wilcoxtest(x, y = NULL, ...)

Arguments

x, y

the two continuous variables.

alternative

indicates the alternative hypothesis and must be one of "two.sided", "greater" or "less".

ties.break

the method used to break ties in case there are ties in the x or y vectors. Can be "none" or "random".

paired

a logical value. If it equals TRUE, you must provide values for x and y and the paired test is implemented. If it equals FALSE, the paired test is implemented when y is null and only x is provided and the two sample test (for independent variables) is implemented when both x and y are provided.

Details

The null hypothesis for the corrected Wilcoxon (Mann-Whitney) test is: H0 Med(X-Y)=0 where Med represents the median. The alternative is specified by the alternative argument: "greater" means that Med(X-Y)>0 and "less" means that Med(X-Y)<0. The null hypothesis for the paired Wilcoxon (Mann-Whitney) test is: H0 Med(D1+D2)=0 where D1 is the difference between X1 and Y1 taken on the same pair (same with D2 on a different pair). Both tests are asymptotically calibrated in the sense that the rejection probability under the null hypothesis is asymptotically equal to the level of the test.

Value

Returns the result of the test with its corresponding p-value and the value of the test statistic.

Note

The function can also be called using formulas: type wilcoxtest(x~y,data) with x the quantitative variable and y a factor variable with two levels. The option ties.break handles ties in the Wilcoxon test. If ties.break="none" the ties are ignored, if ties.break="random" they are randomly broken. For the Wilcoxon rank sum test the ties between the x and y are detected and broken (but the ties inside the x and y vectors are not changed). For the signed rank test, the ties in the vector x-y (or in the x vector in case y=NULL) are randomly broken.

See Also

cortest, robustest, mediantest, vartest.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#Application on the Evans dataset
data(Evans)
#Description of this dataset is available in the lbreg package
with(Evans,wilcox.test(CHL[CDH==0],CHL[CDH==1]))
with(Evans,wilcoxtest(CHL[CDH==0],CHL[CDH==1]))
wilcoxtest(CHL~CDH,data=Evans) #using formulas
wilcoxtest(CHL~CDH,data=Evans,ties.break="random") #same test were ties are randomly broken


#For independent samples
n=100
M=1000 #number of replications
testone=function(n){
X=runif(n,-0.5,0.5)
Y=rnorm(3*n,0,0.04)
list(test1=wilcoxtest(X,Y)$p.value,test2=wilcox.test(X,Y)$p.value) #wilcox.test is the standard Wilcoxon test
}

#Takes a few seconds to run
res1=res2=rep(NA,M)
for (i in 1:M)
{
result=testone(n)
res1[i]=result$test1
res2[i]=result$test2
}
mean(res1<0.05) #0.049
mean(res2<0.05) #0.174

#For paired samples
#We use the value of the median of a Gamma distributed variable with shape parameter equal to 1/5
#and scale parameter equal to 1. This value is computed from the command qgamma(shape=1/5, scale=1, 0.5)
n=100
M=1000 #number of replications
testone=function(n){
D=rgamma(n,shape=1/10,scale=1)-qgamma(shape=1/5, scale=1, 0.5)/2
list(test1=wilcoxtest(D)$p.value,test2=wilcox.test(D)$p.value) #wilcox.test is the standard paired Wilcoxon test
}
for (i in 1:M)
{
result=testone(n)
res1[i]=result$test1
res2[i]=result$test2
}
mean(res1<0.05) #0.054
mean(res2<0.05) #0.074

obouaziz/test documentation built on March 28, 2021, 7:41 p.m.