power.curve_function(s,alpha,n.seq,range.mudiff) { ###A function to compute a power curve for a two-sided hypothesis test. ##Assume equal sample sizes and equal variances ##n.seq is a sequence of n's considered; n is the number in *each* group ##range.mudiff is the range of treatment effects to consider ##s is standard deviation ##alpha is significance level for the test (type I error) mudiff.seq_seq(from=range.mudiff[1],to=range.mudiff[2],length=20) plot.mat_matrix(0,length(mudiff.seq),(length(n.seq)+1)) plot.mat[,1]_mudiff.seq for (i in 1:length(n.seq)){ plot.mat[,(i+1)]_pnorm(-qnorm(1-alpha/2)+sqrt(n.seq[i])*mudiff.seq/(sqrt(2)*s)) #This built in S function gives the same answer, but is much slower. #for (j in 1:length(mudiff.seq)){ #plot.mat[j,(i+1)]_normal.sample.size(mean=0,mean2=mudiff.seq[j],n1=n.seq[i],prop.n2=1,sd1=s,sd2=s,alpha=alpha)$power} } plot(plot.mat[,1],plot.mat[,2],type="n",ylim=c(0,1),xlab="diff. in means", ylab="power") for (i in 2:(length(n.seq)+1)){ lines(plot.mat[,1],plot.mat[,i],lty=(i-1))} text(min(plot.mat[,1]),.99,adj=0,"Num. per group") legend(min(plot.mat[,1]),.97,legend=as.character(n.seq),lty=1:length(n.seq)) }