Sunday, October 28, 2007

Friedman Test for Randomized Block Designs

block <- rep(1:6,3); b <- length(unique(block))
task <- rep(c('A','B','C'),rep(6,3)); k<- length(unique(task))
time <- c(1.21,1.63,1.42,1.16,2.43,1.94,1.56,2.01,1.7,
1.27,2.64,2.81,1.48,1.63,2.06,1.27,1.98,2.44)


# rank for each block
rr <- tapply(time, block,rank)

R <- rep(0,length(time))
for(i in 1:b)
for(j in 1:k)
R[b*(j-1)+i] <- rr[[i]][j]


# rank sum of each treatment
S <- tapply(R,task,sum)

# test statistic
Fr <- 12/b/k/(k+1)*sum(S^2) - 3*b*(k+1)

# p-value
pvalue <- 1-pchisq(Fr,k-1)

# R function
friedman.test(time ~ task | block)

No comments: