Certainly! Below is an RMarkdown template addressing the tasks you've outlined for questions 1 through 4.
Replace "../../data/chimpanzee.csv" and "../../data/TIM.txt" with the correct paths to your data files if necessary.
---
title: "Chimpanzee Behavior and Boston Marathon Analysis"
author: "Your Name"
date: "Today’s Date"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(readr)
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: this proportion is treated as universal for all chimps, summarize your data across ALL chimps to get these estimates.)
# Read in the dataset
chimpanzees <- read.csv("../../data/chimpanzee.csv")
# Compute the totals and sample proportions
total_trials_partner <- sum(chimpanzees$partner != "none")
total_trials_no_partner <- sum(chimpanzees$partner == "none")
prosocial_partner <- sum(chimpanzees$prosocial[chimpanzees$partner != "none"])
prosocial_no_partner <- sum(chimpanzees$prosocial[chimpanzees$partner == "none"])
# Sample proportions
p_partner <- prosocial_partner / total_trials_partner
p_no_partner <- prosocial_no_partner / total_trials_no_partner
# Difference in proportions
diff_proportion <- p_partner - p_no_partner
# Standard error
se_diff <- sqrt((p_partner * (1 - p_partner) / total_trials_partner) +
(p_no_partner * (1 - p_no_partner) / total_trials_no_partner))
# 90% confidence interval
z <- qnorm(0.95)
ci_lower <- diff_proportion - z * se_diff
ci_upper <- diff_proportion + z * se_diff
ci_diff_proportions <- c(ci_lower, ci_upper)
ci_diff_proportions
1B
Interpret the estimated confidence interval in context.
The 90% confidence interval for the difference in proportions ( p_{\text{partner}} - p_{\text{no partner}} ) is (r round(ci_diff_proportions[1], 4)) to (r round(ci_diff_proportions[2], 4)). This interval means that we are 90% confident that the true difference in proportions of prosocial choices made with a partner versus without a partner lies within this range.
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, 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).
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 %in% c(2011, 2012))
2
- Create a new variable called
final that gives the total time each runner took to run the marathon. This will involve summing the split 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 possible 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.
# Create the final time variable
tim <- tim %>%
mutate(final = rowSums(select(., starts_with('K'))) + `K40-Fin`,
speed = if_else(final < 3.5 * 60, "Fast", "Slow"))
# Print the table
table_gender_speed <- tim %>%
group_by(gender, speed) %>%
summarise(count = n()) %>%
ungroup()
# Display the table
table_gender_speed
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.
# Calculate means and confidence intervals
mean_ci <- tim %>%
group_by(gender, speed) %>%
summarise(mean_final = mean(final, na.rm = TRUE),
se_final = sd(final, na.rm = TRUE) / sqrt(n())) %>%
mutate(ci_lower = mean_final - qt(0.95, df = n() - 1) * se_final,
ci_upper = mean_final + qt(0.95, df = n() - 1) * se_final)
# Plot
ggplot(mean_ci, aes(x = gender, y = mean_final, color = gender)) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2, color = "blue") +
facet_wrap(~ speed) +
labs(title = "Mean Final Time with 90% Confidence Intervals by Gender and Speed",
x = "Gender",
y = "Mean Final Time (minutes)") +
theme_minimal()
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?
# Filter the data for 'Slow' runners
slow_runners <- tim %>%
filter(speed == "Slow")
# Calculate the test statistic
t_test <- t.test(final ~ gender, data = slow_runners)
# Extracting the observed test statistic
t_statistic <- t_test$statistic
t_statistic
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).
The distribution of the test statistic under the null hypothesis is the Student's t-distribution with degrees of freedom approximated by Satterthwaite's method:
df <- t_test$parameter
df
4C
What is your computed p-value?
p_value <- t_test$p.value
p_value
4D
Interpret the result of the hypothesis test in context.
if(p_value < 0.05) {
result_interpretation <- "We reject the null hypothesis. There is sufficient evidence to suggest that the average final time for 'Slow' Female runners is different from that of 'Slow' Male runners."
} else {
result_interpretation <- "We fail to reject the null hypothesis. There is not enough evidence to suggest that the average final time for 'Slow' Female runners differs from that of 'Slow' Male runners."
}
result_interpretation
This RMarkdown template covers all the steps mentioned in your prompt, including loading data, performing statistical calculations, creating visualizations, and interpreting the results. You can further customize it as needed.