Description Usage Arguments Details Value Note Author(s) References See Also Examples
Compute the sample size necessary to achieve a specified power for a one or twosample ttest, given the scaled difference and significance level.
1 2 3 4 
delta.over.sigma 
numeric vector specifying the ratio of the true difference δ (δ = μ  μ_0 for the onesample case and δ = μ_1  μ_2 for the twosample case) to the population standard deviation (σ). This is also called the “scaled difference”. 
alpha 
numeric vector of numbers between 0 and 1 indicating the Type I error level
associated with the hypothesis test. The default value is 
power 
numeric vector of numbers between 0 and 1 indicating the power
associated with the hypothesis test. The default value is 
sample.type 
character string indicating whether to compute power based on a onesample or
twosample hypothesis test. When 
alternative 
character string indicating the kind of alternative hypothesis. The possible values are:

approx 
logical scalar indicating whether to compute the power based on an approximation to
the noncentral tdistribution. The default value is 
n2 
numeric vector of sample sizes for group 2. The default value is

round.up 
logical scalar indicating whether to round up the values of the computed
sample size(s) to the next smallest integer. The default value is

n.max 
positive integer greater than 1 indicating the maximum sample size when 
tol 
numeric scalar indicating the toloerance to use in the

maxiter 
positive integer indicating the maximum number of iterations
argument to pass to the 
Formulas for the power of the ttest for specified values of
the sample size, scaled difference, and Type I error level are given in
the help file for tTestPower
. The function tTestN
uses the uniroot
search algorithm to determine the
required sample size(s) for specified values of the power,
scaled difference, and Type I error level.
When sample.type="one.sample"
, tTestN
returns a numeric vector of sample sizes.
When sample.type="two.sample"
and n2
is not supplied,
equal sample sizes for each group is assumed and tTestN
returns a numeric vector of
sample sizes indicating the required sample size for each group.
When sample.type="two.sample"
and n2
is supplied,
tTestN
returns a list with two components called n1
and
n2
, specifying the sample sizes for each group.
See tTestPower
.
Steven P. Millard (EnvStats@ProbStatInfo.com)
See tTestPower
.
tTestPower
, tTestScaledMdd
, tTestAlpha
,
plotTTestDesign
, Normal,
t.test
, Hypothesis Tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101  # Look at how the required sample size for the onesample ttest
# increases with increasing required power:
seq(0.5, 0.9, by = 0.1)
#[1] 0.5 0.6 0.7 0.8 0.9
tTestN(delta.over.sigma = 0.5, power = seq(0.5, 0.9, by = 0.1))
#[1] 18 22 27 34 44
#
# Repeat the last example, but compute the sample size based on the
# approximation to the power instead of the exact method:
tTestN(delta.over.sigma = 0.5, power = seq(0.5, 0.9, by = 0.1),
approx = TRUE)
#[1] 18 22 27 34 45
#==========
# Look at how the required sample size for the twosample ttest
# decreases with increasing scaled difference:
seq(0.5, 2,by = 0.5)
#[1] 0.5 1.0 1.5 2.0
tTestN(delta.over.sigma = seq(0.5, 2, by = 0.5), sample.type = "two")
#[1] 105 27 13 8
#
# Look at how the required sample size for the twosample ttest decreases
# with increasing values of Type I error:
tTestN(delta.over.sigma = 0.5, alpha = c(0.001, 0.01, 0.05, 0.1),
sample.type="two")
#[1] 198 145 105 88
#
# For the twosample ttest, compare the total sample size required to
# detect a scaled difference of 1 for equal sample sizes versus the case
# when the sample size for the second group is constrained to be 20.
# Assume a 5% significance level and 95% power. Note that for the case
# of equal sample sizes, a total of 54 samples (27+27) are required,
# whereas when n2 is constrained to be 20, a total of 62 samples
# (42 + 20) are required.
tTestN(1, sample.type="two")
#[1] 27
tTestN(1, n2 = 20)
#$n1
#[1] 42
#
#$n2
#[1] 20
#==========
# Modifying the example on pages 214 to 215 of USEPA (2009), determine the
# required sample size to detect a mean aldicarb level greater than the MCL
# of 7 ppb at the third compliance well with a power of 95%, assuming the
# true mean is 10 or 14. Use the estimated standard deviation from the
# first four months of data to estimate the true population standard
# deviation, use a Type I error level of alpha=0.01, and assume an
# upper onesided alternative (third compliance well mean larger than 7).
# (The data are stored in EPA.09.Ex.21.1.aldicarb.df.)
# Note that the required sample size changes from 11 to 5 as the true mean
# increases from 10 to 14.
EPA.09.Ex.21.1.aldicarb.df
# Month Well Aldicarb.ppb
#1 1 Well.1 19.9
#2 2 Well.1 29.6
#3 3 Well.1 18.7
#4 4 Well.1 24.2
#5 1 Well.2 23.7
#6 2 Well.2 21.9
#7 3 Well.2 26.9
#8 4 Well.2 26.1
#9 1 Well.3 5.6
#10 2 Well.3 3.3
#11 3 Well.3 2.3
#12 4 Well.3 6.9
sigma < with(EPA.09.Ex.21.1.aldicarb.df,
sd(Aldicarb.ppb[Well == "Well.3"]))
sigma
#[1] 2.101388
tTestN(delta.over.sigma = (c(10, 14)  7)/sigma,
alpha = 0.01, sample.type="one", alternative="greater")
#[1] 11 5
# Clean up
#
rm(sigma)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.