This function performs Wilcoxon rank sum tests for one sample
or for two paired (dependent) samples. For unpaired (independent)
samples, please use the mann_whitney_test()
function.
A Wilcoxon rank sum test is a non-parametric test for the null hypothesis
that two samples have identical continuous distributions. The implementation
in wilcoxon_test()
is only used for paired, i.e. dependent samples. For
independent (unpaired) samples, use mann_whitney_test()
.
wilcoxon_test()
can be used for ordinal scales or when the continuous
variables are not normally distributed. For large samples, or approximately
normally distributed variables, the t_test()
function can be used (with
paired = TRUE
).
wilcoxon_test(
data,
select = NULL,
by = NULL,
weights = NULL,
mu = 0,
alternative = "two.sided",
...
)
A data frame.
Name(s) of the continuous variable(s) (as character vector)
to be used as samples for the test. select
can be one of the following:
select
can be used in combination with by
, in which case select
is
the name of the continous variable (and by
indicates a grouping factor).
select
can also be a character vector of length two or more (more than
two names only apply to kruskal_wallis_test()
), in which case the two
continuous variables are treated as samples to be compared. by
must be
NULL
in this case.
If select
select is of length two and paired = TRUE
, the two samples
are considered as dependent and a paired test is carried out.
If select
specifies one variable and by = NULL
, a one-sample test
is carried out (only applicable for t_test()
and wilcoxon_test()
)
For chi_squared_test()
, if select
specifies one variable and
both by
and probabilities
are NULL
, a one-sample test against given
probabilities is automatically conducted, with equal probabilities for
each level of select
.
Name of the variable indicating the groups. Required if select
specifies only one variable that contains all samples to be compared in the
test. If by
is not a factor, it will be coerced to a factor. For
chi_squared_test()
, if probabilities
is provided, by
must be NULL
.
Name of an (optional) weighting variable to be used for the test.
The hypothesized difference in means (for t_test()
) or location
shift (for wilcoxon_test()
and mann_whitney_test()
). The default is 0.
A character string specifying the alternative hypothesis,
must be one of "two.sided"
(default), "greater"
or "less"
. See ?t.test
and ?wilcox.test
.
Additional arguments passed to wilcox.test()
(for unweighted
tests, i.e. when weights = NULL
).
A data frame with test results. The function returns p and Z-values as well as effect size r and group-rank-means.
The following table provides an overview of which test to use for different types of data. The choice of test depends on the scale of the outcome variable and the number of samples to compare.
Samples | Scale of Outcome | Significance Test |
1 | binary / nominal | chi_squared_test() |
1 | continuous, not normal | wilcoxon_test() |
1 | continuous, normal | t_test() |
2, independent | binary / nominal | chi_squared_test() |
2, independent | continuous, not normal | mann_whitney_test() |
2, independent | continuous, normal | t_test() |
2, dependent | binary (only 2x2) | chi_squared_test(paired=TRUE) |
2, dependent | continuous, not normal | wilcoxon_test() |
2, dependent | continuous, normal | t_test(paired=TRUE) |
>2, independent | continuous, not normal | kruskal_wallis_test() |
>2, independent | continuous, normal | datawizard::means_by_group() |
>2, dependent | continuous, not normal | not yet implemented (1) |
>2, dependent | continuous, normal | not yet implemented (2) |
(1) More than two dependent samples are considered as repeated measurements.
For ordinal or not-normally distributed outcomes, these samples are
usually tested using a friedman.test()
, which requires the samples
in one variable, the groups to compare in another variable, and a third
variable indicating the repeated measurements (subject IDs).
(2) More than two dependent samples are considered as repeated measurements. For normally distributed outcomes, these samples are usually tested using a ANOVA for repeated measurements. A more sophisticated approach would be using a linear mixed model.
Bender, R., Lange, S., Ziegler, A. Wichtige Signifikanztests. Dtsch Med Wochenschr 2007; 132: e24–e25
du Prel, J.B., Röhrig, B., Hommel, G., Blettner, M. Auswahl statistischer Testverfahren. Dtsch Arztebl Int 2010; 107(19): 343–8
t_test()
for parametric t-tests of dependent and independent samples.
mann_whitney_test()
for non-parametric tests of unpaired (independent)
samples.
wilcoxon_test()
for Wilcoxon rank sum tests for non-parametric tests
of paired (dependent) samples.
kruskal_wallis_test()
for non-parametric tests with more than two
independent samples.
chi_squared_test()
for chi-squared tests (two categorical variables,
dependent and independent).
data(mtcars)
# one-sample test
wilcoxon_test(mtcars, "mpg")
#> # One Sample Wilcoxon signed rank test
#>
#> Alternative hypothesis: true location shift is not equal to 0
#>
#> V = 528, p < .001
#>
# base R equivalent, we set exact = FALSE to avoid a warning
wilcox.test(mtcars$mpg ~ 1, exact = FALSE)
#>
#> Wilcoxon signed rank test with continuity correction
#>
#> data: mtcars$mpg
#> V = 528, p-value = 8.311e-07
#> alternative hypothesis: true location is not equal to 0
#>
# paired test
wilcoxon_test(mtcars, c("mpg", "hp"))
#> # Paired Wilcoxon signed rank test
#>
#> Alternative hypothesis: true location shift is not equal to 0
#>
#> V = 0, r = 0.87, Z = -4.94, p < .001
#>
# base R equivalent, we set exact = FALSE to avoid a warning
wilcox.test(mtcars$mpg, mtcars$hp, paired = TRUE, exact = FALSE)
#>
#> Wilcoxon signed rank test with continuity correction
#>
#> data: mtcars$mpg and mtcars$hp
#> V = 0, p-value = 8.338e-07
#> alternative hypothesis: true location shift is not equal to 0
#>
# when `by` is specified, each group must be of same length
data(iris)
d <- iris[iris$Species != "setosa", ]
wilcoxon_test(d, "Sepal.Width", by = "Species")
#> # Paired Wilcoxon signed rank test
#>
#> Alternative hypothesis: true location shift is not equal to 0
#>
#> V = 247, r = 0.39, Z = -2.76, p = 0.006
#>