//Statistical power calculations online. For two means, considering 
//sample size and alpha levels, usefull for randomized controlled 
//trials (RCT)
//copyright: Daan G Uitenbroek PhD

function truncate(real,places)  // truncates 'real' numbers n 'places' behind the decimal dot  
{  
var multi=Math.pow(10,places);  
return (Math.round(real*multi)/multi)  
}  
  
function addspace(real,lenght)   
{  
var str=""+real;  
for (var index=str.length;index<=lenght;index++)  
      str=" "+str;  
var pt=str.indexOf(".");  
if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations  
return str;  
}  
  
function array(len)  
{  
  this.length=len;  
   for (var i=1;i<=len;i++)  
      this[i]=0;  
  return this  
}  
  
function zprob(zval)   
{ 
if ((zval>5)||(zval<-5)) outp=0.9999999999 
else 
{ 
if (zval == 0.0)      
     var x  = 0.0;         
else       
     {   
     var y = 0.5 * Math.abs (zval);         
     if (y >= (10000* 0.5))         
             x=1.0;   
     else if (y < 1.0)          
     {          
     var w = y*y;           
     x = ((((((((0.000124818987 * w   
          -0.001075204047) * w +0.005198775019) * w   
          -0.019198292004) * w +0.059054035642) * w   
          -0.151968751364) * w +0.319152932694) * w   
          -0.531923007300) * w +0.797884560593) * y * 2.0;      
 
     
     }          
     else       
     {   
     y -= 2.0;          
     x  = (((((((((((((-0.000045255659 * y   
          +0.000152529290) * y -0.000019538132) * y   
          -0.000676904986) * y +0.001390604284) * y   
          -0.000794620820) * y -0.002034254874) * y   
          +0.006549791214) * y -0.010557625006) * y   
          +0.011630447319) * y -0.009279453341) * y   
          +0.005353579108) * y -0.002141268741) * y   
          +0.000535310849) * y +0.999936657524*1;       
     }           
     }   
var outp=((zval > 0.0 ? ((x*1+1.0*1) * 0.5) : ((1.0 -x) * 0.5)))  
if ((outp>1)||(outp<-1)) (outp=1)  
} 
return (1-outp)  
}  
 
function alloratio(p1,p2,type)  //calculates the optimum allocation ratio 
{  
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";  
 
if (type<2) 
var theta=Math.sqrt((p2*(1-p2))/(p1*(1-p1))) 
else 
var theta=Math.sqrt((p2*p2)/(p1*p1)) 
 
theta=Math.round(theta*100)/100 
return theta 
} 
  
function numofcases(p1,p2,std1,std2,N,alfa,alloc,checkbox)  
{  
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";  
  
   // document.form1.outp.value+=writeln+p1+"   "+p2+"   "+std1+"   "+std2+" "+N+"   "+alfa+"   "+alloc    


var delta=Math.abs(p1-p2);  
if (std1==0)  
{  
   var phat=((1*p1+alloc*p2)/(1*1+1*alloc));  
   //   document.form1.outp.value+=writeln+"1"  
   var zbeta=((delta*Math.sqrt(alloc*N)-(alfa*Math.sqrt((1*1+alloc*1)*phat*(1-phat))
))/Math.sqrt((alloc*p1*(1-p1))+(p2*(1-p2))))  
}  
else  
{   
   if (std2!=0) std1=(std1*1+std2*1)/2
   var N2=2*alloc*N/(alloc*1+1*1);
   delta=(delta/std1);  
   var zbeta=delta*Math.sqrt(N2/2-(alfa*alfa/8))-alfa  
}  
return zbeta  
}  
  
function eqnum(p1,p2,toll,alfa,std,N,alloc)  
{  
if (std==0)  
var
zbeta=Math.sqrt(N*alloc)*Math.abs(Math.abs(p1*1-p2*1)-toll)/Math.sqrt(alloc*p1*(1-p1)+(p2*(1-p2)))-alfa  
else  
var zbeta=Math.abs(((Math.abs(p2*1-p1*1)-toll)/std))*Math.sqrt(N*alloc/(1*1+alloc*1))-alfa  
return zbeta          
}  
 
function pairw (p1,p2,sd1,sd2,N,alfa,alloc)  //this one from samps  
{  
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";  
 
if ((sd1==0) & (sd2==0))  
{ 
if (alloc>(50*1))  
 {var pp1=(p1*(1-p2*1)) 
  var pp2=(p2*(1-p1*1))} 
  else 
 {var pp1=p1 
  var pp2=p2} 
 
var ratio=(pp1/pp2) 
var piedisc=(pp1*1+pp2*1) 
 
var denom=((Math.sqrt((ratio-1)*(ratio-1)*N*piedisc))-(alfa*(ratio+1*1))); 
numer=Math.sqrt(((ratio+1*1)*(ratio+1*1))-((ratio-1)*(ratio-1)*piedisc)) 
var outp=denom/numer 
}  
 
else  
 
{if (sd1==0) sd1=sd2 
 var delta=p1/sd1;  
   if (sd2==0) {var zbeta=delta*Math.sqrt(N)-alfa} 
       else {var zbeta=delta*Math.sqrt(N-(alfa*alfa/2))-alfa}  
 var outp=zbeta*1  
} 
return outp  
}  //this one from samps  
  
function popul(p1,p2,sd1,sd2,N,alfa)  
{  
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";  
  
var delta=Math.abs(p1-p2);  
if ((sd1==0) & (sd2==0))  
var zbeta=(delta*Math.sqrt(N)-(alfa*Math.sqrt(p1*(1-p1))))/Math.sqrt(p2*(1-p2)) 

else  
{   
   if (sd1==0) {sd1=sd2*1}  
   delta=(delta/sd1);  
   if (sd2==0) {var zbeta=delta*Math.sqrt(N)-alfa} 
       else {var zbeta=delta*Math.sqrt(N-(alfa*alfa/2))-alfa}  
}  
return zbeta  
}  
  
function samplesize(p1,p2,std1,std2,N,alloc,type)    //MAIN  
{  
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";  
  
peas=new array(10);  
tail=new array(10);  
  
peas[1]=1.283; //start blok for double sided  
tail[1]=0.20;  
peas[2]=1.6449;   
tail[2]=0.10;  
peas[3]=1.960;  
tail[3]=0.05;  
peas[4]=2.576;  
tail[4]=0.01;  
peas[5]=3.291;  
tail[5]=0.001;  
peas[6]=0.847; //start blok for single sided  
tail[6]=0.20;  
peas[7]=1.282;   
tail[7]=0.10;  
peas[8]=1.6449;  
tail[8]=0.05;  
peas[9]=2.326;  
tail[9]=0.01;  
peas[10]=3.090;  
tail[10]=0.001;  
  
var typecorrect=0  
  
if (type==2)  
{  
if (p2==0) p2=p1  
document.form1.outp.value+="\t   EQUALITY ANALYSIS"+writeln  
typecorrect=1  
}  
if (type==3) document.form1.outp.value+="\t   POPULATION ANALYSIS"+writeln 
if (type==4) {document.form1.outp.value+="\t   PAIRWISE ANALYSIS"+writeln} 
  
document.form1.outp.value+="\tRESULTS for double sided"+writeln  
document.form1.outp.value+="alpha\tz-power\tpower\t   %"+writeln  
  
if ((p1<1)|(p2<1))   // prepares the analysis  
  if ((p2>1)|(p1>1))    
     document.form1.outp.value+=  
         "WARNING one average is a proportion other one is a number"+writeln; 

  
if ((type!=2)&((p1>=1)&(std1==0))&(type!=3)&(type!=4)) //work out the standardeviations in this block  
{  
std1=Math.sqrt(p1)  
document.form1.outp.value+=  
     "WARNING average one presumed Poisson distributed"+writeln;  
if (std2==0) {std2=Math.sqrt(p2)  
     document.form1.outp.value+=  
         "WARNING average two presumed Poisson distributed"+writeln;}  
}  
if ((type==2)&(p1>=1)&(std2==0)) std2=Math.sqrt(p1+p2)  
  
document.form1.outp.value+="-------------------------------------------------"+writeln;  
  
// end of preperation start of calculations  
  
for (var index=2;index<=5;index++)  //this for statement does clasical model  
   {str=""+tail[index-typecorrect]  
     pt=str.indexOf(".");  
     if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations  
    document.form1.outp.value+=str+"\t"  
         if (type==1)   
            zvalue=numofcases(p1,p2,std1,std2,N,peas[index],alloc);  
          if (type==2)   
            zvalue=eqnum(p1,p2,std1,peas[index-1],std2,N,alloc);  
          if (type==3)   
            zvalue=popul(p1,p2,std1,std2,N,peas[index]); 
          if (type==4)   
            zvalue=pairw(p1,p2,std1,std2,N,peas[index],alloc);     
            tvalue=1-zprob(zvalue)  
         
document.form1.outp.value+=truncate(zvalue,3)+"\t"+truncate(tvalue,4)+"\t"+truncate(tvalue*100,2)  
     document.form1.outp.value+=writeln;  
   }  
document.form1.outp.value+=writeln;       //end of double sided output  
  
if (type==2) document.form1.outp.value+="\t   EQUALITY ANALYSIS"+writeln  
if (type==3) document.form1.outp.value+="\t   POPULATION ANALYSIS"+writeln 
if (type==4) {document.form1.outp.value+="\t   PAIRWISE ANALYSIS"+writeln} 
  
document.form1.outp.value+="\tRESULTS for single sided"+writeln  
document.form1.outp.value+="alpha\tz-power\tpower\t   %"+writeln  
  
document.form1.outp.value+="-------------------------------------------------"+writeln;  
  
for (var index=7;index<=10;index++)  
   {str=""+tail[index-typecorrect]  
     pt=str.indexOf(".");  
     if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations  
    document.form1.outp.value+=str+"\t"  
          if (type==1)   
            zvalue=numofcases(p1,p2,std1,std2,N,peas[index],alloc);  
          if (type==2)   
            zvalue=eqnum(p1,p2,std1,peas[index-1],std2,N,alloc);  
          if (type==3)   
            zvalue=popul(p1,p2,std1,std2,N,peas[index]);  
          if (type==4)   
            zvalue=pairw(p1,p2,std1,std2,N,peas[index],alloc);  
          tvalue=1-zprob(zvalue)  
         
document.form1.outp.value+=truncate(zvalue,3)+"\t"+truncate(tvalue,4)+"\t"+truncate(tvalue*100,2)  
document.form1.outp.value+=writeln;   
} 
if ((type==1) & (p1<1)) 
    document.form1.outp.value+=writeln+"optimum allocation ratio equals: "+addspace(alloratio(p1,p2,1),4) 
if ((type==1) & (p1>=1)) 
    document.form1.outp.value+=writeln+"optimum allocation ratio equals: "+addspace(alloratio(std1,std2,3),4) 
document.form1.outp.value+=writeln;   //end of single sided output  
}  
  
function ClearOutp()  
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";   
document.form1.outp.value ="cleared"+writeln;  
}  

