Compact letter display for kruskal- wallis Dunn post-hoc test on R

1k Views Asked by At

This code is working but it uses Fisher LSD test or I need compact letter display for kruskal-wallis Dunn post hoc test. Does anyone have a solution ?

library(agricolae)

kruskal_cld <- kruskal(MR$hauteur, MR$distance, group=TRUE, p.adj="bonferroni")$groups
kruskal_cld
cld <- as.data.frame(kruskal_cld) 

cld$ordre <- c(6,3,1,2,4,5)
cld <- cld[order(cld$ordre), ]

ggplot(data=MR, aes(x=distance, y=hauteur,col=distance))+
  scale_color_brewer(palette = "Dark2")+
  scale_alpha(0.5)+
  geom_jitter(position = position_jitterdodge(jitter.width = 1), size = 0.5)+
  stat_summary(fun.data=mean_sd, size=0.4, col="black")+
 geom_text(data = cld, aes(label = cld$groups, y = 50, x = ordre), vjust = -0.5, inherit.aes = FALSE)

I tried this code (find it on stackoverflow) but then I realise agricolae package do not use Dunn test but Fisher LSD to display the letters. I need to do it with the Dunn test for my analyze.

1

There are 1 best solutions below

0
SAL On

If it is fine for you to switch to another package capable of performing the dunnTest function, then the FSA package is one solution. Then, using the rcompanion package you can get the compact letter display of your comparisons. As I do not have access to your MR data, I used the mtcars data from R to show how to do it in this way:

# install and load the required packages
library(FSA)
library(rcompanion)

# using mtcars as example data and a new assignment as df
df <- mtcars
# using cyl as factor variable and hp as dependent variable
df$cyl <- factor(df$cyl,levels = c("4","6","8"))

# running kruskal-wallis test if there is any difference between groups

kruskal.test(hp ~ cyl,
             data = df)

    Kruskal-Wallis rank sum test

data:  hp by cyl
Kruskal-Wallis chi-squared = 25.222, df = 2, p-value = 3.336e-06

# running post hoc Dunn test

Phocdunn <- dunnTest(hp ~ cyl,
              data=df,
              method="bonferroni")    # Can adjust p-values;

Phocdunn
 
  Comparison         Z      P.unadj        P.adj
1      4 - 6 -1.823778 6.818569e-02 2.045571e-01
2      4 - 8 -4.989580 6.051057e-07 1.815317e-06
3      6 - 8 -2.437999 1.476883e-02 4.430648e-02

# extract the post hoc from Phocdunn
Phocdunns <- Phocdunn$res

# Compact letter display of comparisons

cld <- cldList(comparison = Phocdunns$Comparison,
        p.value    = Phocdunns$P.adj,
        threshold  = 0.05)[1:2]

names(cld)[1]<-"cyl" # change the name of grouping factor according to the dataset (df)
cld
# here it shows the cyl levels 4 and 6 are not different as they have same letter #but different from cyl 8
    cyl    Letter
1     4      a
2     6      a
3     8      b

# visualization of comparisons using ggplot (you need to adjust the plot arguments based on your dataset)

library(tidyverse)
ggplot(data=df, aes(x=cyl, y=hp,col=cyl))+
  scale_color_brewer(palette = "Dark2")+
  geom_jitter(position = position_jitterdodge(jitter.width = 1), size = 0.5)+
  theme_bw() +
  geom_text(data = cld, aes(label = cld$Letter, y = 300, x = cyl), 
            vjust = -0.5,
            hjust= 0.5,
            fontface = "bold",
            size=3.5,
            check_overlap = F)+
  theme(legend.position = "none")

enter image description here