Power and Sample Size

DESCRIPTION:

Computes power and sample size for one and two-sample tests of proportions.

USAGE:

binomial.sample.size(p2, p.alt, p=0.5, power=0.8, alpha=0.05, 
    n1=NULL, n2=NULL, prop.n2=1, one.sample=F, 
    alternative="two.sided", expand.args=T, exact.n=F, 
    recompute.power=F, correct=T) 

REQUIRED ARGUMENTS:



One of these three arguments must be provided. The function will decide what to compute based on what is not provided. See the Details section.

p2
The second proportion in a two-sample test.
p.alt
The alternative proportion in a one-sample test.
n1
The sample size in a one or two-sample test.

OPTIONAL ARGUMENTS:

p
The first proportion in a two-sample test or the null proportion in a one-sample test. Default is 0.5.
power
The desired power level. Default is 0.80.
alpha
The desired alpha level - probability of a Type I error. Default is 0.05.
n2
Second sample size in two-sample test. Default is equal to n1.
prop.n2
Proportion of n2 to n1: n2 = prop.n2 * n1. Default is 1.
one.sample
The function attempts to determine whether it is one or two-sample based on the input, but this argument can be used explicitly in the event the input is ambiguous.
alternative
Nature of alternative hypothesis. One of "two.sided", "greater", "less".
expand.args
If TRUE, the input arguments will be expanded to build a cross-product table. If FALSE, it is assumed the input is matched, and will only be replicated to the length of the longest vector. Default is TRUE.
exact.n
If TRUE, the computed sample size will not be rounded up. Default is FALSE.
recompute.power
If TRUE, after the sample size is computed, the power will be recomputed. This is only advantageous when the sample size is rounded up. Default is FALSE.
correct
If TRUE (default), a continuity correction will be used.

VALUE:

A data.frame containg the power, sample size and all of the input which was used to perform the computations.

DETAILS:

This function can be used to calculate sample size, power or minimum detectable difference. It determines what to compute base on the arguments provided. If p.alt or p2 is given, but n1 is not, then sample size is computed. If p.alt or p2 is given along with n1, then the power is computed. If only n1 is provided, the minimum detectable difference is computed using the default power of 0.80.

The formulas used for computing sample size are:
One Sample:
For a one-sample test of proportion where

    Ho: pi = p 
    Ha: pi = p.alt 
    Pr(reject Ho | Ho is true) = alpha 
    Pr(reject Ho | Ha is true) = power 
Sample size required to detect delta = abs(p.alt - p) :
    Z.a = qnorm(1 - alpha/2) {2 sided} 
    Z.a = qnorm(1 - alpha)   {1 sided} 
    Z.p = qnorm(power) 
    sd.null = sqrt( p*(1 - p) ) 
    sd.alt = sqrt( p.alt*(1 - p.alt) ) 
    n1 = { (sd.null * Z.a + sd.alt * Z.p) / delta }^2 
    n1 = n1 + 2 / delta  {continuity cor} 

Two Sample:
For a two-sample test of proportions where
    Ho: p2 - p = 0 
    Ha: p2 - p != 0 
    Pr(reject Ho | Ho is true) = alpha 
    Pr(reject Ho | Ha is true) = power 
Sample size required to detect delta = abs(p2 - p) :
    Z.a = qnorm(1 - alpha/2) {2 sided} 
    Z.a = qnorm(1 - alpha)   {1 sided} 
    Z.p = qnorm(power) 
    p.pool = (n1*p + n2*p2) / (n1 + n2) 
           = (p + prop.n2*p2) / (1 + prop.n2) 
    sd.null = sqrt( p.pool*(1 - p.pool) * (1 + 1/prop.n2) ) 
    sd.alt = sqrt( p*(1 - p) + p2*(1 - p2)/ prop.n2 ) 
    n1 = { (sd.null * Z.a + sd.alt * Z.p) / delta }^2 
    n1 = n1 + (prop.n2 + 1) / (prop.n2 * delta)  {continuity cor} 
    n2 = prop.n2 * n1 

REFERENCES:

Fleiss, Joseph L. (1981). Statistical Methods for Rates and Proportions. Wiley, New York. Rosner, Bernard (1990). Fundamentals of Biostatistics (Third Edition). PWS-Kent, Boston.

SEE ALSO:

normal.sample.size, prop.test.

EXAMPLES:

binomial.sample.size(p.alt = c(.6, .7, .8)) 
binomial.sample.size(p2 = .7, n1 = 100) 
binomial.sample.size(p = 0.02, p.alt = 0.05, n1 = 500) 
binomial.sample.size(p = .7, p2 = .85) 
binomial.sample.size(p = .7, p2 = .85, correct = F) 
binomial.sample.size(p = .7, p2 = .85, recompute = T) 
binomial.sample.size(p = .7, p2 = .85, n1 = 100, prop.n2 = 2) 
binomial.sample.size(p.alt = 0.6, n1 = c(50, 100, 200, 300), 
                alpha = c(.01, .05, .1)) 
binomial.sample.size(p2 = 0.6, n1 = c(50,100,200,400,800),  
                alpha = c(.01, .05, .1)) 
binomial.sample.size(p = .25, p2 = .4, alpha = c(.01, 0.02),  
                power = c(0.95, .90)) 
binomial.sample.size(p = .25, p2 = .4, alpha = c(.01, 0.02),  
                power = c(0.95, .90), expand = F) 
binomial.sample.size(p = 0.25, p2 = .40, alpha = 0.01, power = 0.95, 
                prop.n2 = c(5:10) / 10)