MendozaM_Project2

Maria Mendoza - Project 2 - Marathon Results

Source: Running with Rock

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 packages
library(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 dataset
athletes <- 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.

View the data

# view the data
str(athletes)
spc_tbl_ [17,220 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ Bib           : num [1:17220] 25 9 20 46 51 36 43 37 47 30 ...
 $ Zip           : chr [1:17220] "93730" "97124" "80922" "02136" ...
 $ Age           : num [1:17220] 30 32 40 38 26 23 27 29 41 33 ...
 $ Age Group     : chr [1:17220] "Under 35" "Under 35" "40-44" "35-39" ...
 $ Gender        : chr [1:17220] "M" "M" "M" "M" ...
 $ First Half    : num [1:17220] 3832 3845 3795 3997 3979 ...
 $ Second Half   : num [1:17220] 3961 4059 4157 4065 4198 ...
 $ Finish        : num [1:17220] 7793 7904 7952 8062 8177 ...
 $ Positive Split: num [1:17220] 129 214 362 68 219 242 270 188 146 388 ...
 $ Percent Change: num [1:17220] 0.0337 0.0557 0.0954 0.017 0.055 ...
 - attr(*, "spec")=
  .. cols(
  ..   Bib = col_double(),
  ..   Zip = col_character(),
  ..   Age = col_double(),
  ..   `Age Group` = col_character(),
  ..   Gender = col_character(),
  ..   `First Half` = col_double(),
  ..   `Second Half` = col_double(),
  ..   Finish = col_double(),
  ..   `Positive Split` = col_double(),
  ..   `Percent Change` = col_double()
  .. )
 - attr(*, "problems")=<pointer: 0x000002cb02cffcf0> 
names(athletes)
 [1] "Bib"            "Zip"            "Age"            "Age Group"     
 [5] "Gender"         "First Half"     "Second Half"    "Finish"        
 [9] "Positive Split" "Percent Change"
head(athletes)
# 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 lowercase
names(athletes)<- gsub(" ","",names(athletes))
names(athletes)<-tolower(names(athletes))
names(athletes) #check my work
 [1] "bib"           "zip"           "age"           "agegroup"     
 [5] "gender"        "firsthalf"     "secondhalf"    "finish"       
 [9] "positivesplit" "percentchange"
# filter the data
athletes2 <- athletes |> select(bib, agegroup, gender, firsthalf, secondhalf, finish)
head(athletes2) #check my work
# A tibble: 6 × 6
    bib agegroup gender firsthalf secondhalf finish
  <dbl> <chr>    <chr>      <dbl>      <dbl>  <dbl>
1    25 Under 35 M           3832       3961   7793
2     9 Under 35 M           3845       4059   7904
3    20 40-44    M           3795       4157   7952
4    46 35-39    M           3997       4065   8062
5    51 Under 35 M           3979       4198   8177
6    36 Under 35 M           3973       4215   8188

Simple graphs to understand the data

#simple graph of number of runners by age group
athletes2 |> 
  count(agegroup)
# A tibble: 11 × 2
   agegroup        n
   <chr>       <int>
 1 35-39        2263
 2 40-44        2334
 3 45-49        2289
 4 50-54        1745
 5 55-59        1410
 6 60-64        1097
 7 65-69         566
 8 70-74         179
 9 75-79          39
10 80 and Over     8
11 Under 35     5290
athletes2$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=agegroup))+
  geom_bar()

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 group
athletes_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
# A tibble: 11 × 2
   agegroup    avgfinish
   <fct>           <dbl>
 1 Under 35       13603.
 2 35-39          13422.
 3 40-44          13667.
 4 45-49          14016.
 5 50-54          14496.
 6 55-59          14926.
 7 60-64          15624.
 8 65-69          16340.
 9 70-74          17431.
10 75-79          18661.
11 80 and Over    18333.
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 model
marathon_model <- lm(finish ~ agegroup + bib, data = athletes)
summary(marathon_model)

Call:
lm(formula = finish ~ agegroup + bib, data = athletes)

Residuals:
    Min      1Q  Median      3Q     Max 
-7241.1 -1105.3  -310.1   787.9 13831.8 

Coefficients:
                     Estimate Std. Error t value Pr(>|t|)    
(Intercept)         9.933e+03  4.419e+01 224.802  < 2e-16 ***
agegroup40-44       1.210e+02  5.363e+01   2.256   0.0241 *  
agegroup45-49       9.608e+01  5.398e+01   1.780   0.0751 .  
agegroup50-54       2.707e+02  5.813e+01   4.656 3.25e-06 ***
agegroup55-59       3.577e+02  6.210e+01   5.761 8.51e-09 ***
agegroup60-64       6.125e+02  6.763e+01   9.057  < 2e-16 ***
agegroup65-69       1.033e+03  8.626e+01  11.973  < 2e-16 ***
agegroup70-74       1.953e+03  1.417e+02  13.778  < 2e-16 ***
agegroup75-79       3.473e+03  2.938e+02  11.822  < 2e-16 ***
agegroup80 and Over 2.870e+03  6.439e+02   4.457 8.37e-06 ***
agegroupUnder 35    8.284e+00  4.567e+01   0.181   0.8561    
bib                 2.543e-01  1.618e-03 157.211  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1818 on 17208 degrees of freedom
Multiple R-squared:  0.6232,    Adjusted R-squared:  0.6229 
F-statistic:  2587 on 11 and 17208 DF,  p-value: < 2.2e-16
# make sure the age groups are in the right order
athletes2$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 genders
athletes2 |> 
  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 line

ggplot(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.