Power and Sample Size

DESCRIPTION:

Computes power and sample size requirements for one and two-sample T tests.

USAGE:

normal.sample.size(mean2, mean.alt, mean=0, sd1=1, sd2=sd1, 
    power=0.8, alpha=0.05, n1=NULL, n2=NULL, prop.n2=1, 
    one.sample=missing(sd2),  alternative="two.sided", 
    expand.args=T, exact.n=F, recompute.power=F) 

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 below.

mean2
The second mean in a two-sample test.
mean.alt
The alternative mean in a one-sample test.
n1
The sample size in a one or two-sample test.

OPTIONAL ARGUMENTS:

mean
The first mean in a two-sample test or the null mean in a one-sample test.
sd1
Standard deviation for the first sample. Default is 1.
sd2
Standard deviation for the second sample. Ignored if one-sample test. Default is 1.
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.

VALUE:

A data.frame containing 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 mean.alt or mean2 is given, but n1 is not, then sample size is computed. If mean.alt or mean2 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 T-test where

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

Two Sample:
For a two-sample T-test where
        Ho: mean2 - mean = 0  
        Ha: mean2 - mean != 0    
        n2 = prop.n2 * n1 
        Pr(reject Ho | Ho is true) = alpha 
        Pr(reject Ho | Ha is true) = power 
Sample size required to detect delta = abs(mean2 - mean) :
        Z.a = qnorm(1 - alpha/2) {2 sided} 
        Z.a = qnorm(1 - alpha)   {1 sided} 
        Z.p = qnorm(power) 
        n = { sd1 * (Z.a + Z.p) / delta }^2 
        n1 = n * (1 + (sd2^2 / prop.n2 * sd1^2)) 
        n2 = prop.n2 * n1 

REFERENCES:

Rosner, Bernard (1990). Fundamentals of Biostatistics (Third Edition). PWS-Kent, Boston. Fisher, Lloyd D. and Van Belle, Gerald (1993). Biostatistics. Wiley, New York.

SEE ALSO:

binomial.sample.size, t.test.

EXAMPLES:

normal.sample.size(mean.alt = 0.3)   # one-sample 
normal.sample.size(mean2 = 0.3)      # two-sample                
normal.sample.size(mean2 = 0.3, recompute = T)  
normal.sample.size(mean2 = 0.3, exact.n = T)  
normal.sample.size(mean2 = 0.3, alt = "greater") 
normal.sample.size(mean = 100, mean.alt = 94, sd1 = 15,  
        power = 0.9, alt = "less") 
normal.sample.size(mean=120, mean.alt=c(115, 110), sd1=c(sqrt(625), 30), 
        alpha = c(0.05, 0.01), power = c(0.8,0.9), alt="l") 
normal.sample.size(mean=120, mean.alt=c(115, 110), sd1=c(sqrt(625), 30), 
        alpha = c(0.05, 0.01), power = c(0.8,0.9), expand=F)