Plots for a Sampling Design Based on a One- or Two-Sample t-Test
Source:R/plotTTestDesign.R
plotTTestDesign.RdCreate plots involving sample size, power, scaled difference, and significance level for a one- or two-sample t-test.
Usage
plotTTestDesign(x.var = "n", y.var = "power", range.x.var = NULL,
n.or.n1 = 25, n2 = n.or.n1,
delta.over.sigma = switch(alternative, greater = 0.5, less = -0.5,
two.sided = ifelse(two.sided.direction == "greater", 0.5, -0.5)),
alpha = 0.05, power = 0.95,
sample.type = ifelse(!missing(n2), "two.sample", "one.sample"),
alternative = "two.sided", two.sided.direction = "greater", approx = FALSE,
round.up = FALSE, n.max = 5000, tol = 1e-07, maxiter = 1000, plot.it = TRUE,
add = FALSE, n.points = 50, plot.col = "black", plot.lwd = 3 * par("cex"),
plot.lty = 1, digits = .Options$digits, ..., main = NULL, xlab = NULL,
ylab = NULL, type = "l")Arguments
- x.var
character string indicating what variable to use for the x-axis. Possible values are
"n"(sample size; the default),"delta.over.sigma"(scaled minimal detectable difference),"power"(power of the test), and"alpha"(significance level of the test).- y.var
character string indicating what variable to use for the y-axis. Possible values are
"power"(power of the test; the default),"delta.over.sigma"(scaled minimal detectable difference), and"n"(sample size).- range.x.var
numeric vector of length 2 indicating the range of the x-variable to use for the plot. The default value depends on the value of
x.var. Whenx.var="n"the default value isc(2,50). Whenx.var="delta.over.sigma"andalternative="greater"oralternative="two.sided"andtwo.sided.direction="greater", the default value isc(0.5, 2). Whenx.var="delta.over.sigma"andalternative="less"oralternative="two.sided"andtwo.sided.direction="less", the default value is-c(2, 0.5). Whenx.var="power"the default value isc(alpha + .Machine$double.eps, 0.95). Whenx.var="alpha", the default value isc(0.01, 0.2).- n.or.n1
numeric scalar indicating the sample size. The default value is
n.or.n1=25. Whensample.type="one.sample",n.or.n1denotes the number of observations in the single sample. Whensample.type="two.sample",n.or.n1denotes the number of observations from group 1. Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are not allowed. This argument is ignored if eitherx.var="n"ory.var="n".- n2
numeric scalar indicating the sample size for group 2. The default value is the value of
n.or.n1. Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are not allowed. This argument is ignored whensample.type="one.sample".- delta.over.sigma
numeric scalar specifying the ratio of the true difference (\(\delta\)) to the population standard deviation (\(\sigma\)). This is also called the "scaled difference". When
alternative="greater"oralternative="two.sided"andtwo.sided.direction="greater", the default value isdelta.over.sigma=0.5. Whenalternative="less"oralternative="two.sided"andtwo.sided.direction="less", the default value isdelta.over.sigma=-0.5. This argument is ignored whenx.var="delta.over.sigma"ory.var="delta.over.sigma".- alpha
numeric scalar between 0 and 1 indicating the Type I error level associated with the hypothesis test. The default value is
alpha=0.05. This argument is ignored whenx.var="alpha".- power
numeric scalar between 0 and 1 indicating the power associated with the hypothesis test. The default value is
power=0.95. This argument is ignored whenx.var="power"ory.var="power".- sample.type
character string indicating whether the design is based on a one-sample or two-sample t-test. When
sample.type="one.sample", the computations for the plot are based on a one-sample t-test. Whensample.type="two.sample", the computations for the plot are based on a two-sample t-test. 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".- two.sided.direction
character string indicating the direction (positive or negative) for the scaled minimal detectable difference when
alternative="two.sided". Whentwo.sided.direction="greater"(the default), the scaled minimal detectable difference is positive. Whentwo.sided.direction="less", the scaled minimal detectable difference is negative. This argument is ignored unlessalternative="two.sided"and eitherx.var="delta"ory.var="delta".- approx
logical scalar indicating whether to compute the power based on an approximation to the non-central t-distribution. The default value is
approx=FALSE.- 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
round.up=FALSE. This argument is ignored unlessy.var="n".- n.max
for the case when
y.var="n", a positive integer greater than 1 indicating the maximum sample size whensample.type="one.sample"or the maximum sample size for group 1 whensample.type="two.sample". The default value isn.max=5000.- tol
numeric scalar relevant to the case when
y.var="n"ory.var="delta.over.sigma". This argument is passed to theunirootfunction and indicates the tolerance to use in the search algorithm. The default value istol=1e-7.- maxiter
numeric scalar relevant to the case when
y.var="n"andapprox=FALSE(i.e., when the power is based on the exact test), or wheny.var="delta.over.sigma". This argument is passed to theunirootfunction and is a positive integer indicating the maximum number of iterations. The default value ismaxiter=1000.- plot.it
a logical scalar indicating whether to create a new plot or add to the existing plot (see
add) on the current graphics device. Ifplot.it=FALSE, no plot is produced, but a list of (x,y) values is returned (see VALUE). The default value isplot.it=TRUE.- add
a logical scalar indicating whether to add the design plot to the existing plot (
add=TRUE), or to create a plot from scratch (add=FALSE). The default value isadd=FALSE. This argument is ignored ifplot.it=FALSE.- n.points
a numeric scalar specifying how many (x,y) pairs to use to produce the plot. There are
n.pointsx-values evenly spaced betweenrange.x.var[1]andrange.x.var[2]. The default value isn.points=50.- plot.col
a numeric scalar or character string determining the color of the plotted line or points. The default value is
plot.col="black". See the entry forcolin the help file forparfor more information.- plot.lwd
a numeric scalar determining the width of the plotted line. The default value is
3*par("cex"). See the entry forlwdin the help file forparfor more information.- plot.lty
a numeric scalar determining the line type of the plotted line. The default value is
plot.lty=1. See the entry forltyin the help file forparfor more information.- digits
a scalar indicating how many significant digits to print out on the plot. The default value is the current setting of
options("digits").- main, xlab, ylab, type, ...
additional graphical parameters (see
par).
Details
See the help files for tTestPower, tTestN, and
tTestScaledMdd for information on how to compute the power,
sample size, or scaled minimal detectable difference for a one- or two-sample
t-test.
Value
plotTTestDesign invisibly returns a list with components
x.var and y.var, giving coordinates of the points that have
been or would have been plotted.
References
See the help files for tTestPower, tTestN, and
tTestScaledMdd.
Author
Steven P. Millard (EnvStats@ProbStatInfo.com)
Note
See the help files for tTestPower, tTestN, and
tTestScaledMdd.
Examples
# Look at the relationship between power and sample size for a two-sample t-test,
# assuming a scaled difference of 0.5 and a 5% significance level:
if (interactive()) dev.new()
plotTTestDesign(sample.type = "two")
#----------
# For a two-sample t-test, plot sample size vs. the scaled minimal detectable
# difference for various levels of power, using a 5% significance level:
if (interactive()) dev.new()
plotTTestDesign(x.var = "delta.over.sigma", y.var = "n", sample.type = "two",
ylim = c(0, 110), main="")
plotTTestDesign(x.var = "delta.over.sigma", y.var = "n", sample.type = "two",
power = 0.9, add = TRUE, plot.col = "red")
plotTTestDesign(x.var = "delta.over.sigma", y.var = "n", sample.type = "two",
power = 0.8, add = TRUE, plot.col = "blue")
legend("topright", c("95%", "90%", "80%"), lty = 1,
lwd = 3 * par("cex"), col = c("black", "red", "blue"), bty = "n")
title(main = paste("Sample Size vs. Scaled Difference for",
"Two-Sample t-Test, with Alpha=0.05 and Various Powers",
sep="\n"))
#==========
# Modifying the example on pages 21-4 to 21-5 of USEPA (2009), look at
# power versus scaled minimal detectable difference for various sample
# sizes in the context of the problem of using a one-sample t-test to
# compare the mean for the well with the MCL of 7 ppb. Use alpha = 0.01,
# assume an upper one-sided alternative (i.e., compliance well mean larger
# than 7 ppb).
if (interactive()) dev.new()
plotTTestDesign(x.var = "delta.over.sigma", y.var = "power",
range.x.var = c(0.5, 2), n.or.n1 = 8, alpha = 0.01,
alternative = "greater", ylim = c(0, 1), main = "")
plotTTestDesign(x.var = "delta.over.sigma", y.var = "power",
range.x.var = c(0.5, 2), n.or.n1 = 6, alpha = 0.01,
alternative = "greater", add = TRUE, plot.col = "red")
plotTTestDesign(x.var = "delta.over.sigma", y.var = "power",
range.x.var = c(0.5, 2), n.or.n1 = 4, alpha = 0.01,
alternative = "greater", add = TRUE, plot.col = "blue")
legend("topleft", paste("N =", c(8, 6, 4)), lty = 1, lwd = 3 * par("cex"),
col = c("black", "red", "blue"), bty = "n")
title(main = paste("Power vs. Scaled Difference for One-Sample t-Test",
"with Alpha=0.01 and Various Sample Sizes", sep="\n"))
#==========
# Clean up
#---------
graphics.off()