---
title: "Mann-Whitney-Wilcoxon test with `hyper3` objects"
author: "Robin KS Hankin"
date: "10/04/2022"
output: html_document
---

```{r setup, include=FALSE}
set.seed(1)
knitr::opts_chunk$set(echo = TRUE)
library("hyper2")
```

<p style="text-align: right;">
![](`r system.file("help/figures/hyper2.png", package = "hyper2")`){width=10%}
</p>


We do some work with Mann-Whitney test, using the chorioamnion dataset at `wilcox.test.Rd`:

```{r,label=defineAB}
x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)
```

First do the Mann-Whitney-Wilcox test:

```{r,domwwtest}
wilcox.test(x,y)
```

Now use `hyper3`:


```{r,hyper3AB,cache=TRUE}
names(x) <- rep("x",length(x))
names(y) <- rep("y",length(y))
equalp.test(ordervec2supp3(names(sort(c(x,y)))))
```

#  Some permutation tests under the null:


```{r,dosomepvalsNULL,cache=TRUE}
n <- 100
a <- matrix(0,n,2)
colnames(a) <- c("mww","hyper3")
for(i in seq_len(n)){
  d <- rrace3(pn=c(a=10,b=10),ps=c(a=0.5,b=0.5))  #null 
  jj <- seq_along(d)
  a[i,1] <- wilcox.test(jj[d=="a"],jj[d=="b"])$p.value
  a[i,2] <- equalp.test(ordervec2supp3(d))$p.value
}
```

```{r,showpvalsNULL}
head(a)
par(pty="s")
plot(log10(a),asp=1,xlim=c(-2,0),ylim=c(-2,0),main="p-values under the NULL (log scale)",
xlab="log(p),Wilcox",ylab="log(p), hyper3")
abline(0,1)
plot(a,asp=1,xlim=c(0,1),ylim=c(0,1),main="p-values under the NULL (linear scale)",xlab="p,Wilcox",ylab="p,hyper3")
abline(0,1)
(size_wilcox <- sum(a[,1]<0.05)/nrow(a))
(size_hyper3 <- sum(a[,2]<0.05)/nrow(a))

table(type_I_wilcox=a[,1]<0.05,type_I_hyper3=a[,2]<0.05)
```


# Some permutation tests under the alternative


```{r,dosomepvals,cache=TRUE}
n <- 100
a <- matrix(0,n,2)
colnames(a) <- c("mww","hyper3")
for(i in seq_len(n)){
  d <- rrace3(pn=c(a=10,b=10),ps=c(a=0.2,b=0.8))
  jj <- seq_along(d)
  a[i,1] <- wilcox.test(jj[d=="a"],jj[d=="b"])$p.value
  a[i,2] <- equalp.test(ordervec2supp3(d))$p.value
}
```

```{r,showpvals}
head(a)
par(pty="s")
plot(log10(a),asp=1,xlim=c(-4,0),ylim=c(-4,0))
abline(0,1)
(power_wilcox <- sum(a[,1]<0.05)/nrow(a))
(power_hyper3 <- sum(a[,2]<0.05)/nrow(a))

table(wilcox=a[,1]<0.05,hyper3=a[,2]<0.05)
```
