# Nonparametric location tests for one and two samples

## Install required packages

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

## One-sample

### Sign-test

Two-sided test

set.seed(123)
medH0 <- 30
DV    <- sample(0:100, 20, replace=TRUE)

library(DescTools)
SignTest(DV, mu=medH0)

One-sample Sign-Test

data:  DV
S = 15, number of differences = 19, p-value = 0.01921
alternative hypothesis: true median is not equal to 30
95.9 percent confidence interval:
41 90
sample estimates:
median of the differences
61 

### Wilcoxon signed rank test

IQ    <- c(99, 131, 118, 112, 128, 136, 120, 107, 134, 122)
medH0 <- 110
wilcox.test(IQ, alternative="greater", mu=medH0, conf.int=TRUE)

Wilcoxon signed rank test

data:  IQ
V = 48, p-value = 0.01855
alternative hypothesis: true location is greater than 110
95 percent confidence interval:
113.5   Inf
sample estimates:
(pseudo)median
121 

## Two independent samples

### Sign-test

Nj  <- c(20, 30)
DVa <- rnorm(Nj[1], mean= 95, sd=15)
DVb <- rnorm(Nj[2], mean=100, sd=15)
wIndDf <- data.frame(DV=c(DVa, DVb),
IV=factor(rep(1:2, Nj), labels=LETTERS[1:2]))

Looks at the number of cases in each group which are below or above the median of the combined data.

library(coin)
median_test(DV ~ IV, distribution="exact", data=wIndDf)

Exact Two-Sample Brown-Mood Median Test

data:  DV by IV (A, B)
Z = -1.7146, p-value = 0.1482
alternative hypothesis: true mu is not equal to 0

### Wilcoxon rank-sum test ($$=$$ Mann-Whitney $$U$$-test)

wilcox.test(DV ~ IV, alternative="less", conf.int=TRUE, data=wIndDf)

Wilcoxon rank sum test

data:  DV by IV
W = 193, p-value = 0.01703
alternative hypothesis: true location shift is less than 0
95 percent confidence interval:
-Inf -2.402814
sample estimates:
difference in location
-9.133036 
library(coin)
wilcox_test(DV ~ IV, alternative="less", conf.int=TRUE,
distribution="exact", data=wIndDf)

Exact Wilcoxon-Mann-Whitney Test

data:  DV by IV (A, B)
Z = -2.1189, p-value = 0.01703
alternative hypothesis: true mu is less than 0
95 percent confidence interval:
-Inf -2.402814
sample estimates:
difference in location
-9.133036 

### van der Waerden normal scores test

library(coin)
normal_test(DV ~ IV, distribution=approximate(nresample=9999),
data=wIndDf)

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

data:  DV by IV (A, B)
Z = -2.2034, p-value = 0.0291
alternative hypothesis: true mu is not equal to 0

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

library(coin)
oneway_test(DV ~ IV, distribution=approximate(nresample=9999),
data=wIndDf)

Approximative Two-Sample Fisher-Pitman Permutation Test

data:  DV by IV (A, B)
Z = -2.2796, p-value = 0.0195
alternative hypothesis: true mu is not equal to 0

## Two dependent samples

### Sign-test

N      <- 20
DVpre  <- rnorm(N, mean= 95, sd=15)
DVpost <- rnorm(N, mean=100, sd=15)
wDepDf <- data.frame(id=factor(rep(1:N, times=2)),
DV=c(DVpre, DVpost),
IV=factor(rep(0:1, each=N), labels=c("pre", "post")))

Two-sided test

medH0  <- 0
DVdiff <- aggregate(DV ~ id, FUN=diff, data=wDepDf)

library(DescTools)
SignTest(DVdiff$DV, mu=medH0)  One-sample Sign-Test data: DVdiff$DV
S = 11, number of differences = 20, p-value = 0.8238
alternative hypothesis: true median is not equal to 0
95.9 percent confidence interval:
-9.600381 18.945197
sample estimates:
median of the differences
3.309316 

### Wilcoxon signed rank test

wilcox.test(DV ~ IV, alternative="less", paired=TRUE, data=wDepDf)

Wilcoxon signed rank test

data:  DV by IV
V = 79, p-value = 0.1744
alternative hypothesis: true location shift is less than 0

Using package coin

library(coin)
wilcoxsign_test(DV ~ IV | id, alternative="less",
distribution="exact", data=wDepDf)

Exact Wilcoxon-Pratt Signed-Rank Test

data:  y by x (pos, neg)
stratified by block
Z = -0.97065, p-value = 0.1744
alternative hypothesis: true mu is less than 0

Based on data in wide format

# requires R version >= 4.0.0
wilcox.test(Pair(DVpre, DVpost) ~ 1, alternative="less")

### van der Waerden normal scores test

library(coin)
normal_test(DV ~ IV | id, alternative="less",
distribution=approximate(nresample=9999), data=wDepDf)

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

data:  DV by IV (pre, post)
stratified by id
Z = -0.79697, p-value = 0.2194
alternative hypothesis: true mu is less than 0

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

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

Approximative Two-Sample Fisher-Pitman Permutation Test

data:  DV by IV (pre, post)
stratified by id
Z = -1.0843, p-value = 0.2775
alternative hypothesis: true mu is not equal to 0

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

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