# logic of hypothesis test # demo of a permutation test library(ISwR) data(energy) energy help(energy) by(energy$expend, energy$stature, mean) # equivalent to: tapply(energy$expend, energy$stature, mean) obs.diff<-diff(by(energy$expend, energy$stature, mean)) # illustration of a single permutation sample(1:22) cbind(energy[sample(1:22),]$expend, energy$stature) # alternatively, the stature column could be permuted # 1000 permutations perm.diff<-numeric(1000) for(i in 1:1000){ perm<-sample(1:22) perm.diff[i]<-diff(by(energy[perm,]$expend, energy$stature, mean)) } hist(perm.diff, label=T) abline(v=obs.diff) sort(perm.diff) sum(perm.diff>obs.diff) quantile(perm.diff, prob=0.95) quantile(perm.diff, prob=0.995) #How does the output of the permutation test above compare to the results of a t-test? t.test(expend ~ stature, data=energy)