6 Friedman Rank

Nonparametric version of two-way ANOVA. The collected data of an experimental design involves two factors, each at two or more levels, the treatment factor and blocking factor.

library(BSDA)
## Loading required package: lattice
## 
## Attaching package: 'BSDA'
## The following object is masked from 'package:datasets':
## 
##     Orange
x=c(1.83,0.50,1.62,2.48,1.68,1.88,1.55,3.06,1.30);
y=c(0.878,0.647,0.598,2.05,1.06,1.29,1.06,3.14,1.29);
plot(x,y,las=1); abline(0,1)

xy=cbind(x,y); xy
##          x     y
##  [1,] 1.83 0.878
##  [2,] 0.50 0.647
##  [3,] 1.62 0.598
##  [4,] 2.48 2.050
##  [5,] 1.68 1.060
##  [6,] 1.88 1.290
##  [7,] 1.55 1.060
##  [8,] 3.06 3.140
##  [9,] 1.30 1.290
axy=apply(xy,1,rank); axy
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## x    2    1    2    2    2    2    2    1    2
## y    1    2    1    1    1    1    1    2    1
(rowMeans(axy)-3/2)^2
##          x          y 
## 0.07716049 0.07716049
S=sum((rowMeans(axy)-3/2)^2)*12*9/6
S
## [1] 2.777778
pchisq(S,1,lower.tail=F)
## [1] 0.0955807
n=length(x); da=cbind(z=c(x,y),gr=c(rep(1,n),rep(2,n)),blk=rep(1:n,2))
da
##           z gr blk
##  [1,] 1.830  1   1
##  [2,] 0.500  1   2
##  [3,] 1.620  1   3
##  [4,] 2.480  1   4
##  [5,] 1.680  1   5
##  [6,] 1.880  1   6
##  [7,] 1.550  1   7
##  [8,] 3.060  1   8
##  [9,] 1.300  1   9
## [10,] 0.878  2   1
## [11,] 0.647  2   2
## [12,] 0.598  2   3
## [13,] 2.050  2   4
## [14,] 1.060  2   5
## [15,] 1.290  2   6
## [16,] 1.060  2   7
## [17,] 3.140  2   8
## [18,] 1.290  2   9
friedman.test(z~gr|blk,data=da)
## 
##  Friedman rank sum test
## 
## data:  z and gr and blk
## Friedman chi-squared = 2.7778, df = 1, p-value = 0.09558
test.stat=(sum(x>y)-n/2)/sqrt(n/4); t2=test.stat^2
c(2*pnorm(test.stat,lower.tail=F),t2,pchisq(t2,1,lower.tail=F))
## [1] 0.0955807 2.7777778 0.0955807
wilcox.test(x,y,paired=T)
## 
##  Wilcoxon signed rank exact test
## 
## data:  x and y
## V = 40, p-value = 0.03906
## alternative hypothesis: true location shift is not equal to 0