# Design safe test
alpha <- 0.05
beta <- 0.20
designObj <- designSafeT(1, alpha=alpha, beta=beta)
# Design frequentist test
freqObj <- designFreqT(1, alpha=alpha, beta=beta)
# Simulate under the alternative with deltaTrue=deltaMin
simResults <- replicateTTests(nPlan=designObj$nPlan, deltaTrue=1, parameter=designObj$parameter,
nPlanFreq=freqObj$nPlan, beta=beta, nSim=250)
# Should be about 1-beta
simResults$safeSim$powerAtN1Plan
# This is higher due to optional stopping
simResults$safeSim$powerOptioStop
# Optional stopping allows us to do better than n1PlanFreq once in a while
simResults$safeSim$probLeqN1PlanFreq
graphics::hist(simResults$safeSim$allN, main="Histogram of stopping times", xlab="n1",
breaks=seq.int(designObj$nPlan[1]))
# Simulate under the alternative with deltaTrue > deltaMin
simResults <- replicateTTests(nPlan=designObj$nPlan, deltaTrue=1.5, parameter=designObj$parameter,
nPlanFreq=freqObj$nPlan, beta=beta, nSim=250)
# Should be larger than 1-beta
simResults$safeSim$powerAtN1Plan
# This is even higher due to optional stopping
simResults$safeSim$powerOptioStop
# Optional stopping allows us to do better than n1PlanFreq once in a while
simResults$safeSim$probLeqN1PlanFreq
graphics::hist(simResults$safeSim$allN, main="Histogram of stopping times", xlab="n1",
breaks=seq.int(designObj$nPlan[1]))
# Under the null deltaTrue=0
simResults <- replicateTTests(nPlan=designObj$nPlan, deltaTrue=0, parameter=designObj$parameter,
nPlanFreq=freqObj$nPlan, freqOptioStop=TRUE, beta=beta, nSim=250)
# Should be lower than alpha, because if the null is true, P(S > 1/alpha) < alpha for all n
simResults$safeSim$powerAtN1Plan
# This is a bit higher due to optional stopping, but if the null is true,
# then still P(S > 1/alpha) < alpha for all n
simResults$safeSim$powerOptioStop
# Should be lowr than alpha, as the experiment is performed as was planned
simResults$freqSim$powerAtN1Plan
# This is larger than alpha, due to optional stopping.
simResults$freqSim$powerOptioStop
simResults$freqSim$powerOptioStop > alpha
Run the code above in your browser using DataLab