---
title: "Case Study 11.3: STATS 201/8 Extra Case Study - Oneway ANOVA Model"
output:
  rmarkdown::html_document:
    self_contained: true
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.height=3)
```

```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
## Do not delete this!
## It loads the s20x library for you. If you delete it 
## your document may not compile
library(s20x)
## From now on we also need the emmeans library:
library(emmeans)
```

# Question 1

A musical college wished to study the effectiveness of various practice methods for its trombonists. To this end 30 trombonists were randomly assigned to one of 5 practice methods. Each student’s ability with the trombone was initially assessed. To assess improvement, after a set period of time using the assigned practice method only, they reassessed each student and recorded the difference between the student’s final and initial scores. 

The resulting data are in the file *trombone.csv*, which contains the variables:

Variable  | Description
----------|--------------------------------------------------------
diff      | The difference in trombone assessment scores (after – before).
method    | The practice method assigned to the student: NP - no practice control, MP - mental practice only,  MPS - mental practice with simulated slide movement, PP - physical practice only, CP - combination of mental and physical practice


We are interested in whether any of the methods show significant improvement. If so, which method shows the most significant improvement and by how much?


**Instructions:**

*	Comment on what the plot of the data and summary statistics reveal.
* Fit an appropriate model to the data and check the assumptions. Generate the inference output you need from the final model.
* Write a Method and Assumption Checks section.
* Write an *Executive Summary*. Remember to just focus on answering what we are interested in.

## Question of interest/goal of the study

We wish to identify the most promising trombone practice method. Do any of the methods show significant improvement? If so, which method shows the most significant improvement and by how much?
 

## Inspect the data

```{r}
load(system.file("extdata", "trombone.df.rda", package = "s20x"))
```

```{r,fig.height=5.2,fig.width=8, eval = FALSE}
trombone.df <- read.csv("trombone.csv", header=TRUE, stringsAsFactors = TRUE)

trombone.df$method <- factor(trombone.df$method,levels=c("NP","MP","MPS","PP","CP"))

stripchart(diff~method,data=trombone.df,vertical=T,pch=16,cex=1.5,method="stack",ylab="Difference in scores",main="Score change vs Training method")

summaryStats(diff~method,data=trombone.df)
```

```{r,fig.height=5.2,fig.width=8, echo = FALSE}
trombone.df$method <- factor(trombone.df$method,levels=c("NP","MP","MPS","PP","CP"))
stripchart(diff~method,data=trombone.df,vertical=T,pch=16,cex=1.5,method="stack",ylab="Difference in scores",main="Score change vs Training method")
summaryStats(diff~method,data=trombone.df)
```

## Comment on the plots and summary statistics. 

The no practice group has the lowest average change in score, with half of the students' scores dropping in value. The physical practice and combined mental and physical practice groups appeared to have the highest average increases in scores. The variability of the scores was similar for 3 groups, but lower for the mental practice only and combined groups.


## Fit appropriate model and check assumptions

```{r, fig.height=8,fig.width=8}
trombone.fit <- lm(diff~method,data=trombone.df)

modelcheck(trombone.fit)

anova(trombone.fit)
summary(trombone.fit)

pairs(emmeans(trombone.fit, "method"), infer=T)

# Extra code just to extract significant pairs
trom.pairs=pairs(emmeans(trombone.fit, "method"), infer=T)
sig.pairs=subset(data.frame(trom.pairs),p.value<0.05)
sig.pairs

```

```{r, echo = FALSE}
conf1=as.data.frame(sig.pairs)[2,]
resultStr1 = paste0(sprintf("%.1f", abs(conf1$upper.CL)), " and ", sprintf("%.0f", abs(conf1$lower.CL)))
```


## Methods and assumption checks

As we have one explanatory variable which is a factor with five levels we have fitted a oneway ANOVA model to the data.

One student's performance should not affect the performance of another, and the students are assigned randomly. There are differences in the within-group variability, but we have very small individual sample sizes. As we have equal sample sizes and there is no consistent pattern in the residuals (the variability does not increase with the average), the EOV assumptions should be valid. The residuals look approximately normal. There are no obvious influential points. We can therefore rely on the results from fitting this linear model.

Our model is:

$diff_i = \beta_0 + \beta_{1} \times MP_i + \beta_{2} \times MPS_i + \beta_{3} \times PP_i + \beta_{4} \times CP_i + \epsilon_{i}$

where  $MP_i$ equals 1 if student $i$ was trained using mental practice only and 0 otherwise (similar for $MPS_i$, $PP_i$ and $CP_i$), and $\epsilon_{i} \sim iid ~ N(0,\sigma^2)$. (Note: $\beta_0$ is the mean improvement of students without any practice.)

Alternatively our model is:

$diff_{ij} = \mu + \alpha_i + \epsilon_{ij}$ where $\mu$ is the overall mean improvement score for all students and $\alpha_i$ is the effect of working under strategy $i$.

Our model explains 56% of the variation in improvement scores.

## Executive Summary:

A musical college wished to study the effectiveness of various practice methods for its trombonists. Four methods: mental practice only; mental practice with simulated slide movement; physical practice only; and combined mental and physical practice were trialled and compared to no practice.

We found strong evidence that practice method had an influence on the students improvement scores.

Both physical practice only and a combination of physical and mental practice showed significantly better improvement in scores than the no practice schedule, with the combination showing the largest improvement.

On average, a student following a combined practice schedule improved by between `r resultStr1[1]` points more compared to a student with the no practice schedule.






