function (delta.per.sigma=.5,t.treat=3, nrange=2:30,alpha=.05,power0=NULL) { # delta.per.sigma is the ratio of delta over sigma for which # one wants to detect a delta shift in one mean while all other # means stay the same, or delta is the maximum difference # between any two means to be detected. t.treat is the number of # treatments. alpha is the desired significance level. nrange is a # range of sample sizes over which the power will be calculated # for that delta.per.sigma. power0 is on optional value for the # target power that will be highlighted on the plot. #------------------------------------------------------------------- lambda0=((t.treat-1)/t.treat)*delta.per.sigma^2 lambda1=delta.per.sigma^2/2 power=NULL power1=NULL for(n in nrange){ N=n*t.treat Fcrit=qf(1-alpha,t.treat-1,N-t.treat) power=c(power,1-pf(Fcrit,t.treat-1,N-t.treat,n*lambda0)) power1=c(power1,1-pf(Fcrit,t.treat-1,N-t.treat,n*lambda1)) } plot(nrange,power,type="l",xlab=paste("minimum sample size n per each of t =", t.treat," treatments"), ylab="",ylim=c(0,1)) mtext(expression(beta(lambda)~"="~beta(n %*% lambda[0])),2,2.9,at=.6,col="red") mtext(expression(beta(lambda)~"="~beta(n %*% lambda[1])),2,2.9,at=.25,col="blue") abline(h=seq(0,1,.02),col="grey") abline(v=nrange,col="grey") lines(nrange,power,col="red",lwd=2) lines(nrange,power1,col="blue",lwd=2) title(substitute(over(Delta,sigma)==delta.per.sigma~","~alpha==alpha1~", "~lambda[0]==bgroup("(",over(Delta,sigma),")")^2 %*%over(t-1,t)~", "~ lambda[1]==over(1,2)%*%bgroup("(",over(Delta,sigma),")")^2, list(alpha1=alpha, delta.per.sigma=delta.per.sigma))) if(!is.null(power0)){ abline(h=power0,col="grey",lwd=2) par(las=2) mtext(substitute(beta==pow,list(pow=power0)),2,.2,at=power0,col="grey") par(las=0) } if(max(power1)>min(1-power)){ legend(x=c(mean(nrange)-.5*diff(range(nrange)), mean(nrange)-.1*diff(range(nrange))),y=c(max(power)*.2,max(power)*.05),expression("all "~mu[i]~"are same, except for one, differing by"~phantom(0) %+-% Delta, max(list("("~mu[1],ldots,mu[t])~")")-min("("~list(mu[1],ldots,mu[t])~")")==Delta),col=c("red","blue"),lty=c(1,1),bg="white") }else{ legend(x=c(mean(nrange)-.5*diff(range(nrange)),mean(nrange)-.1*diff(range(nrange))), y=c(max(power)+max(1-power)/2,max(power)+max(1-power)*.38),expression("all "~mu[i]~"are same, except for one, differing by"~phantom(0) %+-% Delta, max(list("("~mu[1],ldots,mu[t])~")")-min("("~list(mu[1],ldots,mu[t])~")")==Delta),col=c("red","blue"),lty=c(1,1),bg="white") } }