---
  title: "Testing for Multiple Comparisons and the Fisher Assumptions"
author: "Randi Garcia"
output:
  html_document:
  theme: cerulean
highlight: pygments
css: ../lab.css
toc: true
toc_float: true
---
  
```{r global-options, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
library(dplyr)
library(lattice)
library(ggplot2)
library(tidyr)
library(mosaic)
library(forcats)
```

## SP/RM[2,1]

```{r}
osmo <- read.csv("http://www.science.smith.edu/~rgarcia/sds290-S18/osmoregulation3.csv")
```

```{r}
osmo <- osmo %>%
  mutate(Worms = as.factor(Worms),
         Time = as.factor(Time),
         Species = as.factor(Species),
         Concentration = as.factor(Concentration))
```

## Informal analyses

Facet wrap for 3-way interaction plots

```{r}
ggplot(osmo, aes(x = Time, y = Data, color = Species)) +
  geom_boxplot() +
  facet_wrap(~Concentration)
```

## Formal Analysis

ANOVA

```{r}
mod <- aov(Data ~ Time*Concentration*Species + Error(Worms) + Concentration*Species, data = osmo)

summary(mod)
```

## Adjusting for Multiple Comparisons

```{r}
library(lsmeans)

lsmeans(mod, pairwise ~ Time, adjust="tukey")
```

## Testing Assumptions

```{r}
library(dae)

ggplot(osmo, aes(x = residuals(mod))) + 
  geom_histogram()

ggplot(osmo, aes(sample = residuals(mod))) + 
  geom_qq()

ggplot(osmo, aes(x = fitted(mod), y = residuals(mod))) + 
  geom_point() +
  geom_hline(yintercept = 0, color = "red")
```

