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?
library(plyr) 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) colnames(y)<-colnames(x) 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)
- Solved – Statistical significance test: One way Anova and Kruskal-Wallis test
- Solved – Mean and variance of ranks
- Solved – Comparing unbalanced groups with ANOVA/Kruskal-Wallis when one group has only 1 observation
- Solved – How to plot the results of a Kruskal-Wallis or a Welch’s ANOVA
- Solved – Nonparametric test for trend using Python