//T-test online. Including odds-ratioos, risk-ratio's, and number 
//needed to treat (NNT)
//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 cut(real,lenght)  // changes scientific into normal notation and makes to size
{
var str=""+real;
var r="0.";
var pt=str.indexOf("e");
if (pt>0 ) 
    {
     var a=str.substring(0,1)+str.substring(2,pt+1);
     for (var index=1;index<=(str.substring(pt+2,pt+5)*1-1);index++)
        r+=0;
     var st=r+a;
      for (var index=st.length;index<=lenght;index++)
         st+="0";
     return (st.substring(0,lenght));
     };
for (var index=str.length;index<=lenght;index++)
      str+="0";
pt=str.indexOf(".");
if (pt==0) str="0"+str           //solves that newer versions of Netscape give inconsistent numeric notations
return str.substring(0,lenght);
}

function odt(len1)   // to create a onedimensional table
{
  this.lenght=len1;
   for (var i=1;i<=len1;i++)
      this[i]=0;
  return this
}

function table(len1,len2)   // to create a two dimensional table
{
   this.lenght=len1;
      for (var j=1;j<=len1;j++)
           this[j]=new odt(len2);
    return this
}

function currentCI()
{
this.interval="95"
this.intzval=1.96
this.upper=0.975
this.lower=0.025
this.chi2=3.841 //0.00393
}

function CI(length)
{
this.length=length
   for (var j=1;j<=length;j++)
      this[j]=new currentCI();

this[1].interval="80"
this[1].intzval=1.282
this[1].upper=0.9
this[1].lower=0.1
this[1].chi2=1.642  //0.0642
this[2].interval="90"
this[2].intzval=1.645
this[2].upper=0.95
this[2].lower=0.05
this[1].chi2=2.706     //0.0158
this[3].interval="95"
this[3].intzval=1.96
this[3].upper=0.975
this[3].lower=0.025
this[1].chi2=3.841  //0.00393
this[4].interval="99"
this[4].intzval=2.575
this[4].upper=0.995
this[4].lower=0.005
this[1].chi2=10.827  //0.000157
}

function changeCI(prop)
{
currentoption=prop.selectedIndex;
currentoption+=1*1;
CIcurrent=allCI[currentoption];
}

//following are statements to innitialize the confidence interval
currentoption=3  //3 is for 95 CI
CIcurrent=new currentCI()
allCI=new CI(4)  //because we allow for four different Confidence Intervals
 
function simplevar(p1,N)
{
var variance=p1*(1-p1)/N
return variance
}

function differencevar(p1,p2,N1,N2)
{
var variance=(p1*(1-p1)/N1)*1+(p2*(1-p2)/N2)*1
return variance
}

function odd(number)    //returns zero if number is not odd, one if it is
{
var isodd=0*1;
if (Math.round(number/2+0.00001)==number/2)  isodd=1*1;
return isodd
}

function c2prob(chi2,df)
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

var pval=0;

if (chi2<1000)
{
var add=0;
var mult=1;
if (odd(df)<0.5)
 {
      add=1;
      mult=Math.sqrt(2/chi2/Math.PI);
   }
var denominator=1;
for (var index=df;index>1;index-=2)
         denominator=denominator*index;
var numerator=Math.pow(chi2,((df*1+add*1)/2))*Math.exp(chi2*-1/2);
sum=1*1;
m=1*1;

for (var count=df*1+2*1;m>0.00000000001;count+=2)
   {
      m=(m*chi2)/count;
      sum=sum*1+m*1;
   }
pval=1-sum*mult*numerator/denominator
}
return pval
}

function rprob(corr,n)
{
return c2prob((corr*corr*n),1)
}

function Chi2(dat1,dat2,dat3,dat4,n)
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

data=new table(3,3)
data[1][1]=dat1*1
data[1][2]=dat2*1
data[2][1]=dat3*1
data[2][2]=dat4*1
data[1][3]=data[1][1]*1+data[1][2]*1;
data[2][3]=data[2][1]*1+data[2][2]*1;
data[3][2]=data[1][2]*1+data[2][2]*1;
data[3][1]=data[1][1]*1+data[2][1]*1;    // make marginals

exptable=new table(2,2)
exptable[1][1]=
   (data[1][1]+data[1][2])*(data[1][1]+data[2][1])/(n);
exptable[1][2]=
   (data[1][2]+data[1][1])*(data[1][2]+data[2][2])/(n);
exptable[2][1]=
   (data[2][1]+data[1][1])*(data[2][1]+data[2][2])/(n);
exptable[2][2]=
   (data[2][2]+data[1][2])*(data[2][2]+data[2][1])/(n);

var chi2=1*0;
var lrx=1*0;

for (var index=1;index<3;index++) 
 for (var jndex=1;jndex<3;jndex++)    
{
  chi2=chi2+(((data[index][jndex]*data[index][jndex])
              -(exptable[index][jndex]*exptable[index][jndex]))
              /exptable[index][jndex]);
 var lrx=lrx+(data[index][jndex]*(Math.log(data[index][jndex])
            -Math.log(exptable[index][jndex])));
}

lrx=lrx*2;

var Yates=((data[1][1]*data[2][2])-(data[2][1]*data[1][2]))
if (Yates<0) (Yates=Yates*-1)
Yates=Yates-0.5*n
Yates=Yates*Yates
Yates=Yates/(data[3][1]*data[2][3]*data[3][2]*data[1][3])*n;


var Mantel=chi2/n*(n-1);
document.form1.outp.value+=
 "Chi squares (all with 1 (one) degree of freedom):"+writeln
 +"Pearson's= "+truncate(chi2,3)+" (p= "+cut(c2prob(chi2,1),6)+") (GFX)"+writeln
 +"Likelihood Ratio= "+truncate(lrx,3)+" (p= "+cut(c2prob(lrx,1),6)+") (LRX)"+writeln
 +"Yate's= "+truncate(Yates,3)+" (p= "+cut(c2prob(Yates,1),6)+") (cont corr GFX)"+writeln
 +writeln+"Pearson's correlation: "+cut(Math.sqrt(chi2/n),7)+" (p="
 +cut(rprob(Math.sqrt(chi2/n),n),6)+")"+writeln
}

function DoTest(x11,x12,x21,x22,prevalence,Num)     // THE MAIN PROCEDURE THE MAIN PROCEDURE
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

var partdata=false
if ((x11<1)&&(x11>0)) 
{partdata=true
 sensi=x11*1
 speci=x22}

var NN=Num
if ((partdata==true)&&(prevalence>0)&&(NN>1))
{
x11=sensi*prevalence
x22=speci*(1-prevalence)
x21=prevalence-x11
x12=1-prevalence-x22
document.form1.outp.value+=" Table of proportions"+writeln
document.form1.outp.value+=truncate(x11*1,5)+"\t"+truncate(x12*1,5)+":\t"+truncate(x11*1+x12*1,5)+" tested positive"+writeln
document.form1.outp.value+=truncate(x21*1,5)+"\t"+truncate(x22*1,5)+":\t"+truncate(x21*1+x22*1,5)+" tested negative"+writeln+writeln
x11=Math.round(x11*NN)
x22=Math.round(x22*NN)
x21=Math.round(x21*NN)
x12=Math.round(x12*NN)
document.form1.outp.value+="   Reconstituted Table"+writeln
partdata=false
prevalence=0
}
 
if (partdata==false)
{
var N1x=x11*1+x21*1
var N2x=x12*1+x22*1
var Nx1=x11*1+x12*1
var Nx2=x21*1+x22*1
var sensi=x11/N1x
var speci=x22/N2x
var NN=N1x*1+N2x*1

document.form1.outp.value+="   The Table"+writeln
document.form1.outp.value+=x11*1+"\t"+x12*1+":\t"+(x11*1+x12*1)+" tested positive"+writeln
document.form1.outp.value+=x21*1+"\t"+x22*1+":\t"+(x21*1+x22*1)+" tested negative"+writeln
document.form1.outp.value+="In total "+NN+" observations"+writeln+writeln

var variance=simplevar(x11/NN,NN)
var lower=Wilson(x11/NN,NN,CIcurrent.intzval,-1)
var upper=Wilson(x11/NN,NN,CIcurrent.intzval,+1)
document.form1.outp.value+="True positives eq: "+x11*1+"/"+NN+": proportion: "+truncate(x11/NN,3)+writeln+"Variance proportion: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((x11/NN-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<TP<"
                         +truncate((x11/NN+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<TP<"+ truncate(upper*1,3)+writeln+writeln 
var variance=simplevar(x22/NN,NN)
var lower=Wilson(x22/NN,NN,CIcurrent.intzval,-1)
var upper=Wilson(x22/NN,NN,CIcurrent.intzval,+1)
document.form1.outp.value+="True negatives eq: "+x22*1+"/"+NN+": proportion: "+truncate(x22/NN,2)+writeln+"Variance proportion: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((x22/NN-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<TN<"
                         +truncate((x22/NN+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<TN<"+ truncate(upper*1,3)+writeln+writeln 
var variance=simplevar(x12/NN,NN)
var lower=Wilson(x12/NN,NN,CIcurrent.intzval,-1)
var upper=Wilson(x12/NN,NN,CIcurrent.intzval,+1)
document.form1.outp.value+="False positives eq: "+x12*1+"/"+NN+": proportion: "+truncate(x12/NN,2)+writeln+"Variance proportion: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((x12/NN-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<FP<"
                         +truncate((x12/NN+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<FP<"+ truncate(upper*1,3)+writeln+writeln 
var variance=simplevar(x21/NN,NN)
var lower=Wilson(x21/NN,NN,CIcurrent.intzval,-1)
var upper=Wilson(x21/NN,NN,CIcurrent.intzval,+1)
document.form1.outp.value+="False negatives eq: "+x21*1+"/"+NN+": proportion: "+truncate(x21/NN,2)+writeln+"Variance proportion: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((x21/NN-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<FN<"
                         +truncate((x21/NN+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<FN<"+ truncate(upper*1,3)+writeln+writeln 
var variance=simplevar((x11*1+x22*1)/NN,NN)
var lower=Wilson((x11*1+x22*1)/NN,NN,CIcurrent.intzval,-1)
var upper=Wilson((x11*1+x22*1)/NN,NN,CIcurrent.intzval,+1)
document.form1.outp.value+="Accuracy eq: "+ truncate(x11*1+x22*1,3)+"/"+NN+": proportion: "+ truncate((x11*1+x22*1)/NN,2)+writeln+"Variance proportion: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate(((x11*1+x22*1)/NN-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<Ac<"
                         +truncate(((x11*1+x22*1)/NN+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<Ac<"+ truncate(upper*1,3)+writeln+writeln 
somdiag=x11*1+x22*1
somarg=N1x*Nx1+(N2x*Nx2)
kappa=(somdiag-(somarg/NN))/(NN-(somarg/NN));
somarg2=(N1x*Nx1*(N1x+Nx1))+(N2x*Nx2*(N2x+Nx2))
PC=somarg/(NN*NN);
variance=Math.sqrt(PC+(PC*PC)-(somarg2/(NN*NN*NN)));
variance=variance/((1-PC)*Math.sqrt(NN));
variance=variance*variance
document.form1.outp.value+="Kappa measure of agreement: "+truncate(kappa,3)+writeln+"      ("+somdiag+"-("+somarg+"/"+NN+"))/("+NN+"-("+somarg+"/"+NN+"))"
                         +writeln+"Variance: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((kappa-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<Kappa<"
                         +truncate((kappa+CIcurrent.intzval*Math.sqrt(variance))*1,3)+writeln+writeln
var variance=simplevar(sensi,N1x)
var lower=Wilson(sensi,N1x,CIcurrent.intzval,-1)
var upper=Wilson(sensi,N1x,CIcurrent.intzval,+1)
document.form1.outp.value+="Sensitivity: "+ truncate(sensi,4)+" ("+x11*1+"/"+N1x+")"+writeln+"Variance: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((sensi-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<Se<"
                         +truncate((sensi+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<Se<"+ truncate(upper*1,3)+writeln+writeln 
var variance=simplevar(speci,N2x)
var lower=Wilson(speci,N2x,CIcurrent.intzval,-1)
var upper=Wilson(speci,N2x,CIcurrent.intzval,+1)
document.form1.outp.value+="Specificity: "+ truncate(speci,4)+" ("+x22*1+"/"+N2x+")"+writeln+"Variance: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((speci-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<Sp<"
                         +truncate((speci+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<Sp<"+ truncate(upper*1,3)+writeln+writeln 
var ratio=sensi/(1-speci);
var variance=Math.sqrt((1/x11)-(1/N1x)+(1/x12)-(1/N2x))
var lower=Math.exp(Math.log(ratio)-(variance*CIcurrent.intzval))
var upper=Math.exp(Math.log(ratio)+(variance*CIcurrent.intzval))
//document.form1.outp.value+="x11="+x11+writeln 
document.form1.outp.value+="Positive Likelihood: "+ truncate(ratio,3)+" ("+truncate(sensi,3)+"/(1-"+truncate(speci,3)+"))"+writeln
                         +CIcurrent.interval+"% CI: "+truncate(lower*1,3)+"<PL<"+ truncate(upper*1,3)+writeln+writeln 
var ratio=((1-sensi)/speci);
var variance=Math.sqrt((1/x21)-(1/N1x)+(1/x22)-(1/N2x))
var lower=Math.exp(Math.log(ratio)-(variance*CIcurrent.intzval))
var upper=Math.exp(Math.log(ratio)+(variance*CIcurrent.intzval))
//document.form1.outp.value+="x21="+x21+writeln 
document.form1.outp.value+="Negative Likelihood: "+ truncate(ratio,3)+" (1-"+truncate(sensi,3)+")/"+truncate(speci,3)+writeln
                         +CIcurrent.interval+"% CI: "+truncate(lower*1,3)+"<NL<"+ truncate(upper*1,3)+writeln+writeln 
var ratio=(sensi/(1-sensi))/((1-speci)/speci)
var variance=1/x22+1/x12+1/x21+1/x11;
variance2=variance*ratio*ratio; //(old not so good method to estimate standard error) 
variance=Math.sqrt(variance)
var lower=Math.exp(Math.log(ratio)-(variance*CIcurrent.intzval))
var upper=Math.exp(Math.log(ratio)+(variance*CIcurrent.intzval))
document.form1.outp.value+="Diagnostic Odds Ratio: "+ truncate(ratio,3)+writeln+"        {"+truncate(sensi,3)+"/(1-"+truncate(sensi,3)+")/((1-"+truncate(speci,3)+")/"+truncate(speci,3)+")}"+writeln+"Variance: "+truncate(variance2,5)+"; Std.Err: "+truncate(Math.sqrt(variance2),5)
     +writeln+CIcurrent.interval+"% CI: "+truncate((ratio-CIcurrent.intzval*Math.sqrt(variance2))*1,2)+"<OR<"
     +truncate((ratio+CIcurrent.intzval*Math.sqrt(variance2))*1,2)+"; Wald CI: "+truncate(lower*1,2)+"<OR<"+ truncate(upper*1,2)+writeln+writeln 
var ratio=(sensi/(1-sensi))/(speci/(1-speci))
var variance=1/x22+1/x12+1/x21+1/x11;
variance2=variance*ratio*ratio; //(old not so good method to estimate standard error) 
variance=Math.sqrt(variance)
var lower=Math.exp(Math.log(ratio)-(variance*CIcurrent.intzval))
var upper=Math.exp(Math.log(ratio)+(variance*CIcurrent.intzval))
document.form1.outp.value+="Error Odds Ratio: "+ truncate(ratio,3)+writeln+"        {"+truncate(sensi,3)+"/(1-"+truncate(sensi,3)+")/("+truncate(speci,3)+"/(1-"+truncate(speci,3)+"))}"+writeln+"Variance: "+truncate(variance2,5)+"; Std.Err: "+truncate(Math.sqrt(variance2),5)
     +writeln+CIcurrent.interval+"% CI: "+truncate((ratio-CIcurrent.intzval*Math.sqrt(variance2))*1,2)+"<OR<"
     +truncate((ratio+CIcurrent.intzval*Math.sqrt(variance2))*1,2)+"; Wald CI: "+truncate(lower*1,2)+"<OR<"+ truncate(upper*1,2)+writeln+writeln 
var variance=differencevar(sensi,speci,N1x,N2x)
document.form1.outp.value+="Youden's J: "+ truncate(speci*1+sensi*1-1,3)+" ("+truncate(speci,3)+"+"+truncate(sensi,3)+"-1"+")"+writeln+"Variance: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((speci*1+sensi*1-1-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<J<"
                         +truncate((speci*1+sensi*1-1+CIcurrent.intzval*Math.sqrt(variance))*1,3)+writeln+writeln 
preval=(x11*1+x21*1)/NN
var variance=simplevar(preval,NN)
var lower=Wilson(preval,NN,CIcurrent.intzval,-1)
var upper=Wilson(preval,NN,CIcurrent.intzval,+1)
document.form1.outp.value+="Prevalence eq: "+ truncate(x11*1+x21*1,3)+"/"+NN+": proportion: "+ truncate(preval,4)+writeln+"Variance proportion: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)
                         +writeln+CIcurrent.interval+"% CI: "+truncate((preval-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<pr<"
                         +truncate((preval+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<pr<"+ truncate(upper*1,3)+writeln+writeln 
var pospred=x11/Nx1
variance=simplevar(pospred,Nx1)
var lower=Wilson(pospred,Nx1,CIcurrent.intzval,-1)
var upper=Wilson(pospred,Nx1,CIcurrent.intzval,+1)
document.form1.outp.value+="Positive predictive accuracy: "+truncate(pospred,3)+" ("+x11*1+"/"+Nx1+")"+writeln+"Variance: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((pospred-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<pp<"
                         +truncate((pospred+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<pp<"+truncate(upper*1,3)+writeln+writeln 
var negpred=x22/Nx2
variance=simplevar(negpred,Nx2)
var lower=Wilson(negpred,Nx2,CIcurrent.intzval,-1)
var upper=Wilson(negpred,Nx2,CIcurrent.intzval,+1)
document.form1.outp.value+="Negative predictive accuracy: "+truncate(negpred,3)+" ("+x22*1+"/"+Nx2+")"+writeln+"Variance: "+truncate(variance,5)+"; Std.Err: "+truncate(Math.sqrt(variance),5)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((negpred-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<np<"
                         +truncate((negpred+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson CI: "+truncate(lower*1,3)+"<np<"+truncate(upper*1,3)+writeln+writeln 
Chi2(x11,x21,x12,x22,NN)
}

if (prevalence>0)
{
document.form1.outp.value+=writeln+"Prevalence given by user, Bayesian analysis"+writeln+writeln
x11=sensi*prevalence
x22=speci*(1-prevalence)
x21=prevalence-x11
x12=1-prevalence-x22
var N1x=x11*1+x21*1
var N2x=x12*1+x22*1
var Nx1=x11*1+x12*1
var Nx2=x21*1+x22*1
document.form1.outp.value+=" Table of proportions"+writeln
document.form1.outp.value+=truncate(x11*1,5)+"\t"+truncate(x12*1,5)+":\t"+truncate(x11*1+x12*1,5)+" tested positive"+writeln
document.form1.outp.value+=truncate(x21*1,5)+"\t"+truncate(x22*1,5)+":\t"+truncate(x21*1+x22*1,5)+" tested negative"+writeln+writeln
document.form1.outp.value+="Proportion true positives eq: "+truncate(x11,5)+writeln
document.form1.outp.value+="Proportion true negatives eq: "+truncate(x22,5)+writeln
document.form1.outp.value+="Proportion false positives eq: "+truncate(x12,5)+writeln
document.form1.outp.value+="Proportion false negatives eq: "+truncate(x21,5)+writeln
document.form1.outp.value+="Proportion accurate eq: "+ truncate(x11*1+x22*1,5)+writeln
somdiag=x11*1+x22*1
somarg=N1x*Nx1+(N2x*Nx2)
kappa=(somdiag-somarg)/(1-somarg);
document.form1.outp.value+="Kappa measure of agreement: "+truncate(kappa,3)+writeln
document.form1.outp.value+="Sensitivity: "+truncate(sensi,5)+" $"+writeln
document.form1.outp.value+="Specificity: "+truncate(speci,5)+" $"+writeln
var ratio=sensi/(1-speci);
document.form1.outp.value+="Positive Likelihood: "+ truncate(ratio,3)+writeln
var ratio=(1-sensi)/speci;
document.form1.outp.value+="Negative Likelihood: "+ truncate(ratio,3)+writeln
var ratio=(sensi/(1-sensi))/((1-speci)/speci)
document.form1.outp.value+="Diagnostic Odds Ratio: "+ truncate(ratio,3)+writeln
var ratio=(sensi/(1-sensi))/(speci/(1-speci))
document.form1.outp.value+="Error Odds Ratio: "+ truncate(ratio,3)+writeln
document.form1.outp.value+="Youden's J: "+truncate(speci*1+sensi*1-1,3)+writeln
document.form1.outp.value+="Prevalence: "+truncate(prevalence,5)+" $"+writeln
var pospred=sensi*prevalence/(sensi*prevalence+((1-speci)*(1-prevalence)))
var negpred=speci*(1-prevalence)/(speci*(1-prevalence)+((1-sensi)*(prevalence)))
document.form1.outp.value+="Positive predictive accuracy: "+truncate(pospred,3)+writeln
document.form1.outp.value+="Negative predictive accuracy:  "+truncate(negpred,3)+writeln
ratio=((x11*x22)-(x12*x21))*((x11*x22)-(x12*x21))/((x11+x12)*(x11+x21)*(x22+x12)*(x22+x21))
document.form1.outp.value+="Pearson's correlation: "+truncate(Math.sqrt(ratio),5)+writeln
document.form1.outp.value+=writeln+"$ input into the Bayesian procedure"+writeln
}
}

function alpha(table,n,CIBox)
{
var oratio=(table[1][1]/table[1][2])/(table[2][1]/table[2][2]); 
document.form1.outp.value+="Odds Ratio p1<->p2: "+truncate(oratio,4)  

var  varodd=0;
for (index=1;index<3;index++)    
    for (jndex=1;jndex<3;jndex++) 
        varodd=varodd+(1/table[index][jndex]);   //standard deviation of odds-ratio
// document.form1.outp.value+=writeln+index+","+jndex+": "+table[index][jndex]} for debug

var varodd1=Math.sqrt(varodd*oratio*oratio); //(old not so good method to estimate standard error) 
var varodd2=Math.sqrt(varodd*(1/oratio)*(1/oratio)); //((this one for inverse (came all from Reynolds))

varodd=Math.sqrt(varodd)
document.form1.outp.value+=" (~Std.Err. "+truncate(varodd1,4)+")"+writeln+
                           CIcurrent.interval+" CI: "+truncate(Math.exp(Math.log(oratio)-(varodd*CIcurrent.intzval)),4)+
                           "<O.R.<"+truncate(Math.exp(Math.log(oratio)+(varodd*CIcurrent.intzval)),4)+" (Wald)"+writeln

document.form1.outp.value+="inverse OR p2<->p1: "+truncate((1/oratio),4)+" (~SE="+truncate(varodd2,2)+")" +writeln+
                           CIcurrent.interval+" CI: "+truncate(Math.exp(Math.log(1/oratio)-(varodd*CIcurrent.intzval)),4)+
                           "<O.R.<"+truncate(Math.exp(Math.log(1/oratio)+(varodd*CIcurrent.intzval)),4)+" (Wald)"+writeln

if (CIBox.checked)
{
document.form1.outp.value+=writeln+"Alternative C.I. for Odds Ratio:"+writeln
                           +CIcurrent.interval+" CI: "+truncate(oratio-(varodd1*CIcurrent.intzval),4)
                           +"<"+truncate(oratio,4)+"<"+truncate(oratio+(varodd1*CIcurrent.intzval),4)+" (Normal Apr)"+writeln

document.form1.outp.value+= CIcurrent.interval+" CI: "+truncate(1/oratio-(varodd2*CIcurrent.intzval),4)
                           +"<"+truncate(1/oratio,4)+"<"+truncate(1/oratio+(varodd2*CIcurrent.intzval),4)+" (Normal Apr)"+writeln
var chi2=(table[1][1]*table[2][2]-table[1][2]*table[2][1])*(table[1][1]*table[2][2]-table[1][2]*table[2][1])
chi2=chi2*(table[3][1]+table[3][2])
chi2=chi2/table[3][1]/table[3][2]/table[1][3]/table[2][3]
chi2=Math.sqrt(chi2)
if (oratio>1) {varodd=1} else varodd=-1
document.form1.outp.value+=CIcurrent.interval+" CI: "+truncate(Math.pow(oratio,(1*1-varodd*CIcurrent.intzval/chi2)),4)
                           +"<"+truncate(oratio,4)+"<"+truncate(Math.pow(oratio,(1*1+varodd*CIcurrent.intzval/chi2)),4)+" (Chi-sq based)"+writeln
document.form1.outp.value+=CIcurrent.interval+" CI: "+truncate(Math.pow(1/oratio,(1*1+varodd*CIcurrent.intzval/chi2)),4)
                           +"<"+truncate(1/oratio,4)+"<"+truncate(Math.pow(1/oratio,(1*1-varodd*CIcurrent.intzval/chi2)),4)+" (Chi-sq based)"+writeln
}
};


function Wilson(p,n,z,ul) //continuity corrected version!!!
{
var outcome=2*n*p+z*z+ul+ul*z*Math.sqrt(z*z+ul*2-1/n+4*p*(n*(1-p)-1*ul))
outcome/=(2*(n*1+z*z))
return outcome
}

function ClearOutp()
{
var writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n"; 
document.form1.outp.value="";
}


