```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
download.file('https://wkristan.github.io/template.docx', 'template.docx', mode = 'wb')
```

## Analysis of covariance

We will work on morphological data on three species of hawks, red-tailed hawk (RT), Cooper's hawk (CH), and sharp-shinned hawks (SS). We want to make weight-adjusted comparisons between the species in their wing chords and their tail lengths.

Import the data:

```{r import.data}



```

Note that although these data are real, the original dataset had a fair number of outliers that were very likely to be data recording errors (the values were too large or too small to be correct for these species). Outliers were deleted to make the data set better for teaching purposes.

Before starting on a statistical analysis, we should plot the data:

```{r plot.wing}


 
```

#### Question: which species is heaviest? Which has the longest wing chord?

>

#### Question: we expect RT to have long wings and CH and SS to have short wings, given the differences in how they hunt. Why can't we just compare the wing chords of these birds directly (that is, why do we need to adjust for their weights)?

>

Since log of wing chord and log of weight gives us lines that are closest to parallel, we should use the log of the variables in our analysis too. Log-transform wing chord and weight:

```{r log.transform.wing.weight}



```

Fit the ANCOVA of log of wing chord - use log.wing as the response variable, and log.weight and Species as predictors.

```{r wing.ancova.model}



```

#### Question: which coefficient is the slope of the lines that represents the relationship between log.wing and log.weight? Give the label for the coefficient, and its estimated value to two decimal places.

>

#### Question: the (Intercept) term is actually the intercept of the regression line for one of the species - which one? How do you know?

>

#### Question: what does SpeciesRT tell us? Hint: is it the slope of a line, an intercept, or a difference in intercepts between RT and CH?

>

Get the ANOVA table for the wing ANCOVA model:

```{r wing.ancova.anova.table}



```

#### Question: is there a statistically significant relationship between the log of wing chord and the log of weight? How do you know?

>

#### Question: do the species have different wing chords, after adjusting for weight? How do you know?

>

#### Question: can you tell which species are different at this point, or can you only tell that at least one pair of species is different?

>

To make the ANCOVA graph we want, we need a data frame with species, log.weight, and log.wing re-named as tvar, xvar, and yvar:

```{r make.ancova.wing.points.df}


```

Using this data frame for the points, use emmip() to make a graph that illustrates the parallel lines used by the ANCOVA model running through the data, and that shows the locations of the least-squares means:

```{r wing.graph.ancova}


```

#### Question: the two lines for CH and SS are so similar that you can barely see that there are two of them - what does this tell you about CH and SS wings? Specifically, are their wings shaped differently, or do they only differ in size?

> 

#### Question: why does the amount of difference in wing chord disappear for CH and SS, and get smaller for RT and the two accipiters, when we adjust for log.weight? Answer in terms of the locations of the least squares means compared to the locations of the middle of the data clouds for each species.

> 

Test for which species differ in weight-adjusted wing chords using Tukey tests, and get the weight-adjusted least squares means for the species:

```{r wing.tukey}



```

#### Question: which species have statistically significantly different wing chords? Which do not?

>

#### Question: accipiters, like CH and SS, are supposed to have short wings compared to buteos, like red-tailed hawks. Is this true, according to your results? Explain how you reached this conclusion - if the species are statistically significantly different, refer to the amount of difference between these least squares means (emmeans) in your answer. If they are not statistically significantly different, consider the amount of difference between the least squares means to just be random variation, and do not interpret it.

>

#### Question: both CH and SS are accipiters, do they also differ in weight-adjusted wing chords? How do you know?

>

Compare the predicted mean wing spans for the species at different body weights:

```{r wing.lsmeans}



```

#### Question: is the amount of difference between the species always the same, regardless of the log.mass used?

>

#### Question: are the standard errors the same for every log.mass used? Where are they smallest (and why)?

>

## Tail length

Now repeat the analysis using tail length. Plot the data, and confirm that log-transformation of tail length is needed to produce straight lines:

```{r plot.tail}



```

#### Question: are the patterns the same here as with wing chord? Specifically, do RT have the largest tails, followed by CH and SS?

> 

#### Question: do you expect that adjusting for weight will keep the ordering we see here of RT > CH > SS? Or might the adjustment put a different species on top? Why?

> 

Log of tail and log of weight give us nearly straight lines, better than just transforming weight. So, log-transform tail length:

```{r log.transform.tail}



```

Fit the ANCOVA of tail length - use log.tail as the response variable, and log.weight and Species as predictors.

```{r tail.ancova.model}



```

Get the ANOVA table for the tail ANCOVA model:

```{r tail.ancova.anova.table}



```

#### Question: do you still have a statistically significant difference between the species for this variable?

> 


Test for which species differ in mass-adjusted tail lengths:

```{r tail.tukey}



```

#### Question: which species are statistically significantly different now? Are any not different, like we saw with log.wing?

> 

#### Question: interpret these least squares means - when you adjust for differences in weight, which species has the longest tail? Which has the shortest? Only interpret differences that are statistically significant.

> 

Now graph the model using emmip() from the emmeans package, and place a vertical line at the mean of log.weight to indicate where the least squares means are calculated:

```{r tail.graph.ancova}



```

#### Question: explain how the adjustment for log.weight put the birds with the largest tails (RT) at the bottom.

> 

#### Question: let's wrap this up by revisiting the original scientific hypothesis, that adaptation to different methods of hunting would produce different wing and tail morphology. Based on your results did we find the expected difference between the buteo and the two accipiters? Were the accipiters shaped the same, but only different in size, for both of the variables, or only one of them? Why was it necessary to adjust for the size of the birds in order to see these patterns clearly?

>