Diff for "FAQ/power/propsn" - CBU statistics Wiki
location: Diff for "FAQ/power/propsn"
Differences between revisions 1 and 28 (spanning 27 versions)
Revision 1 as of 2006-09-15 10:48:25
Size: 2496
Editor: PeterWatson
Comment:
Revision 28 as of 2013-03-08 10:17:39
Size: 5494
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
This program uses the difference in proportions [[http://en.wikipedia.org/wiki/Effect_size#Cohen.27s_d|(or equivalently the odds ratio)]] as the effect size.
Line 2: Line 3:
p1 and p2 are the proportions in groups 1 and 2 respectively being compared, delta is the ratio: size of group 2 / size of group 1, alpha is the (two-tailed) type I error. The program then outputs the sample sizes required for the specified power. A power calculator is also available in a [[attachment:prop2wok.xls|spreadsheet]] or using a web calculator [[http://statpages.org/proppowr.html|here.]] and [[FAQ/rpropspow| in R and G*POWER3]].

An [[FAQ/capacityeg| example]] uses marginal probabilities to compute cell probabilities in a 2x2 table to give inputs for a power analysis.

[ADJUST THE EXAMPLE INPUT AS DESIRED; THEN COPY AND PASTE INTO A SPSS SYNTAX WINDOW AND RUN; OUTPUT BOTH TO SPREADSHEET AND OUTPUT WINDOW].
Line 5: Line 11:
/p1 p2 ratio alpha power.
BEGIN DATA.
0.19 0.09 1 0.05 0.80
0.19 0.09 4 0.05 0.80
0.19 0.09 10 0.05 0.80
END DATA.

compute p11= (p1*ratio)/(1+ratio).
compute p12=((1-p1)*ratio)/(1+ratio).
compute p21=p2/(1+ratio).
compute p22=(1-p2)/(1+ratio).
/alpha p1 p2 delta power.
BEGIN DATA.
.05 0.19 0.09 2 0.87
.05 0.40 0.35 2 0.80
END DATA.
Line 18: Line 18:
define orpow ( !pos !tokens(1)
                    / !pos !tokens(1)
                    / !pos !tokens(1)
                    / !pos !tokens(1)
                    / !pos !tokens(1)
                    / !pos !tokens(1)).


compute oddsr = (!1*!4)/(!3*!2).
exe.


COMPUTE #POW = !6.
COMPUTE #chisq1 = ( LN(oddsr) / sqrt((1/!1)+(1/!2)+(1/!3)+(1/!4)) )**2.
compute #df=1.
compute #conf = 1-!5.
define propn (!pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)).
COMPUTE #POW = !5.
compute #conf = 1-!1.
Line 36: Line 27:
compute #chisq = 3.000.
comment COMPUTE #LC1 = 2.000.
COMPUTE #CUMF2 = 1 - NCDF.CHISQ(IDF.CHISQ(#conf,#DF),#DF,#CHISQ).
COMPUTE #DIFF = 1 .
SET MXLOOPS=10000.
compute n1=3.
compute n2=!4*n1.
compute #pbar= (n1*!2 + n2*!3) / (n1+n2).
compute #z2= ( (!2-!3) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
compute oddsr = ( n1*!2*n2*(1-!3) ) / ( n1*(1-!2)*n2*!3 ).
compute #CUMF2=1-ncdf.chisq(idf.chisq(1-!1,1),1,#z2).
compute #diff=1.
SET MXLOOPS=40000.
Line 43: Line 37:
+ COMPUTE #LC3 = #CHISQ.
+ COMPUTE #CHISQ = (#chisq + rnd(#chisq/3)).
+ COMPUTE #CUMF2 = 1 - NCDF.CHISQ(IDF.CHISQ(#conf,#DF),#DF,#CHISQ).
+ COMPUTE #LC3 = n1.
+ COMPUTE n1 = (n1 + 1).
+ compute n2=!4*n1.
+ compute #pbar= (n1*!2 + n2*!3) / (n1+n2).
+ compute #z2= ( (!2-!3) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+ compute #CUMF2=1-ncdf.chisq(idf.chisq(1-!1,1),1,#z2).
Line 47: Line 44:
+ COMPUTE #LC1 = #chisq .
+ COMPUTE #chisq = (#chisq + #LC3)/2 .
+ COMPUTE #CUMF2 = 1 - NCDF.CHISQ(IDF.CHISQ(#conf,#DF),#DF,#CHISQ).
+ COMPUTE #LC1 = n1 .
+ COMPUTE n1 = (n1 + #LC3)/2 .
+ compute n2=!4*n1.
+ compute #pbar= (n1*!2 + n2*!3) / (n1+n2).
+ compute #z2= ( (!2-!3) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+ compute #CUMF2=1-ncdf.chisq(idf.chisq(1-!1,1),1,#z2).
Line 53: Line 53:
compute ntot = #chisq/#chisq1.
exe.
if (ntot-trunc(ntot) gt 0.5) #ind=1.
if (#ind eq 0) ntot=trunc(ntot)+1.
if (#ind eq 1) ntot=rnd(ntot).
if (n1-trunc(n1) gt 0.5) #ind=1.
if (#ind eq 0) n1=trunc(n1)+1.
if (#ind eq 1) n1=rnd(n1).
Line 59: Line 57:
compute p11=!1.
compute p12=!2.
compute p21=!3.
compute p22=!4.
compute alpha=!5.
compute power=!6.
compute p1=p11/(p11+p12).
compute p2=p21/(p21+p22).
formats ntot (f7.0) alpha (f5.2) p1 (f5.2) p2 (f5.2) ratio (f5.2) oddsr power (f5.2).
variable labels ntot 'Total Sample Size Required' /alpha 'Alpha' /p1 'Proportion 1' /p2 'Proportion 2' /ratio 'Ratio Gp1:Gp2' /oddsr 'Odds Ratio' /power 'Power'.
compute alpha=!1.
compute p1=!2.
compute p2=!3.
compute n2=!4*n1.
compute power=!5.
formats n1 (f7.0) n2 (f7.0) alpha (f5.2) p1 (f5.2) p2 (f5.2) power (f5.2).
variable labels n1 'Sample Size 1' /n2 'Sample Size 2' /alpha 'Alpha' /p1 'Proportion 1' /p2 'Proportion 2' /power 'Power'.
Line 70: Line 65:
  /variables=ntot alpha p1 p2 ratio oddsr power
  /title "Sample Size for comparing two independent proportions given power" .
  /variables=n1 n2 alpha p1 p2 power
  /title "Sample Size required for a two sample, two-tailed binomial test" .
Line 73: Line 68:
define propncc (!pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)).
COMPUTE #POW = !5.
compute #conf = 1-!1.
compute #lc3 = 1.
compute #ind=0.
compute n1=3.
compute n2=!4*n1.
compute #contc= 0.5*((1/n1)+(1/n2)).
compute #pbar= (n1*p1 + n2*p2) / (n1+n2).
compute #z21= ( ( abs(p1-p2)-#contc ) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
compute #CUMF2=1-ncdf.chisq(idf.chisq(1-alpha,1),1,#z21).
compute #diff=1.
SET MXLOOPS=40000.
LOOP IF (#DIFF GT .00005) .
+ DO IF (#CUMF2 LT #pow) .
+ COMPUTE #LC3 = n1.
+ COMPUTE n1 = (n1 + 1).
+ compute n2=!4*n1.
+ compute #contc= 0.5*((1/n1)+(1/n2)).
+ compute #pbar= (n1*p1 + n2*p2) / (n1+n2).
+ compute #z21= ( ( abs(p1-p2)-#contc ) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+ compute #CUMF2=1-ncdf.chisq(idf.chisq(1-alpha,1),1,#z21).
+ ELSE .
+ COMPUTE #LC1 = n1 .
+ COMPUTE n1 = (n1 + #LC3)/2 .
+ compute n2=!4*n1.
+ compute #contc= 0.5*((1/n1)+(1/n2)).
+ compute #pbar= (n1*p1 + n2*p2) / (n1+n2).
+ compute #z21= ( ( abs(p1-p2)-#contc ) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+ compute #CUMF2=1-ncdf.chisq(idf.chisq(1-alpha,1),1,#z21).
+ END IF .
+ COMPUTE #DIFF = ABS(#CUMF2 - #pow) .
END LOOP .
if (n1-trunc(n1) gt 0.5) #ind=1.
if (#ind eq 0) n1=trunc(n1)+1.
if (#ind eq 1) n1=rnd(n1).
EXECUTE .
compute alpha=!1.
compute p1=!2.
compute p2=!3.
compute n2=!4*n1.
compute power=!5.
formats n1 (f7.0) n2 (f7.0) alpha (f5.2) p1 (f5.2) p2 (f5.2) power (f5.2).
variable labels n1 'Sample Size 1' /n2 'Sample Size 2' /alpha 'Alpha' /p1 'Proportion 1' /p2 'Proportion 2' /power 'Power'.
report format=list automatic align(center)
  /variables=n1 n2 alpha p1 p2 power
  /title "Sample Sizes for a two sample, two-tailed binomial test (continuity corrected)" .
!enddefine.
Line 75: Line 121:
get m /variables=p11 p12 p21 p22 alpha power /missing=omit.
compute p11=make(1,1,0).
compute p12=make(1,1,0).
compute p21=make(1,1,0).
compute p22=make(1,1,0).
get m /variables=alpha p1 p2 delta power /missing=omit.
Line 81: Line 123:
compute p1=make(1,1,0).
compute p2=make(1,1,0).
compute delta=make(1,1,0).
Line 82: Line 127:
compute p11=m(:,1).
compute p12=m(:,2).
compute p21=m(:,3).
compute p22=m(:,4).
compute alpha=m(:,5
).
compute power=m(:,6).
compute alpha=m(:,1).
compute p1=m(:,2).
compute p2=m(:,3).
compute delta=m(:,4).
compute power=m(:,5).  
Line 89: Line 133:
orpow p11 p12 p21 p22 alpha power. propn alpha p1 p2 delta power.
propncc alpha p1 p2 delta power.

This program uses the difference in proportions (or equivalently the odds ratio) as the effect size.

p1 and p2 are the proportions in groups 1 and 2 respectively being compared, delta is the ratio: size of group 2 / size of group 1, alpha is the (two-tailed) type I error. The program then outputs the sample sizes required for the specified power. A power calculator is also available in a spreadsheet or using a web calculator here. and in R and G*POWER3.

An example uses marginal probabilities to compute cell probabilities in a 2x2 table to give inputs for a power analysis.

[ADJUST THE EXAMPLE INPUT AS DESIRED; THEN COPY AND PASTE INTO A SPSS SYNTAX WINDOW AND RUN; OUTPUT BOTH TO SPREADSHEET AND OUTPUT WINDOW].

DATA LIST free
/alpha p1 p2 delta power. 
BEGIN DATA. 
.05 0.19 0.09 2 0.87
.05 0.40 0.35 2 0.80
END DATA. 

set errors=none.
define propn (!pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)).
COMPUTE #POW = !5.
compute #conf = 1-!1.
compute #lc3 = 1.
compute #ind=0.
compute n1=3.
compute n2=!4*n1.
compute #pbar= (n1*!2 + n2*!3) / (n1+n2).
compute #z2= ( (!2-!3) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
compute oddsr = ( n1*!2*n2*(1-!3) ) / ( n1*(1-!2)*n2*!3 ).
compute #CUMF2=1-ncdf.chisq(idf.chisq(1-!1,1),1,#z2).
compute #diff=1.
SET MXLOOPS=40000.
LOOP IF (#DIFF GT .00005) .
+       DO IF (#CUMF2 LT #pow) .
+               COMPUTE #LC3 = n1.
+               COMPUTE n1 = (n1 + 1).
+                      compute n2=!4*n1.
+                      compute #pbar= (n1*!2 + n2*!3) / (n1+n2).
+                      compute #z2= ( (!2-!3) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+                      compute #CUMF2=1-ncdf.chisq(idf.chisq(1-!1,1),1,#z2).
+       ELSE .
+               COMPUTE #LC1 = n1 .
+               COMPUTE n1 = (n1 + #LC3)/2 .
+                      compute n2=!4*n1.
+                      compute #pbar= (n1*!2 + n2*!3) / (n1+n2).
+                      compute #z2= ( (!2-!3) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+                      compute #CUMF2=1-ncdf.chisq(idf.chisq(1-!1,1),1,#z2).
+       END IF .
+       COMPUTE #DIFF = ABS(#CUMF2 - #pow) .
END LOOP .
if (n1-trunc(n1) gt 0.5) #ind=1.
if (#ind eq 0) n1=trunc(n1)+1.
if (#ind eq 1) n1=rnd(n1).
EXECUTE .
compute alpha=!1.
compute p1=!2.
compute p2=!3.
compute n2=!4*n1.
compute power=!5.
formats n1 (f7.0) n2 (f7.0) alpha (f5.2) p1 (f5.2) p2 (f5.2) power (f5.2).
variable labels n1 'Sample Size 1' /n2 'Sample Size 2' /alpha 'Alpha' /p1 'Proportion 1' /p2 'Proportion 2' /power 'Power'.
report format=list automatic align(center)
  /variables=n1 n2 alpha p1 p2 power 
  /title "Sample Size required for a two sample, two-tailed binomial test" .
!enddefine.
define propncc (!pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)
                   / !pos !tokens(1)).
COMPUTE #POW = !5.
compute #conf = 1-!1.
compute #lc3 = 1.
compute #ind=0.
compute n1=3.
compute n2=!4*n1.
compute #contc= 0.5*((1/n1)+(1/n2)).
compute #pbar= (n1*p1 + n2*p2) / (n1+n2).
compute #z21= ( ( abs(p1-p2)-#contc ) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
compute #CUMF2=1-ncdf.chisq(idf.chisq(1-alpha,1),1,#z21).
compute #diff=1.
SET MXLOOPS=40000.
LOOP IF (#DIFF GT .00005) .
+       DO IF (#CUMF2 LT #pow) .
+               COMPUTE #LC3 = n1.
+               COMPUTE n1 = (n1 + 1).
+                      compute n2=!4*n1.
+                      compute #contc= 0.5*((1/n1)+(1/n2)).
+                      compute #pbar= (n1*p1 + n2*p2) / (n1+n2).
+                      compute #z21= ( ( abs(p1-p2)-#contc ) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+                      compute #CUMF2=1-ncdf.chisq(idf.chisq(1-alpha,1),1,#z21).
+       ELSE .
+               COMPUTE #LC1 = n1 .
+               COMPUTE n1 = (n1 + #LC3)/2 .
+                      compute n2=!4*n1.
+                      compute #contc= 0.5*((1/n1)+(1/n2)).
+                      compute #pbar= (n1*p1 + n2*p2) / (n1+n2).
+                      compute #z21= ( ( abs(p1-p2)-#contc ) /sqrt( #pbar*(1-#pbar)*( (1/n1) + (1/n2) ) ) )**2.
+                      compute #CUMF2=1-ncdf.chisq(idf.chisq(1-alpha,1),1,#z21).
+       END IF .
+       COMPUTE #DIFF = ABS(#CUMF2 - #pow) .
END LOOP .
if (n1-trunc(n1) gt 0.5) #ind=1.
if (#ind eq 0) n1=trunc(n1)+1.
if (#ind eq 1) n1=rnd(n1).
EXECUTE .
compute alpha=!1.
compute p1=!2.
compute p2=!3.
compute n2=!4*n1.
compute power=!5.
formats n1 (f7.0) n2 (f7.0) alpha (f5.2) p1 (f5.2) p2 (f5.2) power (f5.2).
variable labels n1 'Sample Size 1' /n2 'Sample Size 2' /alpha 'Alpha' /p1 'Proportion 1' /p2 'Proportion 2' /power 'Power'.
report format=list automatic align(center)
  /variables=n1 n2 alpha p1 p2 power 
  /title "Sample Sizes for a two sample, two-tailed binomial test (continuity corrected)" .
!enddefine.
matrix.
get m /variables=alpha p1 p2 delta power  /missing=omit.
compute alpha=make(1,1,0).
compute p1=make(1,1,0).
compute p2=make(1,1,0).
compute delta=make(1,1,0).
compute power=make(1,1,0).
compute alpha=m(:,1).
compute p1=m(:,2).
compute p2=m(:,3).
compute delta=m(:,4).
compute power=m(:,5).  
end matrix.
propn alpha p1 p2 delta power.
propncc alpha p1 p2 delta power.

None: FAQ/power/propsn (last edited 2013-03-08 10:17:39 by localhost)