1
1A
To construct a 90% confidence interval for ( p_{\text{partner}} - p_{\text{no partner}} ):
-
Calculate the proportions:
( p_{\text{partner}} ) and ( p_{\text{no partner}} ) are obtained by summarizing the prosocial choices with and without a partner.
-
Compute the Standard Error:
Standard errors for the difference in proportions can be computed given the data.
Let's start by reading the data and computing the required proportions.
# Read the data
chimpanzees <- read.csv("../../data/chimpanzee.csv")
# Summarize the data
summary_data <- chimpanzees %>%
mutate(total_trials = prosocial + selfish) %>%
group_by(partner) %>%
summarise(
prosocial_choices = sum(prosocial),
total_trials = sum(total_trials)
)
# Compute the proportions
p_partner <- summary_data %>%
filter(partner != "none") %>%
summarise(proportion = sum(prosocial_choices) / sum(total_trials)) %>%
pull(proportion)
p_no_partner <- summary_data %>%
filter(partner == "none") %>%
summarise(proportion = sum(prosocial_choices) / sum(total_trials)) %>%
pull(proportion)
# Number of trials
n_partner <- summary_data %>%
filter(partner != "none") %>%
summarise(total = sum(total_trials)) %>%
pull(total)
n_no_partner <- summary_data %>%
filter(partner == "none") %>%
summarise(total = sum(total_trials)) %>%
pull(total)
# Compute the difference in proportions
prop_diff <- p_partner - p_no_partner
# Compute the Standard Error
se_diff <- sqrt((p_partner * (1 - p_partner) / n_partner) + (p_no_partner * (1 - p_no_partner) / n_no_partner))
# Compute the 90% CI
z_star <- qnorm(0.95) # z* value for 90% confidence level
ci_lower <- prop_diff - z_star * se_diff
ci_upper <- prop_diff + z_star * se_diff
ci <- c(ci_lower, ci_upper)
ci
1B
The 90% confidence interval for the difference in proportions ( p_{\text{partner}} - p_{\text{no partner}} ) indicates the range within which we can be 90% confident that the true difference in the proportion of prosocial choices between trials with a partner and trials without a partner lies. If the interval does not include 0, it suggests a significant difference between the two scenarios.
2
To create the new variables and print out the table of total runners by gender and 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)
# Calculate the total time and speed
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*3600, "Fast", "Slow"))
# Summarize the data
summary_table <- tim %>%
group_by(gender, speed) %>%
summarise(count = n())
summary_table
3
To display the plot with the sample means and 90% confidence intervals for each gender and speed category:
# Summarize the data for plot
plot_data <- tim %>%
group_by(gender, speed) %>%
summarise(mean_final = mean(final),
sd_final = sd(final),
n = n(),
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)
# Create the plot
ggplot(plot_data, 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 by Gender and Speed",
x="Gender",
y="Mean Final Time (seconds)")
4
4A
To test the hypothesis for the slow runners and calculate the test statistic:
slow_runners <- tim %>%
filter(speed == "Slow")
mu_F <- mean(slow_runners %>% filter(gender == "F") %>% pull(final))
mu_M <- mean(slow_runners %>% filter(gender == "M") %>% pull(final))
sd_F <- sd(slow_runners %>% filter(gender == "F") %>% pull(final))
sd_M <- sd(slow_runners %>% filter(gender == "M") %>% pull(final))
n_F <- slow_runners %>%
filter(gender == "F") %>%
summarise(count = n()) %>%
pull(count)
n_M <- slow_runners %>%
filter(gender == "M") %>%
summarise(count = n()) %>%
pull(count)
# Calculate the test statistic
test_stat <- (mu_F - mu_M) / sqrt((sd_F^2 / n_F) + (sd_M^2 / n_M))
test_stat
4B
The distribution of the test statistic under the null hypothesis is a t-distribution with degrees of freedom calculated using the Welch-Satterthwaite equation.
# Calculate degrees of freedom
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
The test statistic follows a t-distribution with the calculated degrees of freedom.
4C
To compute the p-value:
# Compute the p-value
p_value <- 2 * pt(-abs(test_stat), df=df)
p_value
4D
Interpretation of the hypothesis test results:
The p-value indicates the probability of observing a difference as extreme as, or more extreme than, the observed difference in mean final times between "Slow" Female and Male runners under the null hypothesis that the true means are equal. If the p-value is less than the chosen significance level (commonly 0.05), we reject the null hypothesis and conclude that there is a significant difference in the mean final times between "Slow" Female and Male runners. Otherwise, we fail to reject the null hypothesis.
# Interpretation of hypothesis test
if (p_value < 0.05) {
interpretation <- "There is sufficient evidence to reject the null hypothesis, suggesting a significant difference in the mean final times between 'Slow' Female and Male runners."
} else {
interpretation <- "There is insufficient evidence to reject the null hypothesis, suggesting no significant difference in the mean final times between 'Slow' Female and Male runners."
}
interpretation