---
title: "Homework 6"
author: "ARNESH TARACHANDANI"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message=FALSE, fig.height = 3)
library(tidyverse)
source("../../scripts/ggprob.R")
library(dplyr)
library(ggplot2)
```


<br/>

### 1

For this question,

- Read in the same `chimpanzee.csv` data file we used for lecture/discussion.
- Make an assumption that there are universal $p_{\text{partner}}$ and $p_{\text{no partner}}$ parameters representing the probability ANY chimpanzee would make a prosocial choice in a single trial with or without a partner.
- Assume that all trials are independent.

Now, suppose we are interested in estimating the *difference* in sample proportions rather than just the individual population proportions. Confidence intervals can be defined for this purpose.

#### 1A

Using the Wald estimates, construct a ***90% confidence*** interval for the difference in the population proportions $p_{\text{partner}} - p_{\text{no partner}}$. (Note: you this proportion is treater as universal for all chimps, summarize your data across ALL chimps to get these estimates.)

```{r}
# insert code here

chimpanzees <- read.csv("../../data/chimpanzee.csv")
```

#### 1B

Interpret the estimated confidence interval in context.


<br/>

### 2-4

For the next several questions, we will be using the Boston Marathon data set again. The following code chunk reads in the *TIM.txt* data file (you may need to adjust the path to the data), and makes the following modifications to the data frame.

The following variables are renamed:
  - `bib` = `BibNum` and changed to a character type
  - `year` = `Year`
  - `age` = `Age`
  - `gender` = `Gender1F2M`, change 1 to F and 2 to M

These variables are removed:  `StartHr`, `StartMin`, `HalfMar`, `Age2014`

Also, the data set is filtered to only include the years 2011 and 2012 (i.e., observations from 2013 are removed).

```{r}
tim = read_table("../../data/TIM.txt", guess_max = 20000) %>%
  rename(bib = BibNum,
         year = Year,
         age = Age,
         gender = Gender1F2M) %>%
  mutate(bib = as.character(bib),
         gender = case_when(
           gender == 1 ~ "F",
           gender == 2 ~ "M"
         )) %>%
  select(-c(StartHr,StartMin,HalfMar, "Age2014")) %>%
  filter(year != 2013)
```



### 2

- Create a new variable called `final` that gives the total time each runner took to run the marathon.  This will involve summing the spit times; the split times are provided in 5 KM intervals starting with `K0-5` through `K35-40`, and the final interval, `K40-Fin`, which is 2.2 KM.
- Add a variable `speed` that has two possibles values,"Fast" or "Slow", depending on if the runner finished the marathon in under 3.5 hours or greater than or equal to 3.5 hours, respectively.
- Print out a table that lists the total number of runners by gender and speed.  There should be one row for Female and Fast, Female and Slow, Male and Fast, and Male and Slow.

``````{r}
# insert code here
```


### 3

- Display a plot that has a point of `size=2` at the sample mean of the final time for each of the four combinations of `gender` and `speed`.
- Facet the graphic into two plots based on the category of `speed` ("Fast" or "Slow").
- Add a blue (vertical) line segment to visualize a ***90% confidence interval*** for the mean final time for each gender and speed category. The lines should extend vertically from the lower bound to the upper bound of the confidence interval, and the point estimate from the previous step will be in the middle of the interval.
-  Label axes appropriately and add an informative title to the plot.

Hint: Your final plot should have four vertical segments for the confidence intervals, a point in the center of each vertical line, and a horizontal dashed line.

``````{r}
# insert code here
```

### 4

- Consider only the runners with `speed` = "Slow".
- Assume that the final time for each runner is independent.
- Test the hypothesis that $\mu_F = \mu_M$ versus the two-sided alternative, where $\mu_F$ is the average final time for ("Slow") Female runners and $\mu_M$ is the average final time for ("Slow") Male runners.


#### 4A

What is the value of the observed test statistic?
``````{r}
# insert code here
```

#### 4B

What is the distribution of the test statistic assuming the null hypothesis is true? State the name of the distribution and the values of its parameter(s).
``````{r}
# insert code here
```


#### 4C

What is your computed p-value?
``````{r}
# insert code here
```

#### 4D

Interpret the result of the hypothesis test in context.
``````{r}
# insert code here
```

Below is chimpanzee.csv
actor	partner	prosocial	selfish	prosocial_color	selfish_color
A	I	22	8	green	purple
A	G	16	14	yellow	black
A	H	22	8	blue	red
A	none	16	14	pink	gray
B	H	18	12	black	yellow
B	C	23	7	purple	green
B	D	19	11	blue	red
B	none	16	14	gray	pink
C	B	18	12	yellow	black
C	F	19	11	red	blue
C	E	20	10	green	purple
C	none	17	13	gray	pink
D	C	19	11	black	yellow
D	B	15	15	green	purple
D	I	16	14	blue	red
D	none	8	22	pink	gray
E	C	14	16	purple	green
E	I	14	16	blue	red
E	J	20	10	black	yellow
E	none	12	18	pink	gray
F	I	18	12	green	purple
F	A	18	12	red	blue
F	D	11	19	black	yellow
F	none	14	16	gray	pink
G	J	16	14	red	blue
G	F	15	15	black	yellow
G	A	6	4	purple	green