---
title: "Rock fries your brains"
author: ""
date: ""
output:
  html_document:
    fig_height: 3
    fig_width: 5
  pdf_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 3
    fig_width: 5
---

```{r, setup, include=FALSE}
require(mosaic)   # Load additional packages here 

# Some customization.  You can alter or delete as desired (if you know what you are doing).
trellis.par.set(theme=theme.mosaic()) # change default color scheme for lattice
knitr::opts_chunk$set(
  tidy=FALSE,     # display code as typed
  size="small")   # slightly smaller font for code
```

# The data

The data for this experiment came from a student named David Merrell, who collected them as part of a high school science project.

Merrell raised three groups of mice.

- Group 1 was raised in the absence of music
- Group 2 heard 10-12 hours of Mozart every day
- Group 3 heard 10-12 hours of rock music (performed by the group Anthrax) every day

They were originally taught to navigate a maze for food, and then were placed back in the maze each week and the time to run the maze was collected. (Actually there were three trials each week, and the data file gives those three times). 
Included are also the mean and the median running time per week. 
Finally, the weights of each mouse when they were received from the breeding lab and at each of the four weeks of testing (wt0 to wt4) are included.

In the data, missing values are coded as 999 (but this is handled by the command below).

## Load data
Load data from URL:

```{r}
musik <- read.delim("https://asta.math.aau.dk/datasets?file=musik.txt", na.strings = "999")
```

## Convert group variable to factor
We now convert the group variable to a factor:

```{r}
musik$group <- factor(musik$group, levels = c(1, 2, 3), labels = c("Control", "Mozart", "Rock"))
```

# Exploratory analysis

## Numerical summaries

Let us see a summary of the `week1` means for each group:

```{r}
favstats(week1 ~ group, data = musik)
```

And the corresponding summary for `week4` means for each group:

```{r}
# Delete this line and add the correct code yourself
```

We can even improve the print out of tables by using the package `pander`:
```{r}
week1 <- favstats(week1 ~ group, data = musik)
library(pander)
pander(week1)
```

## Boxplots

Let us try to illustrate all four weeks' means using boxplots:

```{r, warning = F}
gf_boxplot(week1 ~ group, data = musik) %>% gf_labs(y = "Week1 mean")
gf_boxplot(week2 ~ group, data = musik) %>% gf_labs(y = "Week2 mean")
gf_boxplot(week3 ~ group, data = musik) %>% gf_labs(y = "Week3 mean")
gf_boxplot(week4 ~ group, data = musik) %>% gf_labs(y = "Week4 mean")
```

If we want to arrange these in a joint plot we can use `grid.arrange` from the package `gridExtra`.
Here we save each plot and then arrange them in a grid afterwards:

```{r fig.height=5, fig.width = 10, warning = F, message = F}
w1 <- gf_boxplot(week1 ~ group, data = musik) %>% gf_labs(y = "Week1 mean")
w2 <- gf_boxplot(week2 ~ group, data = musik) %>% gf_labs(y = "Week2 mean")
w3 <- gf_boxplot(week3 ~ group, data = musik) %>% gf_labs(y = "Week3 mean")
w4 <- gf_boxplot(week4 ~ group, data = musik) %>% gf_labs(y = "Week4 mean")
library(gridExtra)
grid.arrange(w1, w2, w3, w4, ncol = 2)
```

## Scatter plots

We can also compare the `week1` performance with `week4` performance for each subject by using one or more scatter plots using `gf_point` function. A basic scatter plot doesn't tells us much (other than some mice have become much slower in week 4):

```{r, warning = F}
gf_point(week4 ~ week1, data = musik)
```

Try to add chunks where you divide the data into groups either by conditioning with ` | group` in the formula. Can you predict what they will do?

Does it seem like there is any difference between the groups?

As a further source of inspiration you can have a look at the [data source](http://www.uvm.edu/%7Edhowell/StatPages/More_Stuff/Anthrax.html), where you also can find some suggestions to the data analysis.
