---
title: "Rock fries your brains 3"
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

To read more about the dataset and see an explorative analysis have a look at the Rmarkdown file `rock-fries-your-brains.Rmd` in the exercises for the first lecture.

### Load data
In the data, missing values are coded as 999 (but this is handled by the command 
below that replace 999 by NA). Loading data:

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

## Mozart versus control group

We will compare the control group with the Mozart group, i.e. we leave
out the Anthrax group (group=3):
```{r}
noAnthrax <- subset(musik, group < 3)
```
Now we have a new dataset called `noAnthrax` which we will use in the remainder.
First change the group variable into a factor with sensible names:
```{r}
noAnthrax$group <- factor(noAnthrax$group, labels = c("control", "Mozart"))
```

### Median time week 1

Consider the response variable `median1` and compare it for the control and Mozart groups.
I.e. use a $t$-test to investigate whether the mean resoponse is different for the two groups. To do this you need to insert a new code chunk and write something like `t.test(response var. ~ grouping var. , data = ???)` with appropriate changes on your own:

Supplement this analysis with a boxplot:
```{r}
# Delete this and write something like gf_boxplot(??? ~ ??? , data = ???)
```

### Median time week 4

Make a similar analysis for `median4`

## Paired test for control group

We now only consider the control group and the response `median` for this group.
It is measured in week1 (`median1`) and week4 (`median4`) and we make a simple
dataset where we stack the median values from week 1 and week 4 on top of each other. The column `values` contains the values (median time) and the column `ind` is an indicator of week 1 or 4 (it is always good to have a look at the first few lines of the dataset with the function `head`):
```{r}
control <- subset(noAnthrax, group == "control")
median14 <- stack(na.omit(control), c(median1, median4))
head(median14)
```

The following commands give a comparative analysis of the median from week1 to week4. Go through the
output and explain what you see in the descriptive comparison (summary statistics and boxplots) as well
as the inferential comparison (t-test).
```{r}
favstats(values ~ ind, data = median14)
gf_boxplot(values ~ ind, data = median14)
t.test(values ~ ind, data = median14, paired = TRUE)
```
