Xavier Prudent
Xavier Prudent

Reputation: 1742

Weighted Wilcoxon test in R

I want to perform a Wilcoxon test on two samples x and y where x and y are numeric vectors of length n.

Given some experimental design, I would like to give to some entries in x some weights. How can such weights be included in the Wilcoxon test? I found various packages: "survey", "Hmisc"

but a simple test with no weights does not give me back the standard Wilcoxon result: for instance:

x=rnorm(n=100,mean=0,sd=1)
y=rnorm(n=100,mean=0.1,sd=1)
wilcox.test(x,y)
data:  x and y 
W = 4389, p-value = 0.1358

with the WWest function: wwest(x,y)

Wald Test of H0: BETA1=0
TS: 0.0284 PVAL: 0.8665 

Drop Test of H0: BETA1=0
TS: 0.0406 PVAL: 0.8407 

I hope I made myself clear.

Upvotes: 0

Views: 2921

Answers (2)

Xavier Prudent
Xavier Prudent

Reputation: 1742

Here is a suggestion of a weighted Wilcox test x and y are the vectors to compare, wx is the vector of weights to be applied to x,

wwilcox = function( x, y, wx  ){

U = 0
## Loop over the selection branches
for( iy in y ){

## Neutral branches smaller or equal
smaller = which(  x < iy )
equal = which( x == iy )

## Count
sumSmaller = sum(wx[smaller])
sumEqual = sum(wx[equal]/2)
sumTot = sumSmaller + sumEqual

## Total rank
U = U + sumTot
}

## U statistics
nY = length(y)
nX = sum(wx)

## Large sample: U follows a Gaussian
mU = nY * nX / 2
sigU = sqrt( ( nY * nX * ( 1 + nY + nX ) ) / 12 )
zU = ( U - mU ) / sigU

## p-value, one-sided
pU = erfc( zU / sqrt(2) ) /2

return(pU)
}

## Complemantery error function
erfc = function(x) 2 * pnorm(x * sqrt(2), lower = FALSE)

Any comment welcome!

Upvotes: 4

Martin
Martin

Reputation: 594

There is some confusion / lacking consensus what to refer as "Wilcoxon" test. In fact, This guy was quite busy and seems to have contributed to many tests.

While WWest seems to work as paired test (according to a glance at the code - please correct me if wrong), wilcox.test (with default parameters paired=FALSE) does not perform what is often referred to as (paired) Wilcoxon test (but a U-test). To get the "clasical" Wilcoxon test, use paired=FALSE.

Upvotes: 0

Related Questions