---
title: 'Stat 140: Inference for a Difference in Means'
author: "Evan Ray"
date: "November 27, 2017"
output:
  pdf_document:
    fig_height: 2.8
    fig_width: 6.5
  html_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 3
    fig_width: 5
---


```{r, include=FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaic)
require(tidyr)
require(ggplot2)

# Some customization.  You can alter or delete as desired (if you know what you are doing).

# knitr settings to control how R chunks work.
require(knitr)
opts_chunk$set(
  tidy=FALSE,     # display code as typed
  size="small"    # slightly smaller font for code
)
```

# Jumping Frog Jubilee

## Introduction

Quote from Astley et al., 2013. Chasing maximal performance: a cautionary tale from the celebrated jumping frogs
of Calaveras County. The Journal of Experimental Biology 216, 3947-3953.

> We recorded video of 3124 bullfrog jumps over the course of the 4-day contest at the Calaveras County Jumping Frog Jubilee, and determined jump distance from these images and a calibration of the jump arena. Frogs were divided into two groups: 'rental' frogs collected by fair organizers and jumped by the general public, and frogs collected and jumped by experienced, 'professional' teams.

Read in the data, and subset to non-professional frog-jumpers:

```{r}
frogs <- read.csv("https://mhc-stat140-2017.github.io/data/misc/frogs/frogs.csv")
head(frogs)
frogs_amateur <- frogs %>%
  filter(frog_type %in% c("rental", "individual") & jump_n <= 3 & distance > 0)
```

\newpage

Plot jump distance by jump number:
```{r}
ggplot(frogs_amateur, aes(x = distance, color = factor(jump_n))) +
  geom_density()
```

Plot jump distance by frog type:
```{r}
ggplot(frogs_amateur, aes(x = distance, color = frog_type)) +
  geom_density()
```

## Example 1: Are mean frog jump distances the same for "rental" frogs and for frogs brought to the fair by amateur "individual"s?

### State Null and Alternative Hypotheses

\vspace{6cm}

### Check Assumptions for Two-Sample t test

1. Independence within each group
\vspace{6cm}

2. Independence across groups
\vspace{6cm}

\newpage

3. Nearly normal distribution

To deal with the issue of independence within each group, let's look at just one of the jumps -- how about the second.  Here's a plot of jump distance by frog type for just the second jump:

\vspace{2cm}

```{r}
frogs_amateur_jump_2 <- frogs_amateur %>% filter(jump_n == 2)
ggplot() +
  geom_density(mapping = aes(x = distance, color = frog_type),
    data = frogs_amateur_jump_2)
ggplot() +
  geom_histogram(mapping = aes(x = distance, fill = frog_type),
    bins = 8,
    data = frogs_amateur_jump_2) +
  facet_wrap(~ frog_type)
```

4. Sample size

```{r}
table(frogs_amateur_jump_2$frog_type)
```
\vspace{4cm}

### Calculate a p-value

```{r}
frogs_rental_jump_2 <- filter(frogs_amateur_jump_2, frog_type == "rental")
frogs_individual_jump_2 <- filter(frogs_amateur_jump_2, frog_type == "individual")

t.test(
  frogs_rental_jump_2$distance,
  frogs_individual_jump_2$distance,
  alternative = "two.sided",
  conf.level = 0.95)
```

### Draw a conclusion for the hypothesis test.

\vspace{4cm}

### Find a 95% confidence interval for the difference in means and interpret it in context.

\vspace{4cm}

\newpage

## Example 2: Are mean jump distances the same for the first and second jumps, among frogs brought by individuals?

```{r}
frogs_individual_jumps_1_2 <- frogs_amateur %>%
  filter(frog_type == "individual" & jump_n >= 1 & jump_n <= 2)
ggplot() +
  geom_density(mapping = aes(x = distance, color = factor(jump_n)), data = frogs_individual_jumps_1_2)
```

### State Null and Alternative Hypotheses

\vspace{3cm}

### Check Assumptions for Two-Sample t test

1. Independence within each group
\vspace{1.5cm}

2. Independence across groups
\vspace{1.5cm}

3. Nearly normal distribution
\vspace{1.5cm}

4. Sample size

\newpage

**We can't do a regular two-sample t test because assumptions of independence across groups are violated.  Instead, we can do a "paired t test":**

 * Calculate differences between observed values for each pair
 * Perform a t test of whether the average difference is equal to 0

\vspace{3cm}

```{r}
frogs_individual_wide <- frogs_individual_jumps_1_2 %>%
  select(id, frog_type, distance, jump_n) %>%
  spread(key = jump_n, value = distance, sep = "_distance_") %>%
  mutate(distance_difference = jump_n_distance_2 - jump_n_distance_1)

head(frogs_individual_wide)

ggplot() +
  geom_density(mapping = aes(x = distance_difference), data = frogs_individual_wide)
```

\newpage

### Check Assumptions for *Paired* t-test

1. Differences between paired observations are independent across different pairs
\vspace{1cm}

2. Differences between paired observations follow a nearly normal distribution
\vspace{1cm}

3. Sample size
\vspace{1cm}

### Calculate a p-value (shown two ways, you only have to do one)

```{r}
t.test(
  frogs_individual_wide$jump_n_distance_2,
  frogs_individual_wide$jump_n_distance_1,
  mu = 0,
  alternative = "two.sided",
  paired = TRUE
)

t.test(
  frogs_individual_wide$distance_difference,
  mu = 0,
  alternative = "two.sided"
)
```

### Draw a conclusion for the hypothesis test.

\vspace{4cm}

### Find a 95% confidence interval for the difference in means and interpret it in context.

\vspace{4cm}
