Description Usage Arguments Details Value Examples
View source: R/wilcoxon_test.R
Performs a Wilcoxon Signed Rank test on a vector of data or two paired vectors of data, or performs a Wilcoxon Rank-Sum test on two unpaired vectors of data.
1 2 |
x |
a numeric vector. |
y |
an optional numeric vector. |
alternative |
a string specifying the alternative hypothesis ("two.sided, "less", or "greater"). Only the first letter is necessary. Defaults to two sided. |
mu |
a number representing a location or location difference to be tested for in the null hypothesis. Defaults to 0. |
paired |
a logical indicating whether to perform a paired or unpaired test. Defaults to false. |
exact |
a logical indicating whether to compute an exact p-value or an approximation. |
correct |
A logical indicating whether to use a continuity correction or not. |
If either y is not supplied or both x and y are supplied and paired is true, then a Wilcoxon signed rank test is performed. The null hypothesis is that the median of x (or x-y in the paired case) is equal to mu.
If both x and y are supplied and paired is false, then a Wilcoxon rank-sum (Mann-Whitney) test is performed. The null hypothesis is that the medians of x and y differ by a location shift of mu. The alternative hypothesis "greater" refers to x being shifted to the right of y.
If exact is true, an exact p-value will be computed using the built-in psignrank() or pwilcox() distribution functions. If exact is false, the p-value is computed using a normal approximation. If exact is not supplied, exact will default to true if the sample size is greater than 50 and there are no ties. For the signed-rank test, there must also be no zeroes. Otherwise, exact will be set to false.
Warning: the built in function pwilcox() uses large amounts of memory and stack, so if exact is true and one of the samples is large (thousands or more), the function can crash R. To prevent this, exact should be set to false instead.
The correct parameter is only relevant if exact is false. If correct is true, a continuity correction will be applied in order to perform the normal approximation. The correction is a shifting of the test statistic by 0.5.
Unlike the built in function wilcox.test(), this function does not support calculating confidence intervals, or taking in a formula as an argument.
A list of class "htest" consisting of the following:
statistic - the value of the test statistic
parameter - for this test, NULL
p.value - the p-value
null.value - the value of mu
alternative - the alternative hypothesis
method - the type of test performed
data.name - the name(s) of the supplied data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | x <- rnorm(50)
y1 <- rnorm(50)
y2 <- rnorm(100)
y3 <- rnorm(5000)
wilcoxon_test(x) # One-sample Wilcoxon signed-rank test
wilcoxon_test(x, alternative = "g", mu = 2) # Alternative hypothesis is that the median of x is greater than 2
wilcoxon_test(x, y1, paired = TRUE) # Paired Wilcoxon signed-rank test
## Not run:
wilcoxon_test(x, y2, paired = TRUE) # Error, cannot perform paired test if vectors have different lengths
## End(Not run)
wilcoxon_test(x, y1, paired = FALSE) # Two-sample Wilcoxon rank-sum test
wilcoxon_test(x, y2, paired = FALSE, exact = TRUE) # Calculates exact p-value from Wilcoxon rank-sum distribution
wilcoxon_test(x, y2, paired = FALSE, exact = FALSE, correct = FALSE) # Uses normal approximation to calculate p-value, without continuity correction
wilcoxon_test(x, y2, paired = FALSE, exact = FALSE, correct = TRUE) # Same as above, but with continuity correction
## Not run:
wilcoxon_test(x, y3, exact = TRUE) # y3 is too large, do not run with exact = T, could crash R
## End(Not run)
wilcoxon_test(x, y3, exact = FALSE) # Ok to run
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.