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

## Profile analysis - the multivariate approach to repeated measures analysis

### Import the data

Import the data:

```{r import.leech.data}



```

Load the car library:

```{r library.car}



```

Make a variables list of the days (which are the response variables in our analysis):

```{r make.variables.list}



```

We need to describe the within-subjects design - we will do this by making an R factor out of the list of days:

```{r time.factor}



```

Then we make a data frame out of the time.factor object:

```{r time.frame}



```

Fit a MANOVA model using the weights as the responses, and food as the predictor:

```{r manova.model}



```

Now we have what we need to run the RMA using:

```{r run.rma}



```

Get the multivariate tests:

```{r multivariate.table}



```

#### Question: which effects are signficant? How do you know?

> 

#### Question: which effect is the test of flatness?

> 

#### Question: which effect is the test of parallelism?

> 

## Post-hoc procedures

Now that we know we have an interaction between food and time, we need to find out a) on which days the foods differ, b) within a food type, which successive days differ, and c) on which days do the groups have different amounts of change (i.e. on which days are the changes not parallel)?

### A. On what days do the weights differ between food types?

If we look at one day at a time, we can test on which days the food types differ:

```{r differences.in.weight.each.day}



```

#### Question: what alpha level should you use for determining if these are statistically significant differences at each of the eight days?

> 

#### Question: which of these differences are statistically significant, using the adjusted alpha level you identified?

> 

### B. how is weight changing over time within each food type?

We could also be interested in how the weights change over time within each food type. We need paired t-tests for these comparisons because the same leeches are being measured repeatedly. To make this simpler to calculate for every pair of days, we can calculate the differences by hand, and then do a one-sample t-test of the differences against 0. First, we need to calculate the sequential differences:

```{r calculate.sequential.diffs}



```

We want to know if there are differences between sequential time points within each of the food types, so we should split the data by food type:

```{r split.the.diffs.by.food.type}



```

Conduct all 14 of the paired t-tests (that is, the one-sample t-tests of the differences against 0) using lapply():

```{r paired.t.tests}



```

Get the p-values for the long list of t-tests in diffs.split.ts:

```{r sapply.command.for.p.values}



```

#### Question: what alpha level should you use to decide which of these p-values are statistically significant?

> 

#### Question: which of the sequential differences are statistically significant?

> 

#### Question: why did we need to use paired t-tests for these tests, but could use an un-paired post-hoc comparisons to test the effects of food?

> 

#### Question: why is the comparison between day 2 and day 3 for blood-fed leeches reported as "NaN"?

> 

We may instead be more interested in when the weights start to differ from the first day (initial conditions). The analysis is exactly the same, but we would calculate differences of each day from 1 to 8 from the first day, 0.

```{r diffs.from.initial}



```

Split the differences from initial weights by food type:

```{r split.diffs.from.initial}



```

Run the paired t-tests:

```{r paired.t.diffs.from.initial}



```

Get the p-values for the paired t-tests:

```{r sapply.for.diffs.from.initial.pvals}



```

#### Question: which days differ from initial conditions for arg-fed leeches? What about for blood-fed leeches?

> 

### C - at what days are the sequential differences different between food types?

To see if every pair of sequential differences is different between groups or not, first make a MANOVA of sequential differences compared between food groups:

```{r diffs.by.group}



```

Compare the differences between food types each day:

```{r compare.diffs.each.day}



```

#### Question: what alpha level should you use for these comparisons?

> 

#### Question: are the changes over time different between the groups between every day of measurement, or were the changes the same for both blood and arginine-fed leeches on some days? Be sure to refer to the p-values to support your answer.

> 

Now test when the animals returned to their pre-feeding weight. Split the data by food type:

```{r split.weight.by.food}



```

Conduct one-sample t-tests against a null value of 1:

```{r weights.return.to.prefeeding}



```

#### Question: by what day do the arginine-fed animals return to pre-feeding weight? Do the blood-fed animals return to their pre-feeding weights?

> 