First, an invaluable function that R provides, related to non-parametric tests is
rank(), which can be used to find the sum of the ranks for sample data, even accommodating ties! Consider the following example:
Suppose a test for independence of judgment was administered to three different groups of students. The higher the score, the more independent the person is in judgment (i.e., the person is not easily influenced b another person's opinions). Group A includes premedical students ($n=11$), Group B includes English majors ($n=10$) and Group C includes psychology majors ($n=11$).
We will do a Kruskal-Wallis test (the long way)...
First, we store the sample data into vectors in the normal way:
> sample.A = c(69,78,90,92,68,77,85,96,87,71,93) > sample.B = c(44,36,41,90,51,42,38,52,87,46) > sample.C = c(36,97,84,72,75,64,62,79,63,66,58)
Having the individual sample sizes stored into some short-named variables will help later, so...
> n.A = length(sample.A) > n.B = length(sample.B) > n.C = length(sample.C)
Then we rank the data, and section it into three groups of ranks -- one for each sample:
all.ranks = rank(c(sample.A,sample.B,sample.C),ties.method = "average") samples.factor = factor(rep(c("A","B","C"),times=c(n.A,n.B,n.C))) ranks.A = all.ranks[samples.factor == "A"] ranks.B = all.ranks[samples.factor == "B"] ranks.C = all.ranks[samples.factor == "C"]
Finally, we find the sum of sample sizes $N$, and calculate the test statistic:
> N = n.A+n.B+n.C > H = (12/(N*(N+1)))*sum(c(sum(ranks.A)^2/n.A,sum(ranks.B)^2/n.B,sum(ranks.C)^2/n.C))-3*(N+1) > H  11.10302
Recalling these test statistics follow a chi-squared distribution, we find the $p$-value in the following way:
> p.value = 1 - pchisq(H,2) > p.value  0.003881588
Of course, with R there is always a faster way...
> kruskal.test(list(sample.A,sample.B,sample.C)) Kruskal-Wallis rank sum test data: list(sample.A, sample.B, sample.C) Kruskal-Wallis chi-squared = 11.109, df = 2, p-value = 0.00387
Seeing a $p$-value much less than $\alpha = 0.5$, we reject the null hypothesis. There is significant evidence that there is a difference between the populations.
To find which pair are significantly different from one another, we follow with a Wilcoxon Test. We could do this the long way by ranking the samples (similar to what we did above, just with a different test statistic and distribution), or we could be lazy and just do:
> wilcox.test(sample.A,sample.B,alternative="two.sided")$p.value  0.004320274 > wilcox.test(sample.A,sample.C,alternative="two.sided")$p.value  0.02806507 > wilcox.test(sample.B,sample.C,alternative="two.sided")$p.value  0.05718419
It appears we have significant evidence of a difference between A and B, and between A and C, but no evidence of a difference between B and C. Thus, A is significantly different from B and C.