adapt1 <- function(simulateP,nsims,postcut,hypothesisP,nCuts){ win <- logical(nsims) ss <- numeric(nsims) nInts <- nCuts for (i in 2:length(nCuts)){ nInts[i] <- nCuts[i] - nCuts[i-1] } for (i in 1:nsims){ x <- rbinom(length(nCuts),nInts,simulateP) x <- c(x[1],x[1]+x[2],x[1]+x[2]+x[3]) ProbSup <- 1 - pbeta(hypothesisP,1+x,1+nCuts-x) # Probability of Success win[i] <- any(ProbSup > postcut ) # Sample size ss[i] <- min(nCuts[ProbSup > postcut],max(nCuts)) } out <- c(length(win[win])/nsims,mean(ss), sqrt(var(ss)),table(ss)/nsims) names(out) <- c('Pr(win)','MeanSS','SD SS',as.character(nCuts)) out } ## Input these values for the Type I error of Example 5.2.1: > nsims <- 1000000 > postcut <- 0.95 > hypothesisP <- 0.5 > simulateP <- 0.5 > nCuts <- c(50,75,100) > out <- adapt1(simulateP,nsims,postcut,hypothesisP,nCuts) > out Pr(win) MeanSS SD SS 50 75 100 0.095770 96.455625 12.247579 0.059165 0.023445 0.917390