Compute the Power of a One- or Two-Sample Proportion Test
Source:R/propTestPower.R
propTestPower.RdCompute the power of a one- or two-sample proportion test, given the sample size(s), true proportion(s), and significance level.
Usage
propTestPower(n.or.n1, p.or.p1 = 0.5, n2 = n.or.n1,
p0.or.p2 = 0.5, alpha = 0.05, sample.type = "one.sample",
alternative = "two.sided", approx = TRUE,
correct = sample.type == "two.sample", warn = TRUE,
return.exact.list = TRUE)Arguments
- n.or.n1
numeric vector of sample sizes. When
sample.type="one.sample", this argument denotes \(n\), the number of observations in the single sample. Whensample.type="two.sample", this argument denotes \(n_1\), the number of observations from group 1. Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are not allowed.- p.or.p1
numeric vector of proportions. When
sample.type="one.sample", this argument denotes the true value of \(p\), the probability of “success”. Whensample.type="two.sample", this argument denotes the value of \(p_1\), the probability of “success” in group 1. The default value isp.or.p1=0.5. Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are not allowed.- n2
numeric vector of sample sizes for group 2. The default value is
n2=n.or.n1. This argument is ignored whensample.type="one.sample". Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are not allowed.- p0.or.p2
numeric vector of proportions. When
sample.type="one.sample", this argument denotes the hypothesized value of \(p\), the probability of “success”. Whensample.type="two.sample", this argument denotes the value of \(p_2\), the probability of “success” in group 2. The default value isp0.or.p2=0.5. Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are not allowed.- alpha
numeric vector of numbers between 0 and 1 indicating the Type I error level associated with the hypothesis test. The default value is
alpha=0.05.- sample.type
character string indicating whether to compute power based on a one-sample or two-sample hypothesis test. When
sample.type="one.sample", the computed power is based on a hypothesis test for a single proportion. Whensample.type="two.sample", the computed power is based on a hypothesis test for the difference between two proportions. The default value issample.type="one.sample".- alternative
character string indicating the kind of alternative hypothesis. The possible values are
"two.sided"(the default),"less", and"greater".- approx
logical scalar indicating whether to compute the power based on the normal approximation to the binomial distribution. The default value is
approx=TRUE. Currently, the exact method (approx=FALSE) is only available for the one-sample case (i.e.,sample.type="one.sample").- correct
logical scalar indicating whether to use the continuity correction when
approx=TRUE. The default value isapprox=TRUEwhensample.type="two.sample"andapprox=FALSEwhensample.type="one.sample". This argument is ignored whenapprox=FALSE.- warn
logical scalar indicating whether to issue a warning. The default value is
warn=TRUE. Whenapprox=TRUE(power based on the normal approximation) andwarn=TRUE, a warning is issued for cases when the normal approximation to the binomial distribution probably is not accurate. Whenapprox=FALSE(power based on the exact test) andwarn=TRUE, a warning is issued when the user-supplied sample size is too small to yield a significance level less than or equal to the user-supplied value ofalpha.- return.exact.list
logical scalar relevant to the case when
approx=FALSE(i.e., when the power is based on the exact test). This argument indicates whether to return a list containing extra information about the exact test in addition to the power of the exact test. By default,propTestPowerreturns only a vector containing the computed power(s) (see the VALUE section below). Whenreturn.exact.list=TRUE(the default) andapprox=FALSE,propTestPowerreturns a list with components indicating the power of the exact test, the true significance level associated with the exact test, and the critical values associated with the exact test (see the DETAILS section for more information).
Details
If the arguments n.or.n1, p.or.p1, n2, p0.or.p2, and
alpha are not all the same length, they are replicated to be the same length
as the length of the longest argument.
The power is based on the difference p.or.p1 - p0.or.p2.
One-Sample Case (sample.type="one.sample").
approx=TRUEWhen
sample.type="one.sample"andapprox=TRUE, power is computed based on the test that uses the normal approximation to the binomial distribution; see the help file forprop.test. The formula for this test and its associated power is presented in most standard statistics texts, including Zar (2010, pp. 534-537, 539-541).approx=FALSEWhen
sample.type="one.sample"andapprox=FALSE, power is computed based on the exact binomial test; see the help file forbinom.test. The formula for this test and its associated power is presented in most standard statistics texts, including Zar (2010, pp. 532-534, 539) and Millard and Neerchal (2001, pp. 385-386, 504-506).
Two-Sample Case (sample.type="two.sample").
When sample.type="two.sample", power is computed based on the test that uses the
normal approximation to the binomial distribution;
see the help file for prop.test.
The formula for this test and its associated power is presented in standard statistics texts,
including Zar (2010, pp. 549-550, 552-553) and
Millard and Neerchal (2001, pp. 443-445, 508-510).
Value
By default, propTestPower returns a numeric vector of powers.
For the one-sample proportion test (sample.type="one.sample"),
when approx=FALSE and return.exact.list=TRUE, propTestPower
returns a list with the following components:
- power
numeric vector of powers.
- alpha
numeric vector containing the true significance levels. Because of the discrete nature of the binomial distribution, the true significance levels usually do not equal the significance level supplied by the user in the argument
alpha.- q.critical.lower
numeric vector of lower critical values for rejecting the null hypothesis. If the observed number of "successes" is less than or equal to these values, the null hypothesis is rejected. (Not present if
alternative="greater".)- q.critical.upper
numeric vector of upper critical values for rejecting the null hypothesis. If the observed number of "successes" is greater than these values, the null hypothesis is rejected. (Not present if
alternative="less".)
References
Berthouex, P.M., and L.C. Brown. (1994). Statistics for Environmental Engineers. Lewis Publishers, Boca Raton, FL, Chapter 15.
Casagrande, J.T., M.C. Pike, and P.G. Smith. (1978). An Improved Approximation Formula for Calculating Sample Sizes for Comparing Two Binomial Distributions. Biometrics 34, 483-486.
Fleiss, J. L. (1981). Statistical Methods for Rates and Proportions. Second Edition. John Wiley and Sons, New York, Chapters 1-2.
Gilbert, R.O. (1987). Statistical Methods for Environmental Pollution Monitoring. Van Nostrand Reinhold, New York, NY.
Haseman, J.K. (1978). Exact Sample Sizes for Use with the Fisher-Irwin Test for 2x2 Tables. Biometrics 34, 106-109.
Millard, S.P., and N. Neerchal. (2001). Environmental Statistics with S-Plus. CRC Press, Boca Raton, FL.
Zar, J.H. (2010). Biostatistical Analysis. Fifth Edition. Prentice-Hall, Upper Saddle River, NJ.
Author
Steven P. Millard (EnvStats@ProbStatInfo.com)
Note
The binomial distribution is used to model processes with binary (Yes-No, Success-Failure, Heads-Tails, etc.) outcomes. It is assumed that the outcome of any one trial is independent of any other trial, and that the probability of “success”, \(p\), is the same on each trial. A binomial discrete random variable \(X\) is the number of "successes" in \(n\) independent trials. A special case of the binomial distribution occurs when \(n=1\), in which case \(X\) is also called a Bernoulli random variable.
In the context of environmental statistics, the binomial distribution is sometimes used to model the proportion of times a chemical concentration exceeds a set standard in a given period of time (e.g., Gilbert, 1987, p.143), or to compare the proportion of detects in a compliance well vs. a background well (e.g., USEPA, 1989b, Chapter 8, p.3-7).
In the course of designing a sampling program, an environmental scientist may wish to determine the
relationship between sample size, power, significance level, and the difference between the
hypothesized and true proportions if one of the objectives of the sampling program is to
determine whether a proprtion differs from a specified level or two proportions differ from each other.
The functions propTestPower, propTestN, propTestMdd, and
plotPropTestDesign can be used to investigate these relationships for the case of
binomial proportions.
Studying the two-sample proportion test, Haseman (1978) found that the formulas used to estimate the power that do not incorporate the continuity correction tend to underestimate the power. Casagrande, Pike, and Smith (1978) found that the formulas that do incorporate the continuity correction provide an excellent approximation.
Examples
# Look at how the power of the one-sample proportion test
# increases with increasing sample size:
seq(20, 50, by=10)
#> [1] 20 30 40 50
#[1] 20 30 40 50
power <- propTestPower(n.or.n1 = seq(20, 50, by=10),
p.or.p1 = 0.7, p0 = 0.5)
round(power, 2)
#> [1] 0.43 0.60 0.73 0.83
#[1] 0.43 0.60 0.73 0.83
#----------
# Repeat the last example, but compute the power based on
# the exact test instead of the approximation.
# Note that the significance level varies with sample size and
# never attains the requested level of 0.05.
prop.test.list <- propTestPower(n.or.n1 = seq(20, 50, by=10),
p.or.p1 = 0.7, p0 = 0.5, approx=FALSE)
lapply(prop.test.list, round, 2)
#> $power
#> [1] 0.42 0.59 0.70 0.78
#>
#> $alpha
#> [1] 0.04 0.04 0.04 0.03
#>
#> $q.critical.lower
#> [1] 5 9 13 17
#>
#> $q.critical.upper
#> [1] 14 20 26 32
#>
#$power:
#[1] 0.42 0.59 0.70 0.78
#
#$alpha:
#[1] 0.04 0.04 0.04 0.03
#
#$q.critical.lower:
#[1] 5 9 13 17
#
#$q.critical.upper:
#[1] 14 20 26 32
#==========
# Look at how the power of the two-sample proportion test
# increases with increasing difference between the two
# population proportions:
seq(0.5, 0.1, by=-0.1)
#> [1] 0.5 0.4 0.3 0.2 0.1
#[1] 0.5 0.4 0.3 0.2 0.1
power <- propTestPower(30, sample.type = "two",
p.or.p1 = seq(0.5, 0.1, by=-0.1))
#> Warning: The sample sizes 'n1' and 'n2' are too small, relative to the given values of 'p1' and 'p2', for the normal approximation to work well for the following element indices:
#> 5
#>
#Warning message:
#In propTestPower(30, sample.type = "two", p.or.p1 = seq(0.5, 0.1, :
#The sample sizes 'n1' and 'n2' are too small, relative to the given
# values of 'p1' and 'p2', for the normal approximation to work well
# for the following element indices:
# 5
round(power, 2)
#> [1] 0.05 0.08 0.26 0.59 0.90
#[1] 0.05 0.08 0.26 0.59 0.90
#----------
# Look at how the power of the two-sample proportion test
# increases with increasing values of Type I error:
power <- propTestPower(30, sample.type = "two",
p.or.p1 = 0.7,
alpha = c(0.001, 0.01, 0.05, 0.1))
round(power, 2)
#> [1] 0.02 0.10 0.26 0.37
#[1] 0.02 0.10 0.26 0.37
#==========
# Clean up
#---------
rm(power, prop.test.list)
#==========
# Modifying the example on pages 8-5 to 8-7 of USEPA (1989b),
# determine how adding another 20 observations to the background
# well to increase the sample size from 24 to 44 will affect the
# power of detecting a difference in the proportion of detects of
# cadmium between the background and compliance wells. Set the
# compliance well to "group 1" and set the background well to
# "group 2". Assume the true probability of a "detect" at the
# background well is 1/3, set the probability of a "detect" at the
# compliance well to 0.4, use a 5% significance level, and use the
# upper one-sided alternative (probability of a "detect" at the
# compliance well is greater than the probability of a "detect" at
# the background well).
# (The original data are stored in EPA.89b.cadmium.df.)
#
# Note that the power does increase (from 9% to 12%), but is relatively
# very small.
EPA.89b.cadmium.df
#> Cadmium.orig Cadmium Censored Well.type
#> 1 0.1 0.100 FALSE Background
#> 2 0.12 0.120 FALSE Background
#> 3 BDL 0.000 TRUE Background
#> 4 0.26 0.260 FALSE Background
#> 5 BDL 0.000 TRUE Background
#> 6 0.1 0.100 FALSE Background
#> 7 BDL 0.000 TRUE Background
#> 8 0.014 0.014 FALSE Background
#> 9 BDL 0.000 TRUE Background
#> 10 BDL 0.000 TRUE Background
#> 11 BDL 0.000 TRUE Background
#> 12 BDL 0.000 TRUE Background
#> 13 BDL 0.000 TRUE Background
#> 14 0.12 0.120 FALSE Background
#> 15 BDL 0.000 TRUE Background
#> 16 0.21 0.210 FALSE Background
#> 17 BDL 0.000 TRUE Background
#> 18 0.12 0.120 FALSE Background
#> 19 BDL 0.000 TRUE Background
#> 20 BDL 0.000 TRUE Background
#> 21 BDL 0.000 TRUE Background
#> 22 BDL 0.000 TRUE Background
#> 23 BDL 0.000 TRUE Background
#> 24 BDL 0.000 TRUE Background
#> 25 0.12 0.120 FALSE Compliance
#> 26 0.08 0.080 FALSE Compliance
#> 27 BDL 0.000 TRUE Compliance
#> 28 0.2 0.200 FALSE Compliance
#> 29 BDL 0.000 TRUE Compliance
#> 30 0.1 0.100 FALSE Compliance
#> 31 BDL 0.000 TRUE Compliance
#> 32 0.012 0.012 FALSE Compliance
#> 33 BDL 0.000 TRUE Compliance
#> 34 BDL 0.000 TRUE Compliance
#> 35 BDL 0.000 TRUE Compliance
#> 36 BDL 0.000 TRUE Compliance
#> 37 BDL 0.000 TRUE Compliance
#> 38 0.12 0.120 FALSE Compliance
#> 39 0.07 0.070 FALSE Compliance
#> 40 BDL 0.000 TRUE Compliance
#> 41 0.19 0.190 FALSE Compliance
#> 42 BDL 0.000 TRUE Compliance
#> 43 0.1 0.100 FALSE Compliance
#> 44 BDL 0.000 TRUE Compliance
#> 45 0.01 0.010 FALSE Compliance
#> 46 BDL 0.000 TRUE Compliance
#> 47 BDL 0.000 TRUE Compliance
#> 48 BDL 0.000 TRUE Compliance
#> 49 BDL 0.000 TRUE Compliance
#> 50 BDL 0.000 TRUE Compliance
#> 51 0.11 0.110 FALSE Compliance
#> 52 0.06 0.060 FALSE Compliance
#> 53 BDL 0.000 TRUE Compliance
#> 54 0.23 0.230 FALSE Compliance
#> 55 BDL 0.000 TRUE Compliance
#> 56 0.11 0.110 FALSE Compliance
#> 57 BDL 0.000 TRUE Compliance
#> 58 0.031 0.031 FALSE Compliance
#> 59 BDL 0.000 TRUE Compliance
#> 60 BDL 0.000 TRUE Compliance
#> 61 BDL 0.000 TRUE Compliance
#> 62 BDL 0.000 TRUE Compliance
#> 63 BDL 0.000 TRUE Compliance
#> 64 0.12 0.120 FALSE Compliance
#> 65 0.08 0.080 FALSE Compliance
#> 66 BDL 0.000 TRUE Compliance
#> 67 0.26 0.260 FALSE Compliance
#> 68 BDL 0.000 TRUE Compliance
#> 69 0.02 0.020 FALSE Compliance
#> 70 BDL 0.000 TRUE Compliance
#> 71 0.024 0.024 FALSE Compliance
#> 72 BDL 0.000 TRUE Compliance
#> 73 BDL 0.000 TRUE Compliance
#> 74 BDL 0.000 TRUE Compliance
#> 75 BDL 0.000 TRUE Compliance
#> 76 BDL 0.000 TRUE Compliance
#> 77 0.1 0.100 FALSE Compliance
#> 78 0.04 0.040 FALSE Compliance
#> 79 BDL 0.000 TRUE Compliance
#> 80 BDL 0.000 TRUE Compliance
#> 81 0.1 0.100 FALSE Compliance
#> 82 BDL 0.000 TRUE Compliance
#> 83 0.01 0.010 FALSE Compliance
#> 84 BDL 0.000 TRUE Compliance
#> 85 BDL 0.000 TRUE Compliance
#> 86 BDL 0.000 TRUE Compliance
#> 87 BDL 0.000 TRUE Compliance
#> 88 BDL 0.000 TRUE Compliance
# Cadmium.orig Cadmium Censored Well.type
#1 0.1 0.100 FALSE Background
#2 0.12 0.120 FALSE Background
#3 BDL 0.000 TRUE Background
# ..........................................
#86 BDL 0.000 TRUE Compliance
#87 BDL 0.000 TRUE Compliance
#88 BDL 0.000 TRUE Compliance
p.hat.back <- with(EPA.89b.cadmium.df,
mean(!Censored[Well.type=="Background"]))
p.hat.back
#> [1] 0.3333333
#[1] 0.3333333
p.hat.comp <- with(EPA.89b.cadmium.df,
mean(!Censored[Well.type=="Compliance"]))
p.hat.comp
#> [1] 0.375
#[1] 0.375
n.back <- with(EPA.89b.cadmium.df,
sum(Well.type == "Background"))
n.back
#> [1] 24
#[1] 24
n.comp <- with(EPA.89b.cadmium.df,
sum(Well.type == "Compliance"))
n.comp
#> [1] 64
#[1] 64
propTestPower(n.or.n1 = n.comp,
p.or.p1 = 0.4,
n2 = c(n.back, 44), p0.or.p2 = p.hat.back,
alt="greater", sample.type="two")
#> [1] 0.08953013 0.12421135
#[1] 0.08953013 0.12421135
#----------
# Clean up
#---------
rm(p.hat.back, p.hat.comp, n.back, n.comp)