## Motivating Example

- Kick off the workshop by exploring a real data set using R!
--


- Goal: get the flavor of using R for data management and exploration
--


- Don't worry about understanding all the coding right away

- We will go back and explain how it all works in detail

- Follow along using [2-MotivatingExample.R](../code/2-MotivatingExample.R)


---
## Getting Started

Let's begin by installing and loading `tidyverse`:

```{r, eval=FALSE}
install.packages("tidyverse")
library(tidyverse)
```

```{r echo=FALSE, message=FALSE, warning=FALSE}
library(tidyverse)
```

This workshop will focus largely on a group of packages that live together under the name `tidyverse`. 

`tidyverse` includes the following well known packages:

- `ggplot2`
- `dplyr`
- `tidyr`
- `readr`
- and more!

---

## Salmonella Shedding Data

- Fecal *Salmonella* shedding recorded over the course of 21 days

- Several **variables** were recorded for each pig at certain time points:

    - Amount of *Salmonella* in feces (log10)
    - Pig weight (kg)
    - Dietary treatment group
    
- **Primary Question**: Do the different dietary treatments affect *Salmonella* shedding?


---
## First look at data in R

Lets use R to look at the top few rows of the *Salmonella* shedding data set. First, we load tip using `read_csv`:

```{r message=FALSE, warning=FALSE}
shed <- read_csv("http://heike.github.io/rwrks/01-r-intro/data/daily_shedding.csv")
```

Now, we use the `head` function to look at the first 6 rows of the data:

```{r}
head(shed)
```

---
## *Salmonella* Shedding Data: Attributes

How big is this data set and what types of variables are in each column?

```{r}
str(shed)
```

---
## *Salmonella* Shedding: Variables

Let's get a summary of the values for each variable:

```{r}
summary(shed)
```

---
## Scatterplots

Lets look at the relationship between time point and *Salmonella* shedding.  

```{r, fig.height=5, fig.width=8}
ggplot(shed, aes(x=time_point, y=total_shedding)) + geom_point()
```

---
## More Scatterplots

Color the points by treatment groups

```{r, fig.height=5, fig.width=9}
ggplot(shed, aes(x=time_point, y=total_shedding, colour = treatment)) + geom_point()
```

---

## More Scatterplots

Switch to lines for easier reading

```{r, fig.height=5, fig.width=9}
ggplot(shed, aes(x=time_point, y=total_shedding, colour = treatment)) + geom_line(aes(group=pignum))
```

---
## Even More Plots

Add facetting

```{r, fig.height=5, fig.width=9}
ggplot(shed, aes(x=time_point, y=total_shedding, color=treatment)) + 
  geom_line(aes(group=pignum)) + 
  facet_wrap(~treatment)
```

---
## Variable Creation and Data Filtering

We will make a new variable in the data: `gain = weight at day 21 - weight at day 0`.

We will then filter the data to only include the final values.

```{r}
final_shed <- shed %>% 
  group_by(pignum) %>% 
  mutate(gain = pig_weight[time_point == 21] - pig_weight[time_point == 0]) %>% filter(time_point == 21) %>% ungroup() %>% select(-c(4:9))

summary(final_shed$gain)
```

---
## Histogram

Lets look distribution of the final shedding values with a histogram

```{r, fig.height=5, fig.width=8}
ggplot(final_shed) + geom_histogram(aes(x = total_shedding))
```

---
## One pig did not shed at all...

which pig and what treatment did it receive?

```{r}
final_shed[which.min(final_shed$total_shedding),]
```

---
## Shedding by Treatment

Look at the average cumulative shedding value for each treatment seperately


```{r}
# Using base `R`:
median(final_shed$total_shedding[final_shed$treatment == "control"])
# then repeat for each treatment. Or ...

```


```{r}
# Using `tidyverse`:
final_shed %>%  group_by(treatment) %>% 
  summarise(med_shed = median(total_shedding))
```
]
---
## Statistical Significance

There is a difference but is it statistically significant?

```{r}
wilcox.test(total_shedding ~ treatment, data = final_shed,
            subset = treatment %in% c("control", "RPS"))
```

---
## Boxplots

We could compare the cumulative shedding values of each treatment group with a side by side boxplot

```{r, fig.height=5, fig.width=9}
ggplot(final_shed) + 
  geom_boxplot( aes(treatment, total_shedding, fill = treatment))
```

---
## Boxplots Again

Alternatively, we could use the original data to compare the daily shedding values of each treatment group with a side by side boxplot over time

```{r, fig.height=4, fig.width=9}
shed %>% filter(time_point != 0) %>% ggplot() + 
  geom_boxplot( aes(treatment, daily_shedding, fill = treatment), position = "dodge")  + 
  facet_wrap(~time_point)
```


---
class: inverse

## Your Turn 

Try playing with chunks of code from this session to further investigate the *Salmonella* shedding data:

1. Get a summary of the daily shedding values (use the `shed` data set)

2. Make side by side boxplots of final weight gain by treatment group (use the `final_shed` data set)

3. Compute a wilcox test for control vs. the "Bglu" treatment group 
