---
title: "Lecture11"
output: html_document
---



```{r}
# Simulation shows that the U- (resp. W-) statistics is normally distributed.
simulation <- function(Levels, N, N1, N2) {
  data <- c()
  for(i in 1:N) {
    s1 <- sample(Levels, N1, replace = T) 
    s2 <- sample(Levels, N2, replace = T) 
    ranks <- rank(c(s1, s2))
    r1 <- sum(ranks[1:N1]) - sum(1:N1)
    r2 <- sum(ranks[(N1+1):(N1+N2)]) - sum(1:N2)
    data <- rbind(data, c(r1, r2))
  }
  return(data.frame(U1 = data[,1], U2 = data[,2]))
}


Levels <- c("a", "b", "c", "d", "e", "f", "g")

N = 10000
n1 = 20
n2 = 20
sim <- simulation(Levels, N, n1, n2)
hist(sim$U1)
hist(sim$U2)

cat("Mean theoretical: ", n1*n2/2, "Mean empirical:", mean(sim$U1),"\n")
cat("SD theoretical (uncorrected):", sqrt(n1*n2*(n1+n2+1)/12), "SD empirical:", sd(sim$U1), "\n")
```

```{r}
# U-Test vs. t-Test
N = 1000
occ <- c()
for(i in 1:N) {
  g1 <- rnorm(30)
  g2 <- rnorm(30)
  occ <- rbind(occ, c(t.test(g1, g2)$p.value, wilcox.test(g1, g2)$p.value))
}

# U and t  (Type-1 Errors)
# t-Test wrong
nrow(occ[occ[,1] < 0.05,])

# U-test wrong
nrow(occ[occ[,2] < 0.05,])

# Disagreements
nrow(rbind(occ[occ[,1] < 0.05 & occ[,2] > 0.05,],
occ[occ[,1] > 0.05 & occ[,2] < 0.05,]))/N

```

```{r}
# U-Test vs. t-Test (Ordinal)
N = 1000
Levels <- c(1,2,3,4,5,6,7)
occ <- c()
for(i in 1:N) {
  g1 <- sample(Levels, 30, replace = T)
  g2 <- sample(Levels, 30, replace = T)
  occ <- rbind(occ, c(t.test(g1, g2)$p.value, wilcox.test(g1, g2, exact=F)$p.value))
}

# U and t  (Type-1 Errors)
# t-Test wrong
nrow(occ[occ[,1] < 0.05,])

# U-test wrong
nrow(occ[occ[,2] < 0.05,])

# Disagreements
nrow(rbind(occ[occ[,1] < 0.05 & occ[,2] > 0.05,],
occ[occ[,1] > 0.05 & occ[,2] < 0.05,]))/N

```

```{r}
# U-Test vs. t-Test
N = 1000
occ <- c()
for(i in 1:N) {
  g1 <- rnorm(30)
  g2 <- rnorm(30, mean = 1)
  occ <- rbind(occ, c(t.test(g1, g2)$p.value, wilcox.test(g1, g2)$p.value))
}


# U and t  (Type-2 Errors)
# t-Test wrong
nrow(occ[occ[,1] > 0.05,])

# U-test wrong
nrow(occ[occ[,2] > 0.05,])

# Disagreements
nrow(rbind(occ[occ[,1] < 0.05 & occ[,2] > 0.05,],
occ[occ[,1] > 0.05 & occ[,2] < 0.05,]))/N
```

```{r}
# U-Test vs. t-Test (Ordinal)
N = 1000
Levels1 <- c(1,2,3,4,5,6,7)
Levels2 <- c(1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7)
occ <- c()
for(i in 1:N) {
  g1 <- sample(Levels1, 30, replace = T)
  g2 <- sample(Levels2, 30, replace = T)
  occ <- rbind(occ, c(t.test(g1, g2)$p.value, wilcox.test(g1, g2, exact=F)$p.value))
}

# U and t  (Type-2 Errors)
# t-Test wrong
nrow(occ[occ[,1] > 0.05,])

# U-test wrong
nrow(occ[occ[,2] > 0.05,])

# Disagreements
nrow(rbind(occ[occ[,1] < 0.05 & occ[,2] > 0.05,],
occ[occ[,1] > 0.05 & occ[,2] < 0.05,]))/N
```

```{r}
# Wilcoxon Signed-Rank-Test Example
before <- c(1, 2, 2, 3, 3)
after <- c(2, 3, 3, 3, 1)

wilcox.test(before, after, paired = T, correct = F, exact = F)
t.test(before, after, paired = T, correct = F)
```

```{r}
# Wilcoxon Rank-Sum-Test Example
m1 <- c(1, 1, 2, 2, 2)
m2 <- c(2, 3, 3, 3, 2)

wilcox.test(m1, m2, correct = F)
```

```{r}
# Kruskall-Wallis-Test Example
m1 <- c(1, 1, 2, 2, 2)
m2 <- c(2, 3, 3, 3, 2)
m3 <- c(1, 2, 2, 1, 2)

kruskal.test(list(m1, m2, m3))
```


