Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get significance codes after Kruskal Wallis post hoc test

Tags:

r

statistics

Is there a way to get significance codes after a pairwise comparisons to a Kruskall wallis test? With significance codes I mean letter codes that are assigned to populations to indicate where differences are significant.

With a traditional anova such a test can be performed using HSD.test from the agricolae library but for non parametric counterparts of anova I have not been able to find anything.

A small toy example:

dv  <-  c(runif(100, 5.0, 10))
iv  <-  as.factor( c(rep("I", 10),  rep("II", 10),  rep("III", 10),  rep("IV", 10), rep("V", 10),
                    rep("VI", 10), rep("VII", 10), rep("VIII", 10), rep("IX", 10), rep("X", 10)))

df  <-  data.frame(dv, iv)

# with anova
library(agricolae)
aov.000  <-  aov(dv ~ iv,  data=df)
HSD.test(aov.000, "iv")

# after KW test: 
(kt  <-  kruskal.test(dv ~ iv,  data=df))

library(coin)
library(multcomp)
NDWD <- oneway_test(dv ~ iv, data = df,
        ytrafo = function(data) trafo(data, numeric_trafo = rank),
        xtrafo = function(data) trafo(data, factor_trafo = function(x)
            model.matrix(~x - 1) %*% t(contrMat(table(x), "Tukey"))),
        teststat = "max", distribution = approximate(B=1000))

### global p-value
print(pvalue(NDWD))

### sites (I = II) != (III = IV) at alpha = 0.01 (page 244)
print(pvalue(NDWD, method = "single-step"))
like image 794
thijs van den bergh Avatar asked Dec 27 '22 02:12

thijs van den bergh


2 Answers

because it might be of use to others, the following seems to work (using the multcompView library):

library(multcompView)
mat  <-  data.frame(print(pvalue(NDWD, method = "single-step")))
(a   <-  c(mat[, 1]));  names(a)  <-  rownames(mat)
multcompLetters(a)

Alternatively the following will work:

test  <-  pairwise.wilcox.test(dv, iv, p.adj="bonferroni", exact=FALSE)
# test  <-  pairwise.wilcox.test(et.ef, s.t, p.adj="holm", exact=FALSE)

library(multcompView)
test$p.value
library(reshape)
(a <- melt(test$p.value))
a.cc  <-  na.omit(a)
a.pvals  <-  a.cc[, 3]
names(a.pvals)  <-  paste(a.cc[, 1], a.cc[, 2], sep="-")
a.pvals
multcompLetters(a.pvals)
like image 145
thijs van den bergh Avatar answered Jan 13 '23 13:01

thijs van den bergh


You can also use the cldList function from the rcompanion package (see https://rcompanion.org/rcompanion/d_06.html). Example:

k_test <- k_test$res

library(rcompanion)

cldList(comparison = k_test$Comparison,
    p.value    = PT$P.adj,
    threshold  = 0.05)


Error: No significant differences.

I used it in combination with the Dunn post-hoc and it worked perfectly.

like image 21
Thomas Avatar answered Jan 13 '23 15:01

Thomas