### 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)

```
── Column specification ──────────────────────────────────────────
cols(
  BibNum = col_double(),
  Year = col_double(),
  Age = col_double(),
  Gender1F2M = col_double(),
  StartHr = col_double(),
  StartMin = col_double(),
  `K0-5` = col_double(),
  `K5-10` = col_double(),
  `K10-15` = col_double(),
  `K15-20` = col_double(),
  `K20-25` = col_double(),
  `K25-30` = col_double(),
  `K30-35` = col_double(),
  `K35-40` = col_double(),
  `K40-Fin` = col_double(),
  HalfMar = col_double(),
  Age2014 = col_double()
)


R version 4.4.1 (2024-06-14 ucrt) -- "Race for Your Life"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> knitr::opts_chunk$set(echo = TRUE, message=FALSE, fig.height = 3)
> library(tidyverse)
── Attaching core tidyverse packages ────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ──────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package to force all conflicts to become errors
> source("../../scripts/ggprob.R")
> knitr::opts_chunk$set(echo = TRUE, message=FALSE, fig.height = 3)
> library(tidyverse)
> source("../../scripts/ggprob.R")
> library(dplyr)
> library(ggplot2)
> # insert code here
> 
> chimpanzees <- read.csv("../../data/chimpanzee.csv")
> # Summarize prosocial choices with and without partner
> with_partner <- sum(chimpanzees$prosocial[chimpanzees$partner != "none"])
> without_partner <- sum(chimpanzees$prosocial[chimpanzees$partner == "none"])
> 
> # Summarize total trials with and without partner
> total_with_partner <- sum(chimpanzees$prosocial[chimpanzees$partner != "none"] + chimpanzees$selfish[chimpanzees$partner != "none"])
> total_without_partner <- sum(chimpanzees$prosocial[chimpanzees$partner == "none"] + chimpanzees$selfish[chimpanzees$partner == "none"])
> 
> # Calculate sample proportions
> p_partner <- with_partner / total_with_partner
> p_no_partner <- without_partner / total_without_partner
> 
> # Calculate standard error
> se_diff <- sqrt((p_partner * (1 - p_partner) / total_with_partner) + (p_no_partner * (1 - p_no_partner) / total_without_partner))
> 
> # Calculate the 90% confidence interval
> z <- qnorm(0.95)
> diff <- p_partner - p_no_partner
> ci_lower <- diff - z * se_diff
> ci_upper <- diff + z * se_diff
> 
> ci <- c(ci_lower, ci_upper)
> ci
[1] 0.05806624 0.19676071
> 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)

── Column specification ──────────────────────────────────────────
cols(
  BibNum = col_double(),
  Year = col_double(),
  Age = col_double(),
  Gender1F2M = col_double(),
  StartHr = col_double(),
  StartMin = col_double(),
  `K0-5` = col_double(),
  `K5-10` = col_double(),
  `K10-15` = col_double(),
  `K15-20` = col_double(),
  `K20-25` = col_double(),
  `K25-30` = col_double(),
  `K30-35` = col_double(),
  `K35-40` = col_double(),
  `K40-Fin` = col_double(),
  HalfMar = col_double(),
  Age2014 = col_double()
)

> library(dplyr)
> library(readr)
> 
> 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)

── Column specification ──────────────────────────────────────────
cols(
  BibNum = col_double(),
  Year = col_double(),
  Age = col_double(),
  Gender1F2M = col_double(),
  StartHr = col_double(),
  StartMin = col_double(),
  `K0-5` = col_double(),
  `K5-10` = col_double(),
  `K10-15` = col_double(),
  `K15-20` = col_double(),
  `K20-25` = col_double(),
  `K25-30` = col_double(),
  `K30-35` = col_double(),
  `K35-40` = col_double(),
  `K40-Fin` = col_double(),
  HalfMar = col_double(),
  Age2014 = col_double()
)

> 
> tim <- tim %>%
+   mutate(final = K0_5 + K5_10 + K10_15 + K15_20 + K20_25 + K25_30 + K30_35 + K35_40 + K40_Fin,
+          speed = ifelse(final < 3.5*60, "Fast", "Slow"))
Error in `mutate()`:
ℹ In argument: `final = +...`.
Caused by error:
! object 'K0_5' not found
Backtrace:
 1. tim %>% ...
 3. dplyr:::mutate.data.frame(...)
 4. dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
 6. dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
 7. mask$eval_all_mutate(quo)
 8. dplyr (local) eval()
> # insert code here
> library(ggplot2)
> 
> # Calculate means and confidence intervals
> summary_stats <- tim %>%
+   group_by(gender, speed) %>%
+   summarise(mean_final = mean(final),
+             n = n(),
+             sd_final = sd(final),
+             se_final = sd_final / sqrt(n),
+             ci_lower = mean_final - qt(0.95, df = n - 1) * se_final,
+             ci_upper = mean_final + qt(0.95, df = n - 1) * se_final)
Error in `group_by()`:
! Must group by variables found in `.data`.
✖ Column `speed` is not found.
Backtrace:
 1. tim %>% group_by(gender, speed) %>% ...
 4. dplyr:::group_by.data.frame(., gender, speed)
> 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)

── Column specification ──────────────────────────────────────────
cols(
  BibNum = col_double(),
  Year = col_double(),
  Age = col_double(),
  Gender1F2M = col_double(),
  StartHr = col_double(),
  StartMin = col_double(),
  `K0-5` = col_double(),
  `K5-10` = col_double(),
  `K10-15` = col_double(),
  `K15-20` = col_double(),
  `K20-25` = col_double(),
  `K25-30` = col_double(),
  `K30-35` = col_double(),
  `K35-40` = col_double(),
  `K40-Fin` = col_double(),
  HalfMar = col_double(),
  Age2014 = col_double()
)

> 
> tim <- tim %>%
+   mutate(final = K0_5 + K5_10 + K10_15 + K15_20 + K20_25 + K25_30 + K30_35 + K35_40 + K40_Fin,
+          speed = ifelse(final < 3.5*60, "Fast", "Slow"))
Error in `mutate()`:
ℹ In argument: `final = +...`.
Caused by error:
! object 'K0_5' not found
Backtrace:
 1. tim %>% ...
 3. dplyr:::mutate.data.frame(...)
 4. dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
 6. dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
 7. mask$eval_all_mutate(quo)
 8. dplyr (local) eval()
> 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)

── Column specification ──────────────────────────────────────────
cols(
  BibNum = col_double(),
  Year = col_double(),
  Age = col_double(),
  Gender1F2M = col_double(),
  StartHr = col_double(),
  StartMin = col_double(),
  `K0-5` = col_double(),
  `K5-10` = col_double(),
  `K10-15` = col_double(),
  `K15-20` = col_double(),
  `K20-25` = col_double(),
  `K25-30` = col_double(),
  `K30-35` = col_double(),
  `K35-40` = col_double(),
  `K40-Fin` = col_double(),
  HalfMar = col_double(),
  Age2014 = col_double()
)
### 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
```