#Initialize vectors p1hat and p2hat: p1hat <- rep(0,1000) p2hat <- rep(0,1000) #Data from sample 1: x1 <- c("yes", "yes", "yes", "yes","yes", "no", "no", "no", "yes","no") #Data from sample 2: x2 <- c("yes", "yes", "no", "no","no", "no", "no", "no", "no","no") #We want to test H0: p1-p2=0 Vs. Ha: p1-p2 != 0. #Under H0 we treat the data as one sample: x3 <- c(x1,x2) #We will shuffle the data in x3 and randomly select n1=10 each time to compute the simulated p1_hat. Similarly we compute p2_hat: #A for loop that will split the data into two parts: for(i in 1:1000){ y1 <- sample(x3, 10) p1hat[i] <- sum(y1=="yes")/10 p2hat[i] <- (sum(x3=="yes")-sum(y1=="yes"))/10 } #Construct a histogram using the 1000 simulated p1_hat-p2_hat: pdiff_hat <- p1hat - p2hat hist(pdiff_hat, breaks=seq(-1, 1,.20)) #Find p1_hat-p2_hat from the original data: pdiff_hat_orig <- sum(x1=="yes")/10 - sum(x2=="yes")/10 #Place it on the histogram: segments(pdiff_hat_orig ,0,pdiff_hat_orig ,300, col="green") #Compute p-value: 2*sum(pdiff_hat > pdiff_hat_orig)/1000