The Research Mining Technology

Saturday 17 November 2012

R code for Wilcoxon rank sum test



Example 1 (R-Code Script)
     Two samples of Young walleye were drawn from two different lakes and the fish were weighed. The data in g are:
R-Code and Results:
> X.1<-c(253,218,292,280,276,275)
> X.2<-c(216,291,256,270,277,285)
> sample<-c(rep(1,6),rep(2,6))
> w<-data.frame(c(X.1,X.2),sample)
> names(w)[1]<-'weight(g)'
> cbind(w[1:6,],w[7:12,])
  weight(g) sample weight(g) sample
1       253      1       216      2
2       218      1       291      2
3       292      1       256      2
4       280      1       270      2
5       276      1       277      2
6       275      1       285      2
> idx<-sort(w[,1],index.return=TRUE)
> d<-rbind(weight=w[idx$ix,1],sample=w[idx$ix,2],
+ rank=1:12)
> dimnames(d)[[2]]<-rep('',12);d
                                                      
weight 216 218 253 256 270 275 276 277 280 285 291 292
sample   2   1   1   2   2   1   1   2   1   2   2   1
rank     1   2   3   4   5   6   7   8   9  10  11  12
> rank.sum<-c(sum(d[3,d[2,]==1]),
+ sum(d[3,d[2,]==2]))
> rank.sum<-rbind(sample=c(1,2),
+ 'rank sum'=rank.sum)
> dimnames(rank.sum)[[2]]<-c('','');rank.sum
             
sample    1  2
rank sum 39 39
> wilcox.test(X.1,X.2)

        Wilcoxon rank sum test

data:  X.1 and X.2
W = 18, p-value = 1
alternative hypothesis: true location shift is not equal to 0
>
Share:

Comment

BTemplates.com

Search This Blog

Powered by Blogger.

Translate

About Me

My photo
Tirunelveli, Tamil Nadu, India

Featured post

Mahalanobis Distance using R code

Mahalanobis distance is one of the standardized distance measure in statistics. It is a unit less distance measure introduced by P. C. Mah...

Weekly