dunn_test: Dunn's Test of Multiple Comparisons

View source: R/dunn_test.R

dunn_testR Documentation

Dunn's Test of Multiple Comparisons

Description

Performs Dunn's test for pairwise multiple comparisons of the ranked data. The mean rank of the different groups is compared. Used for post-hoc test following Kruskal-Wallis test.

The default of the rstatix::dunn_test() function is to perform a two-sided Dunn test like the well known commercial softwares, such as SPSS and GraphPad. This is not the case for some other R packages (dunn.test and jamovi), where the default is to perform one-sided test. This discrepancy is documented at https://github.com/kassambara/rstatix/issues/50.

If a reference group is specified (via ref.group), then each of the remaining group levels is compared only to the reference (control) group, and the p-value adjustment for multiple comparisons is computed over only these k - 1 comparisons (instead of all k(k - 1)/2 pairwise comparisons). Note that this affects the adjusted p-values: it is not equivalent to filtering the full pairwise result afterwards, which would still adjust over all pairs.

See the Datanovia tutorial Kruskal-Wallis Test in R for a worked walkthrough.

Usage

dunn_test(
  data,
  formula,
  p.adjust.method = "holm",
  ref.group = NULL,
  detailed = FALSE,
  effect.size = FALSE
)

Arguments

data

a data.frame containing the variables in the formula.

formula

a formula of the form x ~ group where x is a numeric variable giving the data values and group is a factor with one or multiple levels giving the corresponding groups. For example, formula = TP53 ~ cancer_group.

p.adjust.method

method to adjust p values for multiple comparisons. Used when pairwise comparisons are performed. Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none".

ref.group

a character string specifying the reference group. If specified, for a given grouping variable, each of the group levels will be compared to the reference (control) group, and the p-value adjustment is computed over only these comparisons. Note that, unlike t_test() and wilcox_test(), dunn_test() does not support ref.group = "all".

detailed

logical value. Default is FALSE. If TRUE, a detailed result is shown.

effect.size

logical. Default is FALSE. If TRUE, an r column is added, the effect size r = Z / sqrt(N) where Z is Dunn's z-statistic and N is the total sample size (the whole-sample rank variance Dunn's z is standardised on, not the pairwise n1 + n2). No magnitude label is attached: there is no threshold set calibrated for it.

Details

DunnTest performs the post hoc pairwise multiple comparisons procedure appropriate to follow up a Kruskal-Wallis test, which is a non-parametric analog of the one-way ANOVA. The Wilcoxon rank sum test, itself a non-parametric analog of the unpaired t-test, is possibly intuitive, but inappropriate as a post hoc pairwise test, because (1) it fails to retain the dependent ranking that produced the Kruskal-Wallis test statistic, and (2) it does not incorporate the pooled variance estimate implied by the null hypothesis of the Kruskal-Wallis test.

Value

return a data frame with some of the following columns:

  • .y.: the y (outcome) variable used in the test.

  • group1,group2: the compared groups in the pairwise tests.

  • n1,n2: Sample counts.

  • estimate: mean ranks difference.

  • estimate1, estimate2: show the mean rank values of the two groups, respectively.

  • statistic: Test statistic (z-value) used to compute the p-value.

  • p: p-value.

  • p.adj: the adjusted p-value.

  • method: the statistical test used to compare groups.

  • p.signif, p.adj.signif: the significance level of p-values and adjusted p-values, respectively.

The returned object has an attribute called args, which is a list holding the test arguments.

References

Dunn, O. J. (1964) Multiple comparisons using rank sums Technometrics, 6(3):241-252.

See Also

The Datanovia tutorial: Kruskal-Wallis Test in R.

Examples

# Simple test
ToothGrowth %>% dunn_test(len ~ dose)

# Comparison against a reference (control) group
# each group is compared to the reference; the p-value
# adjustment corrects for only these k - 1 comparisons
ToothGrowth %>% dunn_test(len ~ dose, ref.group = "0.5")

# Grouped data
ToothGrowth %>%
  group_by(supp) %>%
  dunn_test(len ~ dose)

rstatix documentation built on July 24, 2026, 1:06 a.m.