---
title: "Week 10 Lecture 2 -- Hypothesis Testing"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(Sleuth3)
```

### Hypothesis testing using the t-test

First, we'll read in the Finches data

```{r}
finches <- case0201
```

We first visualize the data

```{r}
finches$Year <- as.character(finches$Year)
ggplot(finches) + geom_bar(mapping = aes(x = Depth, y = ..prop..,fill = Year), alpha = 0.5, position = "dodge") 
ggplot(finches) + geom_density(mapping = aes(x = Depth,fill = Year), alpha = 0.5)
ggplot(finches) + geom_boxplot(mapping = aes(x = Year, y = Depth))

```


Since we are satisfied that the model assumptions (that the two distributions in 1976 and 1978 are close to normal) are satisfied, we can proceed to compute the p-value


```{r}
a <- t.test(filter(finches, Year == "1976")$Depth, filter(finches, Year == "1978")$Depth, alternative = "two.sided", var.equal = FALSE)
a$p.value
```

If we set `alternative` to `"one.sided"`, the p-value would be computed by only considering values that are more extreme and positive (if the difference is positive in the first place).

Note that we could have used the formula we saw before and `pt` in order to compute this value.
