Biostatistics 190 #POWER & SAMPLE SIZE CALCULATIONS #FOR TWO SAMPLES #ZAR EXAMPLE 8.1 ZAR=read.table("ZarEX8.1R.txt") ZAR attach(ZAR) X1=data[group=="gpB"] X2=data[group=="gpG"] n1=length(X1) n1 n2=length(X2) n2 s1=sqrt(var(X1)) s2=sqrt(var(X2)) #POOLED VARIANCE: sp=sqrt(((n1-1)*s1^2+(n2-1)*s2^2)/(n1+n2-2)) sp sp^2 #ESTIMATING SAMPLE SIZE FOR CI #SET THE FOLLOWING VALUES AS DESIRED: d=0.5 N0=50 alpha=0.05 #ITERATE THE FOLLOWING UNTIL N IS STABILIZED: N1=(2*sp^2*(qt(1-alpha/2,2*(N0-1)))^2)/d^2 N1 N2=(2*sp^2*(qt(1-alpha/2,2*(N1-1)))^2)/d^2 N2 N3=(2*sp^2*(qt(1-alpha/2,2*(N2-1)))^2)/d^2 N3 N4=(2*sp^2*(qt(1-alpha/2,2*(N3-1)))^2)/d^2 N4 N5=(2*sp^2*(qt(1-alpha/2,2*(N4-1)))^2)/d^2 N5 #ESTIMATING SAMPLE SIZE FOR TWO SAMPLE T-TEST #SET THE FOLLOWING VALUES AS DESIRED: delta=0.5 alpha=0.05 beta=0.10 N0=100 #ITERATE THE FOLLOWING UNTIL N IS STABILIZED: N1=(2*sp^2/delta^2)*(qt(alpha/2,2*(N0-1))+qt(beta,N0-1))^2 N1 N2=(2*sp^2/delta^2)*(qt(alpha/2,2*(N1-1))+qt(beta,N1-1))^2 N2 N3=(2*sp^2/delta^2)*(qt(alpha/2,2*(N2-1))+qt(beta,N2-1))^2 N3 N4=(2*sp^2/delta^2)*(qt(alpha/2,2*(N3-1))+qt(beta,N3-1))^2 N4 N5=(2*sp^2/delta^2)*(qt(alpha/2,2*(N4-1))+qt(beta,N4-1))^2 N5 #ESTIMATING DETECTABLE DIFFERENCE GIVEN N #FOR ONE SAMPLE t-TEST #SET THE FOLLOWING VALUES AS DESIRED: N=20 alpha=0.05 beta=0.10 delta=sqrt(2*sp^2/N)*(qt(alpha/2,2*(N-1))+qt(beta,2*(N-1))) delta #ESTIMATING POWER OF TWO SAMPLE t-TEST #SET THE FOLLOWING VALUES AS DESIRED: N=15 delta=1.0 alpha=0.05 B=(delta/sqrt(2*sp^2/N))-abs(qt(alpha/2,2*(N-1))) B POWER=pt(B,2*(N-1)) POWER POWERN=pnorm(B,0,1) POWERN