# Nonparametric location tests for more than two samples

## Install required packages

wants <- c("coin", "DescTools")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

## Independent samples - unordered groups

### Kruskal-Wallis-test

#### Using kruskal.test()

IQ1  <- c( 99, 131, 118, 112, 128, 136, 120, 107, 134, 122)
IQ2  <- c(134, 103, 127, 121, 139, 114, 121, 132)
IQ3  <- c(110, 123, 100, 131, 108, 114, 101, 128, 110)
IQ4  <- c(117, 125, 140, 109, 128, 137, 110, 138, 127, 141, 119, 148)
Nj   <- c(length(IQ1), length(IQ2), length(IQ3), length(IQ4))
KWdf <- data.frame(DV=c(IQ1, IQ2, IQ3, IQ4),
IV=factor(rep(1:4, Nj), labels=c("I", "II", "III", "IV")))
kruskal.test(DV ~ IV, data=KWdf)

Kruskal-Wallis rank sum test

data:  DV by IV
Kruskal-Wallis chi-squared = 6.0595, df = 3, p-value = 0.1087

#### Using kruskal_test() from package coin

library(coin)
kruskal_test(DV ~ IV, distribution=approximate(nresample=9999),
data=KWdf)

Approximative Kruskal-Wallis Test

data:  DV by IV (I, II, III, IV)
chi-squared = 6.0595, p-value = 0.1043

#### Pairwise group-comparisons

Dunnâ€™s Test

library(DescTools)
DunnTest(KWdf$DV, KWdf$IV, p.adjust.method="holm")

Dunn's test of multiple comparisons using rank sums : holm

mean.rank.diff   pval
II-I         3.012500 1.0000
III-I       -5.800000 0.8038
IV-I         6.241667 0.8031
III-II      -8.812500 0.5573
IV-II        3.229167 1.0000
IV-III      12.041667 0.0993 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Conover Test

library(DescTools)
ConoverTest(KWdf$DV, KWdf$IV, p.adjust.method="holm")

Conover's test of multiple comparisons : holm

mean.rank.diff   pval
II-I         3.012500 1.0000
III-I       -5.800000 0.7621
IV-I         6.241667 0.7565
III-II      -8.812500 0.5230
IV-II        3.229167 1.0000
IV-III      12.041667 0.1014
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

### van der Waerden normal scores test

normal_test(DV ~ IV, distribution=approximate(nresample=9999),
data=KWdf)

Approximative K-Sample van der Waerden (Normal Quantile) Test

data:  DV by IV (I, II, III, IV)
chi-squared = 6.8314, p-value = 0.07061

### Fisher-Pitman permutation test with untransformed response values

oneway_test(DV ~ IV, distribution=approximate(nresample=9999),
data=KWdf)

Approximative K-Sample Fisher-Pitman Permutation Test

data:  DV by IV (I, II, III, IV)
chi-squared = 6.8133, p-value = 0.07611

## Independent samples - ordered groups

### Jonckheere Terpstra trend test

set.seed(123)
P    <- 4
Nj   <- c(41, 37, 42, 40)
muJ  <- rep(c(-1, 0, 1, 2), Nj)
JTdf <- data.frame(IV=ordered(rep(LETTERS[1:P], Nj)),
DV=rnorm(sum(Nj), muJ, 7))

Using JonckheereTerpstraTest() from package DescTools.

library(DescTools)
JonckheereTerpstraTest(DV ~ IV, data=JTdf)

Jonckheere-Terpstra test

data:  DV by IV
JT = 5256, p-value = 0.1609
alternative hypothesis: two.sided

### Linear by linear association test

library(coin)
kruskal_test(DV ~ IV, distribution=approximate(nresample=9999), data=JTdf)

Approximative Linear-by-Linear Association Test

data:  DV by IV (A < B < C < D)
Z = 1.3797, p-value = 0.1701
alternative hypothesis: two.sided

## Dependent samples - unordered groups

### Friedman-test

#### Using friedman.test()

N   <- 5
P   <- 4
DV1 <- c(14, 13, 12, 11, 10)
DV2 <- c(11, 12, 13, 14, 15)
DV3 <- c(16, 15, 14, 13, 12)
DV4 <- c(13, 12, 11, 10,  9)
Fdf <- data.frame(id=factor(rep(1:N, times=P)),
DV=c(DV1, DV2, DV3, DV4),
IV=factor(rep(1:P, each=N),
labels=LETTERS[1:P]))
friedman.test(DV ~ IV | id, data=Fdf)

Friedman rank sum test

data:  DV and IV and id
Friedman chi-squared = 8.2653, df = 3, p-value = 0.04084

#### Using friedman_test() from package coin

library(coin)
friedman_test(DV ~ IV | id, distribution=approximate(nresample=9999),
data=Fdf)

Approximative Friedman Test

data:  DV by IV (A, B, C, D)
stratified by id
chi-squared = 8.2653, p-value = 0.0296

### van der Waerden normal scores test

library(coin)
normal_test(DV ~ IV | id,
distribution=approximate(nresample=9999), data=Fdf)

Approximative K-Sample van der Waerden (Normal Quantile) Test

data:  DV by IV (A, B, C, D)
stratified by id
chi-squared = 6.8183, p-value = 0.06011

### Fisher-Pitman permutation test with untransformed response values

library(coin)
oneway_test(DV ~ IV | id, distribution=approximate(nresample=9999),
data=Fdf)

Approximative K-Sample Fisher-Pitman Permutation Test

data:  DV by IV (A, B, C, D)
stratified by id
chi-squared = 6.8182, p-value = 0.06681

## Dependent samples - ordered groups

### Page trend test for ordered alternatives

N   <- 10
P   <- 4
muJ <- rep(c(-1, 0, 1, 2), each=N)
Pdf <- data.frame(id=factor(rep(1:N, times=P)),
DV=rnorm(N*P, muJ, 3),
IV=ordered(rep(LETTERS[1:P], each=N)))

Using PageTest() from package DescTools.

library(DescTools)
PageTest(DV ~ IV | id, data=Pdf)

Page test for ordered alternatives

data:  DV and IV and id
L = 276, p-value = 0.002047

Using friedman_test() from package coin.

library(coin)
friedman_test(DV ~ IV | id, distribution=approximate(nresample=9999), data=Pdf)

Approximative Page Test

data:  DV by IV (A < B < C < D)
stratified by id
Z = 2.8482, p-value = 0.0046
alternative hypothesis: two.sided

## Detach (automatically) loaded packages (if possible)

try(detach(package:DescTools))
try(detach(package:coin))
try(detach(package:survival))