# Example of generating random permutations to see if an observed difference between the # means of two groups is large compared to differences between randomly generated groups library(UsingR) x=c(6,11,15,18,29,33,34,46,49,73,102,12,13,14,23,26,33,59,59,69,75,78) mean(x[12:22])-mean(x[1:11]) # Take 100 random samples with two groups each from x and calculate the difference between the means of the two groups. diff=0 for(permutation in 1:100) { y=sample(x,22) diff[permutation]=mean(y[1:11])-mean(y[12:22]) } # Print and plot the differences between the randomly generated groups print(diff) sort(diff) hist(diff, main = "Histogram of differences between the means \n for 100 randomly generated groups", xlab="Difference between the means") plot(sort(diff), main = "Plot of differences between the means \n for 100 randomly generated groups", ylab="Difference between the means", xlab="Permutation (sorted)") abline(4.09,0, col="red",lty=2) abline(-4.09,0, col="red",lty=2) # Difference in the original data was 4.09. # What % of random differences are > 4.09? (Note that this is a one-tailed test.) sort(diff)>4.09 pctGT409=100*sum(sort(diff)>4.09)/length(diff) pctGT409 # of random differences are > 4.09 cat(pctGT409, "% of random differences are > 4.09 \n") # Two-tailed version # What % of random differences are > 4.09 or < -4.09? # abs(sort(diff))>4.09 abspctGT409=100*sum(abs(sort(diff))>4.09)/length(diff) abspctGT409 abline(-4.09,0, col="red",lty=2) # find the median diff quantile(diff,0.5) Suppose that 78 out of 100 permutations give us a difference between the means of A and B greater than the difference in the clinical data. If the null hypothesis is true (that there is no difference between Treatment A and Treatment B), then the probability that we would observe a difference between the means of A and B greater than the difference in the clinical data is 78/100 P=0.78. Suppose that 1 out of 100 permutations give us a difference between the means of A and B greater than the difference in the clinical data. If the null hypothesis is true (that there is no difference between Treatment A and Treatment B), then the probability that we would observe a difference between the means of A and B greater than the difference in the clinical data is 1/100 P=0.01.