2-4 For the next several questions, we will be using the Boston Marathon data set again.…
- User
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=BibNumand changed to a character typeyear=Yearage=Agegender=Gender1F2M, change 1 to F and 2 to M
These variables are removed:
StartHr,StartMin,HalfMar,Age2014Also, 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 != 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() )Show in New Window Error in
group_by(): ! Must group by variables found in.data. ✖ Columnspeedis not found. Backtrace:- tim %>% group_by(gender, speed) %>% ...
- dplyr:::group_by.data.frame(., gender, speed)
Error in group_by(., gender, speed) :
✖ Column
speedis not found.
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_chunkset(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(chimpanzeespartner != "none"]) without_partner <- sum(chimpanzeespartner == "none"])
Summarize total trials with and without partner
total_with_partner <- sum(chimpanzeespartner != "none"] + chimpanzeespartner != "none"]) total_without_partner <- sum(chimpanzeespartner == "none"] + chimpanzeespartner == "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:- tim %>% ...
- dplyr:::mutate.data.frame(...)
- dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
- dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
- mask$eval_all_mutate(quo)
- 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. ✖ Columnspeedis not found. Backtrace:- tim %>% group_by(gender, speed) %>% ...
- 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:- tim %>% ...
- dplyr:::mutate.data.frame(...)
- dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
- dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
- mask$eval_all_mutate(quo)
- 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
finalthat 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 withK0-5throughK35-40, and the final interval,K40-Fin, which is 2.2 KM. - Add a variable
speedthat 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.
# 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 ``` - Assistant
Sure, let's tackle each part of your request regarding the Boston Marathon dataset analysis one step at a time.
2
First, we will create the new variables
finalandspeed, then print out a table that lists the total number of runners by gender and speed.# Load libraries library(tidyverse) # Read the data and make modifications as specified 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) %>% 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") ) # Print out the table showing the number of runners by gender and speed count_table <- tim %>% count(gender, speed) print(count_table)3
Display a plot showing the sample mean of the final time for each of the four combinations of
genderandspeed, along with the 90% confidence intervals.# Load ggplot2 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 ) # Create the plot with facets ggplot(summary_stats, aes(x = gender, y = mean_final)) + geom_point(size = 2, color = "blue") + geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2, color = "blue") + facet_wrap(~speed) + labs( title = "Sample Mean and 90% Confidence Interval of Final Marathon Time by Gender and Speed", x = "Gender", y = "Mean Final Time (minutes)" ) + theme_minimal()4
4A
Calculate the observed test statistic for the hypothesis that (\mu_F = \mu_M) for "Slow" runners.
# Filter the data for Slow runners only slow_runners <- tim %>% filter(speed == "Slow") # Calculate the mean and standard deviation for each gender stats_slow <- slow_runners %>% group_by(gender) %>% summarise( mean_final = mean(final), sd_final = sd(final), n = n() ) mean_female <- stats_slow$mean_final[stats_slow$gender == "F"] mean_male <- stats_slow$mean_final[stats_slow$gender == "M"] sd_female <- stats_slow$sd_final[stats_slow$gender == "F"] sd_male <- stats_slow$sd_final[stats_slow$gender == "M"] n_female <- stats_slow$n[stats_slow$gender == "F"] n_male <- stats_slow$n[stats_slow$gender == "M"] # Calculate the observed test statistic t_obs <- (mean_female - mean_male) / sqrt((sd_female^2 / n_female) + (sd_male^2 / n_male)) t_obs4B
State the distribution of the test statistic under the null hypothesis.
# The test statistic follows a t-distribution with # degrees of freedom calculated using the Welch-Satterthwaite equation. df <- ((sd_female^2 / n_female) + (sd_male^2 / n_male))^2 / (((sd_female^2 / n_female)^2 / (n_female - 1)) + ((sd_male^2 / n_male)^2 / (n_male - 1))) df4C
Compute the p-value for the test.
# Compute the p-value p_value <- 2 * pt(-abs(t_obs), df = df) p_value4D
Interpret the result of the hypothesis test in context.
# Interpretation of the hypothesis test if (p_value < 0.05) { cat("The p-value is less than 0.05, which means we reject the null hypothesis. There is significant evidence to suggest that the mean final times for 'Slow' female and male runners are different.") } else { cat("The p-value is greater than 0.05, which means we fail to reject the null hypothesis. There is no significant evidence to suggest that the mean final times for 'Slow' female and male runners are different.") }This code sequence should provide the necessary analyses and visualizations for the dataset and the hypothesis tests you described. Make sure to check your dataset paths and other configurations to ensure the code runs smoothly on your setup.