Here I provide a exact one sample or paired sample test for mean(s), which is the exact version of permutation test to compare a single sample against a mean or to compare paired samples’ differences against a mean.
The algorithm in this function is based on Bryan F. J. Manly. 1997.
Randomization, bootstrap and Monte Carlo methods in biology. 2nd edition. pp. 91-97.
That is, all possible permutations (i.e. all possible cases of exchange between $x_i$ and $\mu$ in one-sample test or all possible cases of exchange between $x_{1i}$ and $x_{2i}$) in paired-sample test are processed to calculate a exact p-value.
R code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The usage of this function exactOneOrPairedSampleTest() is very similar to the R built-in function t.test(). There are four arguments:
x1: a numeric vector specifying $x_{1i}$
x2 = NULL: a numeric vector specifying $x_{2i}$ (in paired-sample case)
mu = 0: a number specifying true mean or true difference $\mu$
alternative = c("t","g","l")a single character specifying $H_0$: true mean of $x_1$ or mean of $x_{1i} - x_{2i}$ is equal, greater or less than $\mu$, respectively.
Example 1: one-sample test
Let $x_i = \{43,67,64,64,51,53,53,26,36,48,34,48,6 \}$, $i=1 \ldots 13$:
> x <- c(43,67,64,64,51,53,53,26,36,48,34,48,6)
Now we can call the above function exactOneOrPairedSampleTest() to test $H_0$: true mean of $x_i = 56$ against $H_A$: true mean of $x_i \neq 56$:
> test1 <- exactOneOrPairedSampleTest(x, alternative="t", mu=56)
> test1
Exact one sample test
Alternative hypothesis: true mean is not equal to 56
mean(x) - mu = -10.3846153846154
Number of total permutation = 8192
Number of rejected permutation = 364
P-value = 0.04443359
Now we can call the function exactOneOrPairedSampleTest() to test $H_0$: true mean of $x_{1i} - x_{2i} \leq 10$ against $H_A$: true mean of $x_{1i} - x_{2i} > 10$:
> test2 <- exactOneOrPairedSampleTest(x1, x2, alternative="g", mu=10)
> # equivalent to test2 <- exactOneOrPairedSampleTest(x1 - x2, alternative="g", mu=10)
> test2
Exact paired sample test
Alternative hypothesis: means of x1 - x2 is greater than 10
mean((x1)-(x2)) - mu = 8.45454545454546
Number of total permutation = 2048
Number of rejected permutation = 445
P-value = 0.2172852