R/twoSamplePermutationTestLocation.R
twoSamplePermutationTestLocation.RdPerform a two-sample or paired-sample randomization (permutation) test for location based on either means or medians.
twoSamplePermutationTestLocation(x, y, fcn = "mean", alternative = "two.sided",
mu1.minus.mu2 = 0, paired = FALSE, exact = FALSE, n.permutations = 5000,
seed = NULL, tol = sqrt(.Machine$double.eps))Extended methods and derivations are available in
Nonparametric estimation and two-sample tests: twoSamplePermutationTestLocation.
Open the vignette locally with
vignette("nonparametric-methods", package = "EnvStats").
numeric vector of observations from population 1.
Missing (NA), undefined (NaN), and infinite (Inf, -Inf)
values are allowed but will be removed.
numeric vector of observations from population 2.
Missing (NA), undefined (NaN), and infinite (Inf, -Inf)
values are allowed but will be removed.
In the case when paired=TRUE, the argument y must have the same number
of elements as the argument x.
character string indicating which location parameter to compare between the two
groups. The possible values are fcn="mean" (the default) and
fcn="median". This argument is ignored when paired=TRUE.
character string indicating the kind of alternative hypothesis. The possible values
are "two.sided" (the default), "less", and "greater".
numeric scalar indicating the hypothesized value of the difference between the
means or medians. The default value is mu1.minus.mu2=0.
logical scalar indicating whether to perform a paired or two-sample permutation
test. The possible values are paired=FALSE (the default; indicates a
two-sample permutation test) and paired=TRUE (indicates take differences of
pairs and perform a one-sample permutation test).
logical scalar indicating whether to perform the exact permutation test (i.e.,
enumerate all possible permutations) or simply sample from the permutation
distribution. The default value is exact=FALSE.
integer indicating how many times to sample from the permutation distribution when
exact=FALSE. The default value is n.permutations=5000.
This argument is ignored when exact=TRUE.
positive integer to pass to the R function set.seed. The
default is seed=NULL, in which case the current value of
.Random.seed is used.
Using the seed argument lets you reproduce the exact same result if all
other arguments stay the same.
numeric scalar indicating the tolerance to use for computing the p-value for the
two-sample permutation test. The default value is tol=sqrt(.Machine$double.eps). See the DETAILS section below for more
information.
A list of class "permutationTest" containing the results of the hypothesis
test. See the help file for permutationTest.object for details.
Efron, B., and R.J. Tibshirani. (1993). An Introduction to the Bootstrap. Chapman and Hall, New York, Chapter 15.
Manly, B.F.J. (2007). Randomization, Bootstrap and Monte Carlo Methods in Biology. Third Edition. Chapman & Hall, New York, Chapter 6.
Millard, S.P., and N.K. Neerchal. (2001). Environmental Statistics with S-PLUS. CRC Press, Boca Raton, FL, pp.426–431.
A frequent question in environmental statistics is “Is the concentration of chemical X in Area A greater than the concentration of chemical X in Area B?”. For example, in groundwater detection monitoring at hazardous and solid waste sites, the concentration of a chemical in the groundwater at a downgradient well must be compared to “background”. If the concentration is “above” the background then the site enters assessment monitoring. As another example, soil cleanup at a Superfund site may involve comparing the concentration of a chemical in the soil at a “cleaned up” site with the concentration at a “background” site. If the concentration at the “cleaned up” site is “greater” than the background concentration, then further investigation and remedial action may be required. Determining what it means for the chemical concentration to be “greater” than background is a policy decision: you may want to compare averages, medians, 95'th percentiles, etc.
Hypothesis tests you can use to compare “location” between two groups include: Student's t-test, Fisher's randomization test (described in this help file), the Wilcoxon rank sum test, other two-sample linear rank tests, the quantile test, and a test based on a bootstrap confidence interval.
# Generate 10 observations from a lognormal distribution with parameters
# mean=5 and cv=2, and and 20 observations from a lognormal distribution with
# parameters mean=10 and cv=2. Test the null hypothesis that the means of the
# two distributions are the same against the alternative that the mean for
# group 1 is less than the mean for group 2.
# (Note: the call to set.seed allows you to reproduce the same data
# (dat1 and dat2), and setting the argument seed=732 in the call to
# twoSamplePermutationTestLocation() lets you reproduce this example by
# getting the same sample from the permutation distribution).
set.seed(256)
dat1 <- rlnormAlt(10, mean = 5, cv = 2)
dat2 <- rlnormAlt(20, mean = 10, cv = 2)
test.list <- twoSamplePermutationTestLocation(dat1, dat2,
alternative = "less", seed = 732)
# Print the results of the test
#------------------------------
test.list
#>
#> Results of Hypothesis Test
#> --------------------------
#>
#> Null Hypothesis: mu.x-mu.y = 0
#>
#> Alternative Hypothesis: True mu.x-mu.y is less than 0
#>
#> Test Name: Two-Sample Permutation Test
#> Based on Differences in Means
#> (Based on Sampling
#> Permutation Distribution
#> 5000 Times)
#>
#> Estimated Parameter(s): mean of x = 2.253439
#> mean of y = 11.825430
#>
#> Data: x = dat1
#> y = dat2
#>
#> Sample Sizes: nx = 10
#> ny = 20
#>
#> Test Statistic: mean.x - mean.y = -9.571991
#>
#> P-value: 0.0012
#>
#Results of Hypothesis Test
#--------------------------
#
#Null Hypothesis: mu.x-mu.y = 0
#
#Alternative Hypothesis: True mu.x-mu.y is less than 0
#
#Test Name: Two-Sample Permutation Test
# Based on Differences in Means
# (Based on Sampling
# Permutation Distribution
# 5000 Times)
#
#Estimated Parameter(s): mean of x = 2.253439
# mean of y = 11.825430
#
#Data: x = dat1
# y = dat2
#
#Sample Sizes: nx = 10
# ny = 20
#
#Test Statistic: mean.x - mean.y = -9.571991
#
#P-value: 0.001
# Plot the results of the test
#-----------------------------
dev.new()
plot(test.list)
#==========
# The guidance document "Statistical Methods for Evaluating the Attainment of
# Cleanup Standards, Volume 3: Reference-Based Standards for Soils and Solid
# Media" (USEPA, 1994b, pp. 6.22-6.25) contains observations of
# 1,2,3,4-Tetrachlorobenzene (TcCB) in ppb at a Reference Area and a Cleanup Area.
# These data are stored in the data frame EPA.94b.tccb.df. Use the
# two-sample permutation test to test for a difference in means between the
# two areas vs. the alternative that the mean in the Cleanup Area is greater.
# Do the same thing for the medians.
#
# The permutation test based on comparing means shows a significant differnce,
# while the one based on comparing medians does not.
# First test for a difference in the means.
#------------------------------------------
mean.list <- with(EPA.94b.tccb.df,
twoSamplePermutationTestLocation(
TcCB[Area=="Cleanup"], TcCB[Area=="Reference"],
alternative = "greater", seed = 47))
mean.list
#>
#> Results of Hypothesis Test
#> --------------------------
#>
#> Null Hypothesis: mu.x-mu.y = 0
#>
#> Alternative Hypothesis: True mu.x-mu.y is greater than 0
#>
#> Test Name: Two-Sample Permutation Test
#> Based on Differences in Means
#> (Based on Sampling
#> Permutation Distribution
#> 5000 Times)
#>
#> Estimated Parameter(s): mean of x = 3.9151948
#> mean of y = 0.5985106
#>
#> Data: x = TcCB[Area == "Cleanup"]
#> y = TcCB[Area == "Reference"]
#>
#> Sample Sizes: nx = 77
#> ny = 47
#>
#> Test Statistic: mean.x - mean.y = 3.316684
#>
#> P-value: 0.0212
#>
#Results of Hypothesis Test
#--------------------------
#
#Null Hypothesis: mu.x-mu.y = 0
#
#Alternative Hypothesis: True mu.x-mu.y is greater than 0
#
#Test Name: Two-Sample Permutation Test
# Based on Differences in Means
# (Based on Sampling
# Permutation Distribution
# 5000 Times)
#
#Estimated Parameter(s): mean of x = 3.9151948
# mean of y = 0.5985106
#
#Data: x = TcCB[Area == "Cleanup"]
# y = TcCB[Area == "Reference"]
#
#Sample Sizes: nx = 77
# ny = 47
#
#Test Statistic: mean.x - mean.y = 3.316684
#
#P-value: 0.0206
dev.new()
plot(mean.list)
#----------
# Now test for a difference in the medians.
#------------------------------------------
median.list <- with(EPA.94b.tccb.df,
twoSamplePermutationTestLocation(
TcCB[Area=="Cleanup"], TcCB[Area=="Reference"],
fcn = "median", alternative = "greater", seed = 47))
median.list
#>
#> Results of Hypothesis Test
#> --------------------------
#>
#> Null Hypothesis: mu.x-mu.y = 0
#>
#> Alternative Hypothesis: True mu.x-mu.y is greater than 0
#>
#> Test Name: Two-Sample Permutation Test
#> Based on Differences in Medians
#> (Based on Sampling
#> Permutation Distribution
#> 5000 Times)
#>
#> Estimated Parameter(s): median of x = 0.43
#> median of y = 0.54
#>
#> Data: x = TcCB[Area == "Cleanup"]
#> y = TcCB[Area == "Reference"]
#>
#> Sample Sizes: nx = 77
#> ny = 47
#>
#> Test Statistic: median.x - median.y = -0.11
#>
#> P-value: 0.9366
#>
#Results of Hypothesis Test
#--------------------------
#
#Null Hypothesis: mu.x-mu.y = 0
#
#Alternative Hypothesis: True mu.x-mu.y is greater than 0
#
#Test Name: Two-Sample Permutation Test
# Based on Differences in Medians
# (Based on Sampling
# Permutation Distribution
# 5000 Times)
#
#Estimated Parameter(s): median of x = 0.43
# median of y = 0.54
#
#Data: x = TcCB[Area == "Cleanup"]
# y = TcCB[Area == "Reference"]
#
#Sample Sizes: nx = 77
# ny = 47
#
#Test Statistic: median.x - median.y = -0.11
#
#P-value: 0.936
dev.new()
plot(median.list)
#==========
# Clean up
#---------
rm(test.list, mean.list, median.list)
graphics.off()