* (I) SAMPLE SIZE FOR CONFIDENCE INTERVAL ESTIMATION *. DEFINE ciprop(p=!DEFAULT(0.5) !TOKENS(1) /eps=!TOKENS(1) /ci=!DEFAULT(95) !TOKENS(1) ). * Sample size required for the CI for one proportion *. MATRIX. PRINT /TITLE ' SAMPLE SIZE: CONFIDENCE INTERVAL PRECISION: ONE PROPORTION'. compute p=!p. compute eps=!eps. compute ci=!ci. do if ci=95. compute zalfa=1.96. end if. do if ci=99. compute zalfa=2.576. end if. do if ci=99.9. compute zalfa=3.09. end if. print ci /format="f8.1" /title='Confidence level (%)'. print eps /format="f8.2" /title="Precision desired". print p /format="f8.2" /title="Estimated proportion". compute n=TRUNC(p*(1-p)*((zalfa/eps)**2))+1. print n /format="f8.0" /title="Sample size required". END MATRIX. !ENDDEFINE. * MACRO call: give values for estimated proportion, precision desired and confidence level If there is no info about the estimated proportion, assume the most conservative (p=0.5) default values for p & ci are 0.5 and 95 *. ciprop eps=0.02. ciprop p=0.3 eps=0.04 ci=99. ciprop p=0.2 eps=0.03 ci=99.9. ******************************************************************************** (II) SAMPLE SIZE FOR HIPOTHESIS TESTING *. * 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 one or two tailed tests - power=80%, 90%, 95% and 99%. * This file must be active in order to make 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. ********************************************* * ONE SAMPLE, BINARY OUTCOMES (PROPORTIONS) * *********************************************. DEFINE oneprop(p0=!TOKENS(1) /p1=!TOKENS(1)). * Comparison of one sample proportion with a population proportion p0: null hypothesis value for p p1: alternative hypothesis value for p (auxiliary file needed) *. MATRIX. PRINT /TITLE ' SAMPLE SIZE: ONE PROPORTION '. 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 p0=!p0. compute p1=!p1. compute diff=abs(p0-p1). print {p0,p1,diff} /format="f8.2" /clabels="Null p","Alt. p", "Diff." /title="Difference worth detecting". compute n=TRUNC(((zalfa*sqrt(p0*(1-p0))-zbeta*sqrt(p1*(1-p1)))&**2)/(diff&**2))+1. print {id,100*alfa,tails,100*power,n} /format="f8.0" /clabels="Id","Alfa(%)","N. tails","Power(%)","N" /title="Sample size required". END MATRIX. !ENDDEFINE. * MACRO call: give values for p0 & p1 *. oneprop p0=0.5 p1=0.8. DEFINE poneprop(p0=!TOKENS(1) /p1=!TOKENS(1) /n=!TOKENS(1) /alfa=!DEFAULT(0.05) !TOKENS(1) /tails=!DEFAULT(2) !TOKENS(1)). * Power of a test, given a sample size and values for p0 and p1 (auxiliary file NOT needed) *. MATRIX. PRINT /TITLE ' SAMPLE SIZE: POWER OF A TEST (ONE PROPORTION)'. compute p0=!p0. compute p1=!p1. compute diff=abs(p0-p1). 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. print {p0,p1,diff} /format="f8.2" /clabels="Null p","Alt. p", "Diff." /title="Difference worth detecting". print {100*alfa,tails,n} /format="f8.0" /clabels="Alfa(%)","Tails","N" /title="Input data". compute beta=100*(1-cdfnorm((zalfa*sqrt(p0*(1-p0))-diff*sqrt(n))/sqrt(p1*(1-p1)))). print beta /format="f8.1" /title="Power of the test (%)". END MATRIX. !ENDDEFINE. * MACRO call: give values for p0, p1, sample size used, alfa level and tails *. poneprop p0=0.5 p1=0.8 n=30 alfa=0.05 tails=2. poneprop p0=0.5 p1=0.8 n=20 alfa=0.05 tails=2. poneprop p0=0.5 p1=0.8 n=15 alfa=0.05 tails=1. * alfa levels & tails have default values of 0.05 & 2. ******************************************** * TWO INDEPENDENT SAMPLES, BINARY OUTCOMES * ********************************************. DEFINE twoprop(p1=!TOKENS(1)/ p2=!TOKENS(1)/ r=!DEFAULT(1)!TOKENS(1)). * Comparison of 2 independent proportions (auxiliary file needed) *. MATRIX. PRINT /TITLE ' SAMPLE SIZE: TWO INDEPENDENT PROPORTIONS'. 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=!r. compute p1=!p1. compute q1=1-p1. compute p2=!p2. compute q2=1-p2. compute p=(p1+k*p2)/(1+k). compute q=1-p. compute diff=abs(p1-p2). print {p1,p2,diff} /format="f8.2" /clabels="p1","p2","Diff." /title="Difference worth detecting". print k /format="f8.0" /title="Ratio (N2/N1)". * Ni *. COMPUTE n1=TRUNC(((zalfa*sqrt(p*q*(1+k)/k)-zbeta*sqrt(p1*q1+(p2*q2/k)))&**2)/(diff**2))+1. COMPUTE n2=TRUNC(((zalfa*sqrt(p*q*(1+k))-zbeta*sqrt(k*p1*q1+(p2*q2)))&**2)/(diff**2))+1. print {id,100*alfa,tails,100*power,n1,n2,n1+n2} /format="f8.0" /clabels="Id","Alfa(%)","N. tails","Power(%)","N1","N2","Total N" /title="Sample size required". END MATRIX. !ENDDEFINE. * MACRO call: indicate values for p1, p2 and ratio *. twoprop p1=0.5 p2=0.3 r=1. twoprop p1=0.5 p2=0.3 r=2. twoprop p1=0.5 p2=0.3 r=3. * If you want to get the output for just one condition, for example alfa=5%, one tail and 90% power (row Nr. 3). TEMPORARY. SELECT IF ID=3. twoprop p1=0.5 p2=0.3 r=2. DEFINE ptwoprop(p1=!TOKENS(1) /p2=!TOKENS(1) /n1=!TOKENS(1) /n2=!TOKENS(1) /alfa=!DEFAULT(0.05) !TOKENS(1) /tails=!DEFAULT(2) !TOKENS(1)). * Power of a test, given sample sizes and observed p1 & p2 (auxiliary file NOT needed) *. MATRIX. PRINT /TITLE ' SAMPLE SIZE: POWER OF A TEST (TWO PROPORTIONS)'. compute p1=!p1. compute p2=!p2. compute n1=!n1. compute n2=!n2. compute q1=1-p1. compute q2=1-p2. compute p=(n1*p1+n2*p2)/(n1+n2). compute q=1-p. compute diff=abs(p1-p2). 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 diff=abs(p1-p2). print {p1,p2,diff} /format="f8.2" /clabels="p1","p2","Diff." /title="Difference worth detecting". print {100*alfa,tails,n1,n2} /format="f8.0" /clabels="Alfa(%)","Tails","N1","N2" /title="Input data". compute beta=100*(1-cdfnorm((zalfa*sqrt(2*p*q)-diff*sqrt(n))/sqrt(p1*q1+p2*q2))). print beta /format="f8.1" /title="Power of the test (%)". END MATRIX. !ENDDEFINE. * MACRO call: give values for both sample sizes & proportions, alfa level and tails *. ptwoprop p1=0.5 p2=0.3 n1=124 n2=124 alfa=0.05 tails=2. ptwoprop p1=0.5 p2=0.3 n1=82 n2=245 alfa=0.05 tails=2. ptwoprop p1=0.5 p2=0.3 n1=115 n2=229 alfa=0.01 tails=1. ptwoprop p1=0.5 p2=0.4 n1=50 n2=80 alfa=0.05 tails=1. * alfa levels & tails have default values of 0.05 & 2.