While it's easy to compute the p-value of Krsukal-Wallis non-parametric test using the kruskal.test() function, I don't know how to compute the mean ranks and standard errors for each group.

For instance suppose the following example:

x <- data.frame(group=rep(1:2,each=10),height=runif(20))
kruskal.test(x$height, g=x$group)
#   Kruskal-Wallis rank sum test
# data:  x$height and x$group
# Kruskal-Wallis chi-squared = 0.0057, df = 1,
# p-value = 0.9397

It returns only p-value, but not the mean ranks and standard errors for each group.

I have also tried the verboseBarplot() of package "WGCNA", it generates a bar plot with standard errors, but I think the height of bars are not mean of the "ranks", but rather seem to be normal mean values.


I have done the analysis as follows, that computes mean ranks and standard errors for each column of x, based on "group" column which is first. Is it correct?

se <- function(x) sd(x)/sqrt(length(x))
colSe <- function(x) apply(x, 2, se)
y <- sapply(2:ncol(x),function(i) rank(x[,i])) 
y <- cbind(x$group,y)
y <- data.frame(y)
m <- ddply(y, .(group),colMeans)
m.se <- ddply(y, .(group), colSe)
m.se$group <- m$group

When you want to compare just two groups, you'll have to use the Wilcoxon Rank Sum test / Mann-Whitney test (for independent groups) or the Wilcoxon Signed Rank test (for dependent groups). The Kruskall-Wallis test is used when you want to compare more than two (independent) groups.

Creating some sample data with two grouping variables; one with two levels (sex) and one with four levels (group):

x <- data.frame(group=rep(1:4,each=10),sex=rep(1:2,each=20),height=runif(40))

Loading required packages:

require(pastecs)  # for descriptive statistics
require(pgirmess) # for post hoc tests

When you want to compare the male participants with the female partipants, you use the Wilcoxon Signed Rank test (which is more or less the same as the Mann-Whitney test):

wilcox.test(height ~ sex, data = x, paired = FALSE)
by(x$height, x$sex, stat.desc, basic = FALSE)

A Kruskall-Wallis test for group (which has four levels):

kruskal.test(height ~ group, data = x)

Comparing the ranks for the groups:

# creating a rank variable
x$heightRank <- rank(x$height)

# getting the descriptive statistics for the groups
by(x$heightRank, x$group, stat.desc, basic = FALSE)

# post-hoc test for identifying statistical significant differences between the groups
kruskalmc(height ~ group, data = x)
  • $\begingroup$ Great answer, thanks. I understand your answer, but paired=FALSE does not make sense to me in the first wilcox.test(). Is it required/true? $\endgroup$ May 2 '14 at 19:31
  • $\begingroup$ You have to use paired=FALSE when you compare independent groups. Use paired=TRUE when you want to compare dependent groups (e.g. repeated measures). $\endgroup$
    – Jaap
    May 4 '14 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.