---
title: " Revision: Developing Understanding"
author: "Emma Rand"
output:
  html_document:
    depth: 3
    highlight: pygments
    theme: lumen
    toc: yes
    toc_float:
      collapsed: no
      smooth_scroll: no
  pdf_document:
    toc: yes
  word_document: default
---
![](../../pics/17C.png)
```{r setup, include=FALSE}
knitr::opts_chunk$set(
	message = FALSE,
	warning = FALSE,
	include = FALSE
)
```


# Introduction

## Aims

This practical is designed to help you develop real understanding of the way the tests covered work by asking you to consider a research question and determine an appropriate test and generate the data required for particular effects.
This will help you meet the overall Data Analysis learning objectives which were that the successful student will be able to:

1. Explain the purpose of data analysis
2. Name, identify and choose classical univariate statistical tests (and
some non-parametric equivalents) appropriate to a given scenario
and recognise when these are not suitable
3. Use R to perform these analyses on data in a variety of formats
4. Interpret, report and graphically present the results of covered tests



## Slides

Revision: Developing Understanding: [pdf](../lects/web10Developing Understanding.pdf) (recommended) / [pptx](../lects/web10Developing Understanding.pptx)



## Getting started

![W](../../pics/W.png) Start RStudio from the [Start menu](../../pics/startmenu.png).

![R](../../pics/R.png) Make a new project with File | New Project and chose New directory and then New project. Be purposeful about where you create it by using the Browse button. I suggest using your 17C folder. Give the Project (directory) a name, perhaps "revision"

![R](../../pics/R.png) Make a new folder 'data' where you will later save data files.

![R](../../pics/R.png) Make a new folder 'figures' where you will later save your figures.

![R](../../pics/R.png) Make a [new script](../../pics/newscript.png) file called analysis.R or similar to carry out the rest of the work.

![R](../../pics/R.png) You probably want to load the `tidyverse` with `library(tidyverse)`.
```{r}
library(tidyverse)
```

# Practical exercises

## Instructions 

I suggest you work with one or two other people.

For each of the scenarios you need to:

* identify an appropriate design and statistical test for the general research question 
* generate the data using the random number functions that would give the effects specified. See the useful commands section below. 
* run the test you chose and regenerate the data to get the desired effect if needed
* create figures to accompany the results

Expect to need to discuss your ideas with others, including demonstrators and to have to generate a few datasets before you work out how to get the data which show the requested effects. This is a different way to think but one that is very instructive and will improve your understanding. There plenty of exercises so you may need to continue the practical independently.



## Some useful commands

To create a variable x of 10 random numbers with a mean of 15 and a standard deviation of 2:
```{r echo=TRUE}
x <- rnorm(10, 15, 2)
```
To round those numbers to 2 decimal places:
```{r include=TRUE}
x <- round(x, 2)
```
To create variable y of 10 random numbers with a mean of 18 and a standard deviation of 2:
```{r include=TRUE}
y <- rnorm(10, 18, 2)
```
To create dataframe with the values of x and y in one column and a grouping variable in another:
```{r include=TRUE} 
data <- stack(data.frame(x, y))
```
To rename dataframe column names: 
```{r include=TRUE}
names(data) <- c("measure", "group")
```
To create a similar dataframe in a different way:
```{r include=TRUE} 
mean1 <- 15
mean2 <- 18
# make all the values in a group the group mean
measure <- rep (c(mean1, mean2), each = 10)
# then add the random variation
measure <- measure + rnorm(20, 0, 2)
# create the grouping variable
group <- rep(c("x", "y"), each = 10)
# put them in a dataframe
data <- data.frame(measure, group)
```
To write your dataframe to a file so you can access the data outside of R: 
```{r eval=FALSE, include=TRUE}
write.table(data, file = "data/mydat.txt", row.names = F)
```

## The scenarios 

1. Researchers were interested in the effect of a drug treatment on blood sugar. They had 30 people and gave 15 the drug and 15 a placebo before measuring their blood glucose. 
What might be an appropriate parametric test?
Create a data set on which you can use that test and which indicates the drug decreases blood glucose relative to the placebo. Try to generate data that give a highly significant result and only just significant result.  Consider how the data and analysis would differ if they had only 15 people and gave each of them both the drug and the placebo.
```{r}
#--- ANSWER---
# blood glucose is the response variable, then there are two groups in the treatment. since the 15 people in each group are different (one measure for each person) then we have a two-sample t-test.
# we need to generate data which meet the assumptions of the two-sample t which means both samples are normally distributed and and the variances are the same. the effect given is that the glucose should be lower in the drug group than the placebo group
# first the placebo group. i'm going to make the blood glucose level have a mean of 10 and an sd of 1.5
placebo <- rnorm(15, 10, 1.5)
# now the drug group. the blood glucose level needs to be lower - i'll use 8. The sd should the same 1.5
drug <- rnorm(15, 8, 1.5)    
# create dataframe with the glucose in one column and a grouping variable in another:
bloodglucose <- stack(data.frame(placebo, drug))
# To rename dataframe column names: 
names(bloodglucose) <- c("glucose", "treatment") 
# write the data to file
# write.table(bloodglucose, file = "data/bloodglucose.txt", row.names = F)
# doing a two sample t
t.test(bloodglucose$glucose ~ bloodglucose$treatment, var.equal = T, paired = F)
# mine is sig. the way to alter the significance level is to adjust the difference between the means and/or the sd. if the difference between the means is big relative to the sd then the test will be more significant. if the difference between the means is small relative to the sd then the test will be less significant - and you can go as far as making it NS
# figure - either of these would be fine
glusummary <- bloodglucose %>%
  group_by(treatment) %>% 
  summarise(mean = mean(glucose),
            std = sd(glucose),
            n = length(glucose),
            se = std/sqrt(n))

fig <- ggplot() +
  geom_point(data = bloodglucose, aes(x = treatment, y = glucose),
             position = position_jitter(width = 0.1, height = 0),
             colour = "gray50") +
  geom_errorbar(data = glusummary, 
                aes(x = treatment, ymin = mean - se, ymax = mean + se),
                width = 0.3) +
  geom_errorbar(data = glusummary, 
                aes(x = treatment, ymin = mean, ymax = mean),
                width = 0.2) +
  ylab("Glucose (units)") +
  xlab("Treatment") +
  ylim(0, 15) +
  theme_classic()

```

2. Can you manipulate the data (change the values, add data or generate new data) in the the above so that a parametric test is no longer possible? The easiest way to do this is write your dataframe to a file,  edit some of the values then read it in again.
What might be an appropriate non-parametric test? 
```{r}
#--- ANSWER---
# a parametric test would no longer be possible if the data were not normal.
# there are several ways this could be tackled. 
# You can open the data file and replace some of the values - make several the same/nearly the same 
# or add extreme values or make them bimodal

# another way is to generate random numbers from a different distribution
# for example, the binomial distribution
bloodglucose$glucose2 <- c(rbinom(15, 8, 0.2), rbinom(15, 8, 0.1))

# mann-whitney aka wilcoxon rank sum test would be the non-parametric alternative
wilcox.test(bloodglucose$glucose2 ~ bloodglucose$treatment, paired = F)

```

3. Researchers were interested in the effect of location on the size of a particular species of beetle. They measured 8 individuals from each of four locations, alpha, beta, gamma, delta.
What might be an appropriate parametric test?
Create a data set on which you can use that test and which indicates differences between all populations except alpha and beta.
```{r}
#--- ANSWER---
#size is the response variable and location is the explanatory variable. since there are four locations we need a one-way anova with a post-hoc test
# i am going to set the means for each group then add random variation to all of them but i could use the same method as i used for the two-sample t-test and randomly draw the four sample separately
#i'm setting a and b to be the same as requested
meana <- 5
meanb <- 5
meanc <- 7
meand <- 9
#the size of each individual if every individual is its location mean size
size <- rep (c(meana, meanb, meanc, meand), each = 8)
#adding random variation
size <- size + rnorm(32, 0, 1)
#making the group variable
location <- rep(c("a", "b", "c", "d"), each = 8)
#putting the two variables together
beetles <- data.frame(size, location)

# running the anova
mod <- aov(beetles$size ~ beetles$location)
summary(mod)
TukeyHSD(mod)
fig <- plot(TukeyHSD(mod), cex.axis = 0.7)


beetsummary <- beetles %>%
  group_by(location) %>% 
  summarise(mean = mean(size),
            std = sd(size),
            n = length(size),
            se = std/sqrt(n))

fig <- ggplot() +
  geom_point(data = beetles, aes(x = location, y = size),
             position = position_jitter(width = 0.1, height = 0),
             colour = "gray50") +
  geom_errorbar(data = beetsummary, 
                aes(x = location, ymin = mean - se, ymax = mean + se),
                width = 0.3) +
  geom_errorbar(data = beetsummary, 
                aes(x = location, ymin = mean, ymax = mean),
                width = 0.2) +
  ylab("Size (units)") +
  xlab("Location") +
  ylim(0, 10) +
  scale_x_discrete(labels = c("alpha", "beta", "gamma", "delta")) +
          annotate("text", x = 1, y = 7, label = "a") +
          annotate("text", x = 2, y = 7, label = "a") +
  theme_classic()


```

 
4. Researchers were interested in the effect of two treatments, T1 and T2, on neuron length. They wanted to know if each treatment had an effect and if the treatments acted independently. They created four treatment combinations: neither treatment, with T1, with T2 and with both T1 and T2. 
What might be an appropriate parametric test?
Create a data set on which you can use that test and which indicates T1 has a significant effect, T2 does not and the two treatments act independently.
```{r}
#--- ANSWER---
# neuron length is the response variable and T1 and T2 are the explanatory variables. since there are two explanatory variables we need a two-way anova. since there are only two levels of each factor, we are not going to need a post-hoc test
# i am going to set the means for each group then add random variation to all of them but i could use the same method as i used for the two-sample t-test and randomly draw the four sample separately
#one way to tackle this problem is to think about what the graph would look like. It will be a clustered errorbar with one treatment on the x-axis and the other shown by different coloured/shaped points. If T2 is on the x-axis and T1 is the different bars then we want (here shown as bars)
#
#   ___      ___      T1
#   |X|      |X|   [ ] 0
#___|X|   ___|X|   [x] 1
#| ||X|   | ||X|
#| ||X|   | ||X|
#| ||X|   | ||x|
#  0        1
#      T2
#
#the T2 0 and T2 1 are the same on average - i.e., no effect of T2. There is a difference between T1 0 and 1 and that difference is the same of both levels of T2 so there is no interaction
#
#
# I've gone with 8 reps. it wasn't specified

mean00 <- 10
mean01 <- 10
mean10 <- 12
mean11 <- 12
#the neuron length of each rep if every rep is its treatment combination mean length
length <- rep (c(mean00, mean01, mean10, mean11), each = 8)
#adding random variation
length <- length + rnorm(32, 0, 2)
#making the group variable
T1 <- rep(c("0", "1"), each = 16)
T2 <- rep(c("0", "1"), each = 8, times = 2)
#putting the three variables together
data <- data.frame(length, T1, T2)

# running the anova
mod <- aov(data$length ~ data$T1*data$T2)
summary(mod)    
#i do have the intended effects
neursummary <- data %>% 
  group_by(T1, T2) %>% 
  summarise(mean = mean(length),
            std = sd(length),
            n = length(length),
            se = std/sqrt(n))
  


fig <- ggplot() +
  geom_point(data = data, aes(x = T1, y = length, shape = T2),
             position = position_jitterdodge(dodge.width = 1,
                                             jitter.width = 0.4,
                                             jitter.height = 0),
             colour = "gray50") +
  geom_errorbar(data = neursummary, 
                aes(x = T1, ymin = mean - se, ymax = mean + se, group = T2),
                width = 0.4, size = 1,
                position = position_dodge(width = 1)) +
  geom_errorbar(data = neursummary, 
                aes(x = T1, ymin = mean, ymax = mean, group = T2),
                width = 0.3, size = 1,
                position = position_dodge(width = 1) ) +
  ylab("Length (units)") +
  ylim(0, 14) +
  xlab("Treatment 1") +
  theme_classic() +
  theme(legend.position = c(0.2, 0.2))
  

     
```

5. Using the scenario above, create a data set on which that test indicates no apparent effect of each treatment on average, but that the treatments do not act independently.
```{r}
#--- ANSWER---
# if there is an interaction, but the treatments seem ns then that suggests one treatment has opposite effects on each level of the other:
#
#   ___   ___         T1
#   |X|   | |      [ ] 0
#___|X|   | |___   [x] 1
#| ||X|   | ||X|
#| ||X|   | ||X|
#| ||X|   | ||x|
#  0        1
#      T2
#

mean00 <- 10
mean01 <- 12
mean10 <- 12
mean11 <- 10
# the neuron length of each rep if every rep is its treatment combination mean length
length <- rep (c(mean00, mean01, mean10, mean11), each = 8)
# adding random variation
length <- length + rnorm(32, 0, 2)
# making the group variable
T1 <- rep(c("0", "1"), each = 16)
T2 <- rep(c("0", "1"), each = 8, times = 2)
# putting the three variables togther
data <- data.frame(length, T1, T2)
mod <- aov(data$length ~ data$T1*data$T2)
summary(mod)    
# i have the intended effects
neursummary <- data %>% 
  group_by(T1, T2) %>% 
  summarise(mean = mean(length),
            std = sd(length),
            n = length(length),
            se = std/sqrt(n))
  


fig <- ggplot() +
  geom_point(data = data, aes(x = T1, y = length, shape = T2),
             position = position_jitterdodge(dodge.width = 1,
                                             jitter.width = 0.4,
                                             jitter.height = 0),
             colour = "gray50") +
  geom_errorbar(data = neursummary, 
                aes(x = T1, ymin = mean - se, ymax = mean + se, group = T2),
                width = 0.4, size = 1,
                position = position_dodge(width = 1)) +
  geom_errorbar(data = neursummary, 
                aes(x = T1, ymin = mean, ymax = mean, group = T2),
                width = 0.3, size = 1,
                position = position_dodge(width = 1) ) +
  ylab("Length (units)") +
  ylim(0, 14) +
  xlab("Treatment 1") +
  theme_classic() +
  theme(legend.position = c(0.2, 0.2))
  
     
```
    
6. Researchers wanted to know if the blood glucose response to a drug treatment could be predicted from the dose. They gave doses from 5mg to 50mg in steps of 5mg and and measure blood glucose.
What might be an appropriate parametric test?
Create a data set on which you can use that test and which indicates a significant positive relationship between dose and response. Try to generate data that give a highly significant result and only just significant result.
```{r}
#--- ANSWER---
# our reponse variable is blood glucose and our explanatory variable is something we have set. this is a regression
# first make the x variable
dose <- seq(5, 50, 5)
# the y variable is going to be produced by a straight line relationship with some random variation
# we can chose any numbers for the intercept and slope but the slope must be positive
b0 <- 10
b1 <- 2
glucose <- b0 + b1*dose
# add the random variation
glucose <- glucose + rnorm(10, 0, 10)
data <- data.frame(glucose, dose)
mod <- lm(data = data, glucose ~ dose)
summary(mod)
fig <- ggplot(data, aes(x = dose, y = glucose)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE, colour = "black")+
  ylab("Glucose (units)") +
  xlab("Dose (mg)") +
  theme_classic()


```
    
7. Generate the data for a significant negative correlation.
```{r}
#--- ANSWER---
# for a correlation, both variables need to be randomly sampled and all we need to do to give a negative correlation is to sort them so the largest scores of one variable go with the smallest scores on the other
# it doesn't matter what the values themselves are, so i have gone with the default mean = 0 and sd = 1
x1 <- rnorm(10)
x2 <- rnorm(10)    
# now sort in opposite directions
x1 <- sort(x1, decreasing = FALSE)
x2 <- sort(x2, decreasing = TRUE) 
cor.test(x1, x2)

# our r value will be close to minus 1 because we don't have any point out of order. if ytoiu do a rank correlation on that data the r will be exactly -1
cor.test(x1, x2, method = "spearman")

```


# The Rmd file
This is file I use to compile the practical. Rmd stands for R markdown allow R code and ordinary text to be interweaved to produce well-formatted reports including webpages.
These contain answers and code even though they do not appear on the webpage itself.

[Rmd file](09DevelopingUnderstanding.Rmd)

# Objectives from previous sessions

[Introduction to module and RStudio](01IntroductionToModuleAndRStudio.html)

* to explain why we need statistical tests and the logic of hypothesis testing (MLO 1)
* use the R command line as a calculator and to assign variables (MLO 3)
* create and use the basic data types in R (MLO 3)
* find their way around the RStudio windows (MLO 3)
* create, use and save a script file to run r commands (MLO 3)
* search and understand manual pages (MLO 3)

[Testing, Data types and reading in data](02TestingDataTypesReadingInData.html)

* to able to explain what response and explanatory variables are, distinguish between data types and describe how these impact choice of test (MLO 1 and 2)
* demonstrate the process of hypothesis testing with an example and evaluate potential inferences (MLO 1 and 2)
* read in data in to RStudio, create simple summaries and plots using manual pages where necessary (MLO 3)
* create neat reports in Word which include text and figures (MLO 4)

[Goodness of Fit and Contingency chi-squared tests](03ChiSquaredTests.html)

* recognise when to use chi-squared Goodness of Fit and Contingency tests (MLO 2)
* be able to carry out, interpret and report scientifically both types of test by hand and in R (MLO 3 and 4)

[Calculating summary statistics, probabilities and confidence intervals](04CalculatingSummaryStatisticsProbabilitiesAndCI.html)

* Explain the properties of 'normal distributions' and their use in statistics (MLO 1 and 2)
* Define, select and calculate with R probabilities, quantiles and confidence intervals (MLO 3 and 4)

[One- and two- sample tests](05OneAndTwoSampleTests.html)

* Explain dependent and independent samples (MLO 2)
* Select, appropriately, t-tests and their non-parametric equivalents (MLO 2)
* Apply, interpret and evaluate the legitimacy of  the tests in R (MLO 3 and 4)
* Summarise and illustrate with appropriate R figures test results scientifically (MLO 3 and 4)

[One-way ANOVA and Kruskal-Wallis](06One-wayAnovaAndKW.html)

* Explain the rationale behind ANOVA and complete a partially filled ANOVA table (MLO 1 and 2)
* Apply (appropriately), interpret and evaluate  the legitimacy of, one-way ANOVA and Kruskal-Wallis including post-hoc tests in R (MLO 2, 3 and 4)
* Summarise and illustrate with appropriate R figures test results scientifically (MLO 3 and 4)

[Two-way ANOVA](07Two-wayAnova.html)

* Explain the rationale behind ANOVA and complete a partially filled ANOVA table (MLO 1 and 4)
* Read in data formatted for other statistical packages (MLO 3)
* Apply (appropriately), interpret and evaluate the legitimacy of, two-way ANOVA in R (MLO 2, 3 and 4)
* Explain the meaning of a significant interaction (MLO 4)
* Summarise and illustrate with appropriate figures test results scientifically (MLO 3 and 4)
* Use RStudio projects (MLO 4)

[Correlation and Regression ](08CorrelationAndRegression.html)

* Explain the principles of correlation and of regression (MLO 1)
* Apply (appropriately), interpret and evaluate the legitimacy of, both in R (MLO 2, 3 and 4)
* Summarise and illustrate with appropriate R figures test results scientifically (MLO 3 and 4)

![](../../pics/17Cend.png)
