For project 2, I will be working on a dataset that contains the results of the Boston Marathon for 2024. This data was compiled from the Boston Athletics Association website for searching individuals’ results and uploaded onto Kraggle. According to the person who uploaded the dataset, only runners with United States zipcodes, so international runners are excluded.
This dataset has ten variables; two are categorical and eight are quantitative. The categorical variables are Age Group and Gender. The quantitative variables are Bib, Zip, Age, First Half, Second Half, Finish, Positive Split, and Percent Change. For project 2, I only plan to use Bib, Age Group, Gender, First Half, Second Half, and Finish.
I wanted to do some research on marathons because running seems to be a popular hobby among my age group (25-35), and many of my friends are being “selected” to run marathons. Before this project, the only thing I know about marathons is the distance of one (26 miles). So I am curious about the amount of time it takes to finish a marathon, and if this differs between genders. I am also curious about the average race time between age groups.
Access Library Packages and load dataset
# load packageslibrary(tidyverse)
Warning: package 'tidyverse' was built under R version 4.6.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)library(ggthemes)library(RColorBrewer)# load the datasetathletes <- readr::read_csv("Athletes.csv")
Rows: 17220 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): Zip, Age Group, Gender
dbl (7): Bib, Age, First Half, Second Half, Finish, Positive Split, Percent ...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# A tibble: 6 × 10
Bib Zip Age `Age Group` Gender `First Half` `Second Half` Finish
<dbl> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
1 25 93730 30 Under 35 M 3832 3961 7793
2 9 97124 32 Under 35 M 3845 4059 7904
3 20 80922 40 40-44 M 3795 4157 7952
4 46 02136 38 35-39 M 3997 4065 8062
5 51 06119 26 Under 35 M 3979 4198 8177
6 36 90291 23 Under 35 M 3973 4215 8188
# ℹ 2 more variables: `Positive Split` <dbl>, `Percent Change` <dbl>
#view(athletes)
Upon inspecting the data, there are no missing values in any columns and each of the columns have the appropriate data type for the variable. The variables have titles that are relatively easy to use for coding, but nonetheless, I will change them so that they are all lowercase and have no spaces. There is not too much to clean. However, I will not be using half of these columns so I must filter the data to only have the columns I plan to use. I only plan to use bib, age group, gender, first half, second half, and finish for the charts I want to create.
Clean and Filter the data
# remove spaces in column names and make all lowercasenames(athletes)<-gsub(" ","",names(athletes))names(athletes)<-tolower(names(athletes))names(athletes) #check my work
There are over 5000 runners under the age of 35, and another 2000+ in the 35-39 age range. This is not surprising to me at all because many of my own friends have taken up the hobby of running.
# simple graph of finish time by age groupathletes_agegroup <- athletes2 |>group_by(agegroup) |>summarize(avgfinish =mean(finish, na.rm =TRUE))athletes_agegroup$agegroup <-factor( athletes_agegroup$agegroup, levels =c("Under 35", "35-39", "40-44", "45-49","50-54","55-59","60-64","65-69","70-74","75-79","80 and Over"))athletes_agegroup
ggplot(athletes_agegroup, aes(x=agegroup, y=avgfinish)) +geom_col() +labs(x="Age Group",y="Finish Time (in seconds)",title="Average Marathon Finish Time by Age Group",caption="Boston Athletics Association, 2024" )
For this graph, I just wanted to check if the different age groups had different average finish times. It seems that the finish time increases as a runner’s age increases, however the Under 35 group took a bit more time to finish than the 35-39 age group. Also, the 80 and Over age group has a lower average finish time than the 75-79 age group.
Chart 1 - Correlation between Bib Number, Finish Time, and Age Group
For my first chart, I want to see if there’s a correlation between a runner’s bib number and their finish time. To do this, I will make a scatterplot that has the bib number as the x-axis and the finish time as the y-axis. I also want to see if there’s a significant difference of finish time for age groups, for which I plan to make the dots different colors by age group.
# calculate the regression modelmarathon_model <-lm(finish ~ agegroup + bib, data = athletes)summary(marathon_model)
# make sure the age groups are in the right orderathletes2$agegroup <-factor( athletes2$agegroup, levels =c("Under 35", "35-39", "40-44", "45-49","50-54","55-59","60-64","65-69","70-74","75-79","80 and Over"))ggplot(athletes2, aes(x=bib, y=finish)) +geom_point(aes(color = agegroup)) +geom_smooth() +labs(x="Bib Number",y="Finish Time (in seconds)",title="Bib Number vs Finish Time",caption="Boston Athletics Association, 2024") +scale_color_brewer(name="Age Group", palette ="Paired") +theme_clean()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
There does seem to be a positive correlation between bib number and finish time. The higher the bib number they receive, the longer it takes for a runner to finish a marathon. However, there are a good amount of outliers, especially around the 8000 and 16000 marks because there is a higher concentration of people that seemed to take longer to finish. In terms of age group, there is a huge spread for almost all of the age groups except for the 60s and 70s age runners, who have a bib numbers in the mid-20,000s and a concentrated finish time between 15,000 and 20,000 seconds. It is also surprising how large the spread is for the Under 35 age group because the bib numbers are both on the lowest end and the highest end, with them having the largest spread for finish time as well.
This conclusion is also supported by the regression model whereby the bib number and finish time correlates with the runner’s age for the most part but it doesn’t have much correlation for the age group of Under 35 because of the large spread.
Chart 2 - Pacing Between the First Half and Second Half
For my second chart, I want to see the difference between how runners do in the first half of the marathon versus the second half. Do they tend to keep the same pace or does it take them longer to finish after the first half? In addition to this, I want to investigate if there is a difference between the pacing between genders.
# count of runners between gendersathletes2 |>count(gender)
# A tibble: 2 × 2
gender n
<chr> <int>
1 F 8122
2 M 9098
ggplot(athletes2, aes(x=gender))+geom_bar()
There are almost 1000 more male runners than female runners
# scatterplot of first half vs second half with regression lineggplot(athletes2, aes(x=firsthalf, y=secondhalf))+geom_point(aes(color=gender)) +geom_smooth(color="green")+geom_abline(lty=2,lwd=.5)+scale_x_continuous(limits =c(3000, 18000)) +scale_y_continuous(limits =c(3000, 18000)) +coord_fixed(ratio=1)+labs(x="First Half (in seconds)",y="Second Half (in seconds)",color="Gender",title="Pacing for First Half vs Second Half",caption="Boston Athletics Association, 2024") +scale_color_brewer(palette ="Set1")+theme_calc()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Among both genders, the runners tend to take longer in the second half compared to the first half, as shown by the majority of the runners being above the 1:1 ratio line. It also seems that the male population has a higher range of difference between the first and second half because the blue is a lot more scattered throughout the graph compared to the female population. There are a couple outliers (both male) such as the person who took around 10000 seconds to finish the first half but only around 6000 seconds to finish the second half, and also someone who took 7500 seconds to finish the first half and about 17500 seconds to finish the second half.
Conclusion
I learned a lot more about marathons and marathon runners after the analysis of this dataset. I learned that there is a positive correlation between a runner’s bib number and how fast it takes them to finish the race, and that a runner’s age mostly affects their finish time as well. However, due to the large amount of people starting to run in their 30s, the correlation between age and finish time gets a little blurred. I also learned that in general, most runners take longer to finish the second half of the marathon compared to the first half, regardless of their gender.
The data itself didn’t have much cleaning to do because it was already cleaned by the author of the Kaggle post, however the author was using this data to compare the weather of the runner’s home address to their performance at the Boston Marathon. I just wanted to use the data to learn more about marathons because so many people of my age and many of my friends started to take up running as a hobby.
The only trouble I really had while creating the graphs was making sure the order of the age groups were correct in the legend in the first chart (“Bib Number vs Finish Time”), and also making the second graph have a reasonable x- and y- axis such that it could be easy to tell whether or not it took a runner longer to finish the second half. To do this, I made both axes have the same scale using scale_x/y_continuous and controlling the limits, as well as visually controlling the axes by using coord_fixed(ratio=1). I also created a dotted reference line with a 1:1 slope to more easily see where the ticks are the same.