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() )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
2
To analyze the Boston Marathon data according to the specified transformations and calculations:
# Loading necessary libraries library(tidyverse) # Reading in the data and filtering 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")) # Printing the table of total number of runners by gender and speed table(tim$gender, tim$speed)3
Creating the plot with sample means and 90% confidence intervals:
# Summarize 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) # Plotting ggplot(summary_stats, aes(x = gender, y = mean_final)) + geom_point(size = 2) + facet_wrap(~speed) + geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2, color = "blue") + labs(title = "Mean Final Time by Gender and Speed with 90% CI", x = "Gender", y = "Mean Final Time (minutes)") + theme_minimal()4
Considering only the runners with
speed= "Slow":4A
Calculate the observed test statistic for the hypothesis :
# Subset the data for Slow runners slow_runners <- tim %>% filter(speed == "Slow") # Group by gender and calculate summarized statistics slow_summary <- slow_runners %>% group_by(gender) %>% summarise(mean_final = mean(final), sd_final = sd(final), n = n()) # Mean and standard deviations meanF <- slow_summary$mean_final[slow_summary$gender == "F"] sdF <- slow_summary$sd_final[slow_summary$gender == "F"] nF <- slow_summary$n[slow_summary$gender == "F"] meanM <- slow_summary$mean_final[slow_summary$gender == "M"] sdM <- slow_summary$sd_final[slow_summary$gender == "M"] nM <- slow_summary$n[slow_summary$gender == "M"] # Calculate the test statistic t_value <- (meanF - meanM) / sqrt((sdF^2/nF) + (sdM^2/nM)) t_value4B
State the distribution of the test statistic assuming null hypothesis is true:
# The test statistic follows a t-distribution with degrees of freedom df <- ((sdF^2/nF + sdM^2/nM)^2) / ((sdF^2/nF)^2 / (nF - 1) + (sdM^2/nM)^2 / (nM - 1)) df4C
Compute the p-value:
p_value <- 2 * pt(-abs(t_value), df) p_value4D
Interpretation of the result in context:
# Interpretation of the hypothesis test if (p_value < 0.05) { "We reject the null hypothesis at the 5% significance level. This suggests that there is a significant difference in average final times between slow male and female runners." } else { "We fail to reject the null hypothesis at the 5% significance level. This suggests that there is not a significant difference in average final times between slow male and female runners." }These steps will give you the necessary analysis and visualization for the Boston Marathon data with details on summing split times, creating new variables, and conducting hypothesis testing.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.