---
title: "Factorial"
author: "Nancy"
date: "26/10/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(SMPracticals)
library(faraway)
par(mfrow=c(2,1))
```

## Two treatment factors, completely randomized design
```{r SM Poisons}
data(poisons, package = "SMPracticals")
pmod <- lm(time ~ poison + treat, data = poisons)
anova(pmod)
with(poisons, interaction.plot(treat, poison, time))
with(poisons, interaction.plot(poison,treat,time))
```

## Randomized block, 1 treatment factor and 1 blocking factor
```{r Faraway Oat Varieties}
data(oatvar, package = "faraway")
xtabs(yield ~ variety + block, data = oatvar)
with(oatvar, interaction.plot(variety,block,yield))
with(oatvar, interaction.plot(block, variety, yield))
lmod <- lm(yield ~ block + variety, data = oatvar)
anova(lmod)
lmod2 <- lm(yield ~ variety + block, data = oatvar)
anova(lmod2)

```

Faraway writes "An examination of which varieties give the highest yields and which are significantly better than others can now follow", but he doesn't do it. Here's a start:
  
```{r mean comparison}
means <- with(oatvar, tapply(yield, variety, mean))
sort(means)
lmod3 <- aov(yield ~ variety + block, data = oatvar)
TukeyHSD(lmod3, "variety") # this is better than the code in the text, we don't want to compare block means.
```
