Let's walk through the homework step-by-step.
Question 1: Chimpanzee Data Analysis
Firstly, we'll read in the chimpanzee.csv data and calculate the confidence interval for the difference in population proportions p_partner - p_no partner.
# Read the chimpanzee data
chimpanzees <- read.csv("../../data/chimpanzee.csv")
# Summarize data for all chimps to get prosocial choices with and without partners
chimp_summary <- chimpanzees %>%
group_by(partner) %>%
summarize(
prosocial = sum(prosocial, na.rm = TRUE),
total = sum(prosocial + selfish, na.rm = TRUE)
)
# Extract prosocial counts and total trials for 'partner' and 'none'
with_partner = chimp_summary %>% filter(partner != 'none')
no_partner = chimp_summary %>% filter(partner == 'none')
# Calculate sample proportions
p_partner = sum(with_partner$prosocial) / sum(with_partner$total)
p_no_partner = sum(no_partner$prosocial) / sum(no_partner$total)
# Calculate estimated difference in proportions
diff_prop = p_partner - p_no_partner
# Calculate standard error for the difference in proportions
se_diff = sqrt((p_partner * (1 - p_partner) / sum(with_partner$total)) +
(p_no_partner * (1 - p_no_partner) / sum(no_partner$total)))
# 90% Confidence Interval
z = qnorm(0.95)
margin_of_error = z * se_diff
ci_lower = diff_prop - margin_of_error
ci_upper = diff_prop + margin_of_error
ci_lower
ci_upper
1B: Interpretation
The 90% confidence interval for the difference in population proportions p_partner - p_no_partner indicates that we are 90% confident that the true difference in the probability of making a prosocial choice with a partner compared to without a partner lies within this interval.
Question 2-4: Boston Marathon Data Analysis
Next, we will load and process the Boston Marathon data.
# Read the Boston Marathon data
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)
# Create the 'final' variable
tim <- tim %>%
mutate(final = rowSums(select(., starts_with('K'))))
# Create the 'speed' variable
tim <- tim %>%
mutate(speed = ifelse(final < 3.5 * 60 * 60, "Fast", "Slow"))
# Print the table
table(tim$gender, tim$speed)
Question 3: Plotting the Mean Final Time with Confidence Intervals
# Calculate mean and confidence interval for each combination of gender and speed
summary_stats <- tim %>%
group_by(gender, speed) %>%
summarize(
mean_final = mean(final),
se = sd(final) / sqrt(n())
) %>%
mutate(
ci_lower = mean_final - qnorm(0.95) * se,
ci_upper = mean_final + qnorm(0.95) * se
)
# Plotting the data
ggplot(summary_stats, aes(x = gender, y = mean_final)) +
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 for Marathon Runners",
x = "Gender",
y = "Mean Final Time (seconds)"
)
Question 4: Hypothesis Testing for Slow Runners
4A: Observed Test Statistic
# Subset tim data for slow runners
tim_slow <- tim %>% filter(speed == "Slow")
# Calculate the mean and standard deviation for Female and Male slow runners
mu_F <- mean(tim_slow$final[tim_slow$gender == "F"])
mu_M <- mean(tim_slow$final[tim_slow$gender == "M"])
sd_F <- sd(tim_slow$final[tim_slow$gender == "F"])
sd_M <- sd(tim_slow$final[tim_slow$gender == "M"])
n_F <- length(tim_slow$final[tim_slow$gender == "F"])
n_M <- length(tim_slow$final[tim_slow$gender == "M"])
# Calculate the test statistic for difference in means
test_stat = (mu_F - mu_M) / sqrt((sd_F^2 / n_F) + (sd_M^2 / n_M))
test_stat
4B: Distribution of the Test Statistic
Under the null hypothesis, the test statistic follows a t-distribution with the degrees of freedom calculated as follows:
df = ((sd_F^2 / n_F + sd_M^2 / n_M)^2) /
((sd_F^2 / n_F)^2 / (n_F - 1) + (sd_M^2 / n_M)^2 / (n_M - 1))
df
4C: Computed p-value
# Compute the p-value for a two-sided test
p_value = 2 * pt(-abs(test_stat), df)
p_value
4D: Interpretation
The p-value indicates the probability of observing a test statistic at least as extreme as the one calculated, assuming the null hypothesis is true. If the p-value is less than the significance level (usually 0.05), we reject the null hypothesis and conclude that there is a significant difference in mean final times between Female and Male slow runners.
Using the code above, you can deduce the p-value and make an appropriate conclusion based on the p-value obtained.