* SAMPLE SIZE FOR CORRELATION HYPOTHESIS TESTING *.
* Marta Garcia-Granero biostatistics@terra.es .

* Usual conditions for alfa and power (auxiliary file).
* This file covers the most used conditions for
  sample size determination (add your own if required):
  - alfa=5% and 1% with for or two tailed tests
  - power=80%, 90%, 95% and 99%.
* This file must be active in order to make some of 
  the MACROS work.

DATA LIST LIST /id(F2.0) alfa(f8.3) tails(F2.0) power(F8.3).
BEGIN DATA
 1 0.05 1 0.80
 2 0.05 2 0.80
 3 0.05 1 0.90
 4 0.05 2 0.90
 5 0.05 1 0.95
 6 0.05 2 0.95
 7 0.05 1 0.99
 8 0.05 2 0.99
 9 0.01 1 0.80
10 0.01 2 0.80
11 0.01 1 0.90
12 0.01 2 0.90
13 0.01 1 0.95
14 0.01 2 0.95
15 0.01 1 0.99
16 0.01 2 0.99
END DATA.
COMPUTE zalfa = IDF.NORMAL(1-(alfa/tails),0,1) .
COMPUTE zbeta = IDF.NORMAL(1-power,0,1) .
EXEC.

DEFINE onecorr(r0=!DEFAULT(0) !TOKENS(1)/r1=!TOKENS(1)).
* This one requires the auxiliary file *. 
MATRIX.
PRINT  /TITLE 'SAMPLE SIZE: ONE SAMPLE CORRELATION TEST'.
get id /var=id.
get alfa /var=alfa.
get tails/ var=tails.
get power /var=power.
get zalfa /var=zalfa.
get zbeta /var=zbeta.
compute r0=!r0.
compute r1=!r1.
COMPUTE zr0=.5*(LN((1+r0)/(1-r0))).
COMPUTE zr1=.5*(LN((1+r1)/(1-r1))).
compute zdiff=abs(zr1-zr0).
print {r0,r1,zr0,zr1,zdiff}
 /format="f8.2"
 /clabels="H0 rho","H1 rho","Zr0","Zr1","Dif. Z"
 /title="Correlation coefficients under  H0 & H1".
compute n=TRUNC(((zalfa-zbeta)/zdiff)&**2)+4.
print {id,100*alfa,tails,100*power,n}
 /format="f8.0"
 /clabels="Id","Alfa(%)","Nє colas","Pot.(%)","N"
 /title="Required sample size for the following values of alpha & beta".
END MATRIX.
!ENDDEFINE.

DEFINE onecorrp(r0=!DEFAULT(0) !TOKENS(1)/r1=!TOKENS(1) /n=!TOKENS(1)
 /alfa=!DEFAULT(0.05) !TOKENS(1) /tails=!DEFAULT(2) !TOKENS(1)). 
* Auxiliary file NOT needed *. 
MATRIX.
PRINT  /TITLE 'SAMPLE SIZE: POWER FOR A ONE SAMPLE CORRELATION TEST'.
compute r0=!r0.
compute r1=!r1.
compute n=!n.
compute alfa=!alfa.
compute tails=!tails.
compute pval=alfa/tails.
do if pval=0.05.
compute zalfa=1.645.
end if.
do if pval=0.025.
compute zalfa=1.96.
end if.
do if pval=0.01.
compute zalfa=2.326.
end if.
do if pval=0.005.
compute zalfa=2.576.
end if.
COMPUTE zr0=.5*(LN((1+r0)/(1-r0))).
COMPUTE zr1=.5*(LN((1+r1)/(1-r1))).
compute zdiff=abs(zr1-zr0).
print {r0,r1,zr0,zr1,zdiff}
 /format="f8.2"
 /clabels="H0 rho","H1 rho","Zr0","Zr1","Dif. Z"
 /title="Correlation coefficients under H0 & H1".
print {100*alfa,tails,n}
 /format="f8.0"
 /clabels="Alpha(%)","Tails","N"
 /title="Input data".
compute power=100*(1-cdfnorm(zalfa-abs(zdiff*sqrt(n-3)))).
print power
 /format="f8.1"
 /title="Test power (%)".
END MATRIX.
!ENDDEFINE.

DEFINE twocorr(r1=!TOKENS(1)/ r2=!TOKENS(1)/ ratio=!DEFAULT(1)!TOKENS(1)). 
* This one requires the auxiliary file *. 
MATRIX.
PRINT  /TITLE 'SAMPLE SIZE: TWO SAMPLES CORRELATION TEST'.
get id /var=id.
get alfa /var=alfa.
get tails/ var=tails.
get power /var=power.
get zalfa /var=zalfa.
get zbeta /var=zbeta.
compute k=!ratio.
compute r1=!r1.
compute r2=!r2.
COMPUTE zr1=.5*(LN((1+r1)/(1-r1))).
COMPUTE zr2=.5*(LN((1+r2)/(1-r2))).
compute zdiff=abs(zr1-zr2).
print {r1,r2,zr1,zr2,zdiff}
 /format="f8.2"
 /clabels="Corr 1","Corr 2","Zr1","Zr2","Dif. Z"
 /title="Minimum significant difference".
print k
 /format="f8.0"
 /title="Ratio (N2/N1)".
compute n=TRUNC(2*((zalfa-zbeta)/zdiff)&**2)+4.
COMPUTE n1=n*(k+1)/(2*k).
COMPUTE n2=n*(1+k)/2.
print {id,100*alfa,tails,100*power,n1,n2,n1+n2}
 /format="f8.0"
 /clabels="Id","Alfa(%)","Nє colas","Pot.(%)","N1","N2","N Total"
 /title="Required sample sizes for the following values of alpha & beta".
END MATRIX.
!ENDDEFINE.

DEFINE twocorrp(r1=!TOKENS(1) /r2=!TOKENS(1) /n1=!TOKENS(1) /n2=!TOKENS(1)
 /alfa=!DEFAULT(0.05) !TOKENS(1) /tails=!DEFAULT(2) !TOKENS(1)). 
* Auxiliary file NOT needed *. 
MATRIX.
PRINT  /TITLE 'SAMPLE SIZE: POWER FOR A TWO SAMPLES CORRELATION TEST'.
compute r1=!r1.
compute r2=!r2.
compute n1=!n1.
compute n2=!n2.
compute n=2*n1*n2/(n1+n2).
compute alfa=!alfa.
compute tails=!tails.
compute pval=alfa/tails.
do if pval=0.05.
compute zalfa=1.645.
end if.
do if pval=0.025.
compute zalfa=1.96.
end if.
do if pval=0.01.
compute zalfa=2.326.
end if.
do if pval=0.005.
compute zalfa=2.576.
end if.
COMPUTE zr1=.5*(LN((1+r1)/(1-r1))).
COMPUTE zr2=.5*(LN((1+r2)/(1-r2))).
compute zdiff=abs(zr1-zr2).
print {r1,r2,zr1,zr2,zdiff}
 /format="f8.2"
 /clabels="Corr 1","Corr 2","Zr1","Zr2","Dif. Z"
 /title="Correlations compared".
print {100*alfa,tails,n1,n2}
 /format="f8.0"
 /clabels="Alfa(%)","Tails","N1","N2"
 /title="Input data".
compute power=100*(1-cdfnorm(zalfa-abs(zdiff*sqrt((n-3)/2)))).
print power
 /format="f8.1"
 /title="Test power (%)".
END MATRIX.
!ENDDEFINE.

* MACRo calls (some examples):

ONECORR  r1=0.5.
ONECORR  r0=0.3 r1=0.6.
ONECORRP r1=0.5 n=25 alfa=0.05 tails=1.
TWOCORR  r1=0.3 r2=0.6 ratio=2.
TWOCORRP r1=0.3 r2=0.6 n1=110 n2=220.