How to apply dunn.test for dataframes in R?

3.1k views Asked by At

I have a CSV file with two columns: group and weight. I'm able to do a Kruskal–Wallis test:

kruskal.test(Weight~Group, data=Data.df)

I want to calculate pairwise multiple comparisons with the control group (there are different treatments). I have tried dunn.test.control, but I only get errors. I don't think I'm using the command correctly.

Any idea what I'm doing wrong? Feel free to show an example!

1

There are 1 answers

0
dcarlson On BEST ANSWER

You will get a better response if you include some data (or illustrate your issue using data already available in R (see data()), show the code that was not working and the error message(s), and indicate what package the function giving you trouble comes from since dunn.test.control is not included with R but in the PMCMR package. For example, we can use the iris data set included in R (?iris for info):

kruskal.test(Sepal.Width~Species, iris)
#
#   Kruskal-Wallis rank sum test
#
# data:  Sepal.Width by Species
# Kruskal-Wallis chi-squared = 63.571, df = 2, p-value = 1.569e-14

Shows that there is a significant difference. Now the Dunn test:

library(PMCMR)
dunn.test.control(iris$Sepal.Width, iris$Species)
#
#   Pairwise comparisons using Dunn's-test for multiple 
#                         comparisons with one control
#
# data: iris$Sepal.Width and iris$Species
#
#           setosa 
# versicolor 1.4e-14
# virginica  7.7e-08

No error message so you probably did not specify the test correctly. But this is not a pairwise test. You probably want

posthoc.kruskal.dunn.test(iris$Sepal.Width, iris$Species)
# 
#   Pairwise comparisons using Dunn's-test for multiple 
#                          comparisons of independent samples
# 
# data: iris$Sepal.Width and iris$Species
# 
#            setosa  versicolor
# versicolor 2.0e-14 -         
# virginica  1.5e-07 0.016     
# 
# P value adjustment method: holm
# Warning message:
# In posthoc.kruskal.dunn.test.default(iris$Sepal.Width, iris$Species) :
#   Ties are present. z-quantiles were corrected for ties.

Notice the warning message. The test runs, but exact probabilities cannot be computed because there are tied ranks. Two of the comparisons are so small that ties are not a factor unless your sample size is small. I prefer the version of the test in DescTools since it also shows the mean rank difference between each pair:

library(DescTools)
DunnTest(Sepal.Width~Species, iris)
# 
#  Dunn's test of multiple comparisons using rank sums : holm  
# 
#                      mean.rank.diff    pval    
# versicolor-setosa            -67.38 2.0e-14 ***
# virginica-setosa             -46.50 1.5e-07 ***
# virginica-versicolor          20.88  0.0158 *  
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1