lines = 'Noise Duration Experience 0 11.5 4 0 9.5 7 20 12.0 3 20 13.0 2 40 15.0 11 40 16.0 8 60 20.0 5 60 19.0 7 80 28.0 8 80 27.0 10' con = textConnection( lines ) DF = read.table( con, header=TRUE ) close(con) DF$Noise = as.factor( DF$Noise ) compute_statistic = function( DF ){ # # Computes the statistic as discussed in the solutions. # noise_groups = unique( DF$Noise ) nng = length(noise_groups) S = 0. for( ng in 1:nng ){ mask = DF$Noise == noise_groups[ng] df = DF[mask,] S = S + sum( DF$Duration * DF$Experience ) } return(S) } # Next lets apply a permutation test: # S = compute_statistic( DF ) # the value of S under the original ordering set.seed(1234) B = 100 # the sumber of bootstraps to run all_Ss = rep( NA, B ) # the bootstrap values of the F_1 for( bi in 1:B ){ DF$Duration = sample(DF$Duration) # reorder the labels all_Ss[bi] = compute_statistic( DF ) } plot( density( all_Ss ), xlab="statistic", main="" ) grid() abline(v=S, col='red') alpha = sum( all_Ss >= S ) / B print( sprintf("Permutation Bootstrap (B= %d): prob. type I error: %10.6f", B, alpha) )