epl2012 <- read.csv("http://www.stat.ufl.edu/~winner/data/epl_2012_home_perm.csv", header=T) attach(epl2012); names(epl2012) ### Obtain Sample Size and Test Statistic (Average of d.jk) (n <- length(d.jk)) (TS.obs <- mean(d.jk)) ### Choose the number of samples and initialize TS, and set seed N <- 9999; TS <- rep(0,N); set.seed(86420) ### Loop through samples and compute each TS for (i in 1:N) { ds.jk <- d.jk # Initialize d*.jk = d.jk u <- runif(n)-0.5 # Generate n U(-0.5,0.5)'s u.s <- sign(u) # -1 if u.s < 0, +1 if u.s > 0 ds.jk <- u.s * ds.jk TS[i] <- mean(ds.jk) # Compute Test Statistic for this sample } summary(TS) (num.exceed1 <- sum(TS >= TS.obs)) # Count for 1-sided (Upper Tail) P-value (num.exceed2 <- sum(abs(TS) >= abs(TS.obs))) # Count for 2-sided P-value (p.val.1sided <- (num.exceed1 + 1)/(N+1)) # 1-sided p-value (p.val.2sided <- (num.exceed2 + 1)/(N+1)) # 2-sided p-value ### Draw histogram of distribution of TS, with vertical line at TS.obs hist(TS,xlab="Mean Home-Away",main="Randomization Distribution for EPL 2012 Home Field Advantage") abline(v=TS.obs)