```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, fig.align="center")
```

#### Statistics for Laboratory Scientists ( 140.615 )

## Permutation and Non-Parametric Tests

#### Example 1 from class

The paired data.

```{r}
x <- c(117.3, 100.1,  94.5, 135.5,  92.9, 118.9, 144.8, 103.9, 103.8, 153.6, 163.1)
y <- c(145.9,  94.8, 108.0, 122.6, 130.2, 143.9, 149.9, 138.5,  91.7, 162.6, 202.5)
d <- y-x
d
```

The Wilcoxon signed rank test.

```{r}
wilcox.test(d)
```

The t-test.

```{r}
t.test(y,x,paired=TRUE)
t.test(d)
```

#### Example 2 from class

The two-sample data.

```{r}
x <- c(43.3, 57.1, 35.0, 50.0, 38.2, 61.2)
y <- c(51.9, 95.1, 90.0, 49.7, 101.5, 74.1, 84.5, 46.8, 75.1)
```

The Wilcoxon rank-sum test.

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

The t-test.

```{r}
t.test(x,y)
```

#### Example 3

```{r,fig.width=4}
xA <- c(27.0,54.6,33.5,27.6,46.0,22.2,44.2,17.3,15.9,32.8)
xB <- c(17.4,20.5,13.9,14.8,27.9,10.6,33.7,15.4,25.0,24.1)

stripchart(list(xA,xB),vertical=T,pch=21,xlim=c(0.5,2.5))
segments(0.9,mean(xA),1.1,mean(xA),lwd=2,col="red")
segments(1.9,mean(xB),2.1,mean(xB),lwd=2,col="blue")
abline(h=mean(c(xA,xB)),lty=2)

t.test(xA,xB)
wilcox.test(xA,xB)
```

Let's see how one outlier influences the test statistics!

```{r,fig.width=4}
sort(xA)
sort(xA,decreasing=TRUE)
order(xA)
order(xA,decreasing=TRUE)

xAnew <- xA
xAnew[2] <- 99.9
xA
xAnew

stripchart(list(xAnew,xB),vertical=T,pch=21,xlim=c(0.5,2.5))
segments(0.9,mean(xAnew),1.1,mean(xAnew),lwd=2,col="red")
segments(1.9,mean(xB),2.1,mean(xB),lwd=2,col="blue")
abline(h=mean(c(xAnew,xB)),lty=2)

t.test(xA,xB)$p.value
t.test(xAnew,xB)$p.value # why is the p-value now > 0.05?
wilcox.test(xA,xB)$p.value
wilcox.test(xAnew,xB)$p.value # why are the p-values the same?
```
