#Simulations-based test for the equality of several means. #Example with four means: x1 <- c(13,8,9,10) x2 <- c(15,11,13,13) x3 <- c(8,12,7,9) x4 <- c(11,15,10,12) #Find the sample means for each group: mean(x1) mean(x2) mean(x3) mean(x4) #Compute the Mean Absolute Difference (MAD) of the actual data (observed MAD): MAD1 <- ( abs(mean(x1)-mean(x2)) + abs(mean(x1)-mean(x3)) + abs(mean(x1)-mean(x4)) + abs(mean(x2)-mean(x3)) + abs(mean(x2)-mean(x4)) + abs(mean(x3)-mean(x4)) ) / 6 #Under H0 the four means are equal. So place all of them in one sample x <- c(13,8,9,10, 15,11,13,13, 8,12,7,9, 11,15,10,12) #We will re-randomize our data and generate each time four groups with 4 observations in each group: #Initialize vectors: MAD <- rep(0, 1000) for(i in 1:1000){ xx <- sample(x) xxx <- matrix(xx, nrow=4, ncol=4, byrow=TRUE) qq <- colMeans(xxx) MAD[i] <- (abs(qq[1]-qq[2]) + abs(qq[1]-qq[3]) + abs(qq[1]-qq[4]) + abs(qq[2]-qq[3]) + abs(qq[2]-qq[4]) + abs(qq[3]-qq[4]) ) / 6 } #Construct the histogram of MAD: hist(MAD) #Place the observed MAD on the histogram: points(MAD1,0, col="green", pch=19) #Count how many simulated MAD values are equal or larger than the observed MAD: sum(MAD >= MAD1)