hcistats:kruskalwallis

Kruskal-Wallis is basically a non-parametric version of ANOVA. Thus, if you have the data which contain more than two groups to compare, and your data are ordinal or your data cannot assume the normality, Kruskal-Wallis is the way to go. Fortunately, in R, the way to run a Kruskal-Wallis test is similar to the way to run an ANOVA test.

There is also a non-parametric version of repeated-measure ANOVA, which is called Friedman test. **If you are comparing the data against a between-subject factor, you can use a Kruskal-Wallis test. Otherwise, you need to use a Friedman test**.

As you can see in the example below, Kruskal-Wallis and Friedman tests only support a one-way analysis. This means that you can compare the data only across one factor and unfortunately, you cannot easily extend these tests to two-way or mixed-design as we can do with ANOVA. This is one of the costs you have to pay when you have to use a non-parametric test. I will post non-parametric methods equivalent to two-way repeated-measure or mixed-design ANOVA once I figure them out.

Unfortunately, there is no straight way to calculate the effect size of Kruskal Wallis test or Friedman test. Instead, you can calculate the effect size in the post-hoc test with Mann-Whitney or Wilcoxon test. The effect size of those tests are calculated as follows:

,

where *N* is the total number of the samples. Here is the standard value of *r* for small, medium, and large sizes.

small size | medium size | large size | |
---|---|---|---|

abs(r) | 0.1 | 0.3 | 0.5 |

Kruskal-Wallis and Friedman tests give you a chi-squared. However, its degree of freedom is more than 1, and thus it is not straightforward to convert the chi-squared into the effect size. Thus, we calculate the effect size for the post-hoc comparison.

First, you need to prepare the data.

Then, run a Kruskal-Wallis test. The format is pretty much the same as ANOVA.

Now you get the result.

So, we have a significant effect of Group.

Let's use the same data as in the example of a Kruskal-Wallis test. But we need to format the data so that Group represents a within-subject factor.

The rows represent the values from each participant. Now, you run a Friedman“s test.

You have a significant effect of Group.

Similar to ANOVA, you need to do a post-hoc test after Kruskal-Wallis and Friedman if you find a significant effect. As we do multiple t tests with Bonferroni or Holm correction, we can do Mann-Whitney or Wilcoxon tests with the same corrections. If you have unpaired (factorial, or non-repeated-measure) data, you can do pairwise comparison with a Mann-Whitney test. First, take a look at how to do pairwise comparison with a Mann-Whitney test with Bonferroni correction.

If you have any tied value in your data, you can only calculate an approximate p value. (You can try to calculate the exact p value by changing the option to “exact=T”, but will get warnings if you have ties.) Unfortunately, it seems there is no way to get this around, but in many cases, this approximate p value is close enough to the exact p value, and will not cause a lot of troubles. But if you really want to be very precise, you can calculate the exact p value by using functions in coin package, and manually apply Bonferroni corrections to the p values you have gained by using p.adjust() function. For doing this, see the Mann-Whitney test or the Wilcoxon test.

Instead of Bonferroni correction, you can also use Holm correction.

If you have a within-factor (or repeated-measure data), you need to specify *paired* so that you will use a Wilcoxon test.

For calculating the effect size, you have to do a Mann-Whitney or Wilcoxon test to find the Z value (which is necessary for the calculation of the effect size). In this example, we are going to use a Mann-Whitney test. If you need to use a Wilcoxon test (*i.e.*, you have a within-subject factor), see this page.

Thus, the effect size is:

Remember that you have to use the total sample size (10 * 2 = 20) to calculate the effect size *r*.

You can report the results of Kruskal Wallis or Friedman test as follows: **A Kruskal Wallis test revealed a significant effect of Group on Value (****(2)=13.7, p < 0.01). A post-hoc test using Mann-Whitney tests with Bonferroni correction showed the significant differences between Group A and B (p < 0.05, r = 0.56) and between Group A and C (p < 0.01, r = 0.70)**.

hcistats/kruskalwallis.txt · Last modified: 2014/08/14 05:23 by Koji Yatani

## Comments

And to demonstrate the R code as well, well that is a lifesaver!

Thanks again!

I'm a beginner user of R so maybe you can help me.

I'm executing some statistical tests (Wilcoxon, Friedman...) and I need to get de p-value as a single value to work with it.

How can i do it ?

You can answer me at kakius82@gmail.com.

Thank you very much.

I wonder how can you get the r value in the comparison of group A and group B.

I did Kruskal-Wallis test on my data, and it showed significance; but in further pairwise.wilcox.test, no significance was found between groups. Do you have any idea what the problems might be. Thank you very much.