---
title: "Generalizing the Mann-Whitney-Wilcoxon test with `hyper3` objects"
author: "R. K. S. Hankin"
output: bookdown::html_document2
bibliography: hyper2.bib  # copy vignettes/hyper2.bib to inst/
---

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

```{r out.width='20%', out.extra='style="float:right; padding:10px"',echo=FALSE}
knitr::include_graphics(system.file("help/figures/hyper2.png", package = "hyper2"))
```

To cite the `hyper2` package in publications, please use @hankin2017_rmd.
Here I discuss the `hyper2` generalization of the
Mann-Whitney-Wilcoxon test, applying it to 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)
```

# References