# Brief introduction to analysis of covariance


```{r setup96, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,message = FALSE,warning = FALSE)

```


```{r}
library(tidyverse)
library(multcomp)
library(aqm)
```


```{block, type="rmdnote", echo =TRUE}

This type of analysis is not needed to complete the assignment as set. However, it could come in useful for some of the possible questions that could be addressed using the data set. The specific relationships shown below are rather trivial, but others may possibly be more interesting. Do not try to use this technique unless you really feel that it is useful and you understand the basic idea. If not, you can ignore this section for this particular unit. We will return to the theme in AQM, as it is rather advanced.

```



Analysis of covariance is a handy technique to know about. The full details around use of the technique and the theory behind it is rather complex, and it can be applied in a variety of ways. However the most common reason to apply analysis of covariance to an observational data set is quite straightforward.

If we can assume that there exists a simple linear relationship between two numerical variables, we might ask if this relationship varies between levels of a categorical variable.


Let's take the sleep data set as an example.

```{r}
data("sleep")
d<-sleep
d$logBodyWt<-log10(d$BodyWt)
d$logBrainWt<-log10(d$BrainWt)
```

We can easily show that there is a (trivially) strong relationship between logBrainWt and LogBodyWt. This is well modelled by regression.

```{r}
g0<-ggplot(data=d,aes(x=logBodyWt,y=logBrainWt)) 
g1 <- g0 + geom_point() + geom_smooth(method="lm")
g1
```

We have already seen that the residuals can be taken from the fitted model and used in a subsequent analysis. 

However, we might ask if the same relationship holds for each level of diet. Let's plot this out and look at it.

```{r}
g0<-ggplot(data=d,aes(x=logBodyWt,y=logBrainWt, col=Diet)) 
g1 <- g0 + geom_point() + geom_smooth(method="lm")
g1
```

It looks as if the slopes of the lines are actually very similar. A test of this consists of fitting a model with an interaction between logBodyWt and Diet. If the interaction is significant then there is an indication of variability between the slopes of the lines.

```{r}
mod<-lm(data=d,logBrainWt~logBodyWt*Diet)
anova(mod)
```

In this case the interaction is not significant F(4,43)=0.4, p= 0.8. So we could stop at this point and conclude that there is no detectable difference between the slopes.  The (weakly) significant effect of diet implies that there may be a difference between logbrainweights that is attributable to diet after accounting for the effect of logBodyWt. This could be investigated more simply by taking the residuals from the overall relationship for all the species. 

If the interaction had been significant then things would get a little more complex, as we may wish to carry out specific comparisons. 

```{r}
# Use filter to select two of the levels
d %>% filter(Diet %in% c("HERB","CARN")) -> d1

g0<-ggplot(data=d1,aes(x=logBodyWt,y=logBrainWt, col=Diet)) 
g1 <- g0 + geom_point() + geom_smooth(method="lm")
g1
```

We have already seen that the overall model did not detect a difference, so when we fit a model to the subset of the data and obtain a confidence interval for the difference between the two slopes we would expect it to include zero. 

```{r}
mod<-lm(data=d1,logBrainWt~logBodyWt*Diet)
confint(mod)
```

Which it does.

There is more to learn about this technique in order to fully appreciate the subtleties involved, but this introduction is enough to allow you to ask (and answer) questions involving differences between the slopes of responses.

## Mussels example

The same sort of (non significant) findings apply to the mussels data.

```{r}
data(mussels)
d<- mussels
```


```{r}
g0<-ggplot(data=d,aes(x=Lshell,y=BTVolume, col=Site)) 
g1 <- g0 + geom_point() + geom_smooth(method="lm")
g1
```


```{r}
mod<-lm(data=d,BTVolume~Lshell*Site)
anova(mod)
```


You might think from the first figure that there could be a difference in slopes between site 1 and site 5. However the overall test has not shown any significant variability so a specific comparison should reach the same conclusion. 
 

```{r}
d %>% filter(Site %in% c("Site_1","Site_5")) ->  d1
g0<-ggplot(data=d1,aes(x=Lshell,y=BTVolume, col=Site)) 
g1 <- g0 + geom_point() + geom_smooth(method="lm")
g1
```


```{r}
mod<-lm(data=d1,BTVolume~Lshell*Site)
anova(mod)
```


```{r}
mod<-lm(data=d1,BTVolume~Lshell*Site)
confint(mod)
```


```{block, type="rmdwarn", echo =TRUE}

As always the caveats regarding statistical signficance apply. You have not shown that there is **no difference**. The analysis fails to detect a difference, given the sample used.

```





