SR-M13 week 2 assignment 2026

This assignment is marked out of 32. It constitutes 8% of the total module mark.

Name: Carly Christensen

Student number: 2535701



Datasets for analysis

You will use data of goals scored in the Spanish football league and of the marathon race record time for female runners of different ages.

#spain_data <-read.csv("spanish_football_data.csv", header = TRUE)
#marathon_data <- read.csv("female_marathon.csv")

#### IF LOADING AN RSTUDIO DATA SPACE USE THIS COMMAND ###########
load("week2_workspace.Rdata")

Hypothesis testing

This question requires you to manipulate the Spanish football dataset to select particular years or teams, to look at the distribution of the data, and to calculate the statistical probability of difference between data samples.

This code shows how to create a new dataset by selecting part of a larger dataset and how to calculate statistical metrics according to some criteria (e.g. calculate the mean for all values from the same year).

Q1. Create a new dataset that contains data for Racing Santander in the 1929 season. The dataset should contain ONLY the season, home team name, visiting team name and the number of home goals. Display the structure of your dataset using the head(name) function in your R-code. [4 MARKS]

RacingSantander1929 = spain_data[spain_data$home == 'Racing Santander' & spain_data$Season == '1929', c("Season", "home", "visitor", "hgoal")]

head(RacingSantander1929)
    Season             home            visitor hgoal
91    1929 Racing Santander Espanyol Barcelona     4
103   1929 Racing Santander    Arenas de Getxo     2
114   1929 Racing Santander    Atletico Madrid     3
130   1929 Racing Santander          CE Europa     6
142   1929 Racing Santander      Real Sociedad     2
151   1929 Racing Santander       FC Barcelona     2

Q2. Create a histogram of the home goals scored by Racing Santander in 1929 matches and display the mean and standard deviation of this metric.
(NOTE: make sure that the histogram bins are centred at integer values). [4 MARKS]

bins=(1:8)-0.5
hist(RacingSantander1929$hgoal,
     breaks = bins,
     main = "Home goals by Racing Santander", 
     xlab = "Home goals", 
     ylab = "Frequency", 
     col = "blue")

mean(RacingSantander1929$hgoal)
[1] 3
sd(RacingSantander1929$hgoal)
[1] 1.414214

Q3. Create a histogram of the mean number of home goals scored per season by Racing Santander, over the whole period covered by this Spanish football dataset. [6 MARKS]

homegoalmean = spain_data[spain_data$home == 'RacingSantander' & spain_data$Season]
homegoalmean = aggregate(hgoal ~ Season, data = spain_data, FUN = mean)

bins=(1:6)-0.5
hist(homegoalmean$hgoal,
     main = "Racing Santander: Mean Home Goals per Season", 
     xlab = "Average Goals", 
     ylab = "Frequency (Number of Seasons)",
     col = "darkgreen", 
     breaks = bins)

Q4. Use a one-sample Student’s-t test to determine if the mean homegoals scored by Santander in the 1929 season is statistically different to the average they scored per season over the whole data record. HINT: ask an AI engine: ‘how do i do a one-sample t-test in R?’ [5 MARKS]

all_santander_home = spain_data[spain_data$home == 'Racing Santander', ]
historical_mu = mean(all_santander_home$hgoal, na.rm = TRUE)
goals_1929 = all_santander_home$hgoal[all_santander_home$Season == 1929]

t_test_results = t.test(goals_1929, mu = historical_mu)
print(t_test_results)

    One Sample t-test

data:  goals_1929
t = 2.8859, df = 8, p-value = 0.02033
alternative hypothesis: true mean is not equal to 1.639551
95 percent confidence interval:
 1.912939 4.087061
sample estimates:
mean of x 
        3 

Delete one of the following statements:

The 1929 season IS statistically different to the average.

Bivariate data

In this section you will perform a regression analysis to model the relationship between runner age and race time for the marathon. Dataset: marathon_data.

Q6. THE FINAL 2 QUESTIONS GO BEYOND WHAT I HAVE SHOWN YOU IN CLASS. You may want to complete these outside of the Wednesday session to give yourself more time. Remember - YOU CAN USE AN AI CHATBOT TO HELP YOU DEVELOP THE REQUIRED CODE.

Create a new dataset that contains marathon run times ONLY for runners aged between 30 and 50 years old. Create a fully-labeled plot of runtime vs. age for this dataset. [4 MARKS]

Use the R-function lm to calculate the coefficients of a 2nd order polynomial model for race time vs age.
i.e. model of the form: y = a + bx + cx2.
Add a line on your plot showing the regression line. [6 MARKS]

marathon_runtimes = marathon_data[marathon_data$age >= 30 & marathon_data$age <= 50, ]

plot(marathon_runtimes$age,marathon_runtimes$time,
     main = "Marathon Runtime vs. age (30-50 Years Old)",
     xlab = "Age (Years)",
     ylab = "Runtime (Minutes)",
     pch = 19,           
     col = "steelblue") 

model <- lm(time ~ age + I(age^2), data = marathon_runtimes)
summary(model)

Call:
lm(formula = time ~ age + I(age^2), data = marathon_runtimes)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.6968 -0.4692 -0.1487  0.5606  4.4186 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 171.34281   22.90676   7.480    9e-07 ***
age          -2.33757    1.16637  -2.004   0.0613 .  
I(age^2)      0.04047    0.01460   2.772   0.0130 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.95 on 17 degrees of freedom
Multiple R-squared:  0.8977,    Adjusted R-squared:  0.8857 
F-statistic: 74.58 on 2 and 17 DF,  p-value: 3.84e-09
plot(marathon_runtimes$age, marathon_runtimes$time,
     main = "Marathon Runtime vs. Age: Quadratic Fit",
     xlab = "Age (Years)", ylab = "Runtime (Minutes)",
     pch = 19, col = "steelblue")

age_range <- seq(30, 50, length.out = 100)
predicted_times <- predict(model, newdata = data.frame(age = age_range))

lines(age_range, predicted_times, col = "red", lwd = 3)

Q7. Use your linear model to predict the race time of a runner aged 65 years old.

Comment on the accuracy of your regression model by comparing your predicted value to the actual measurement data in the marathon_data set. [2 MARKS]

new_runner <- data.frame(age = 65)
predicted_time <- predict(model, newdata = new_runner)
predicted_time
       1 
190.3801 

The model is likely to be inaccurate for ages outside the 30–50 range. The actual runner was much faster (age 65.15, 187.8 minutes) in the original data set, than the predicted time, proving that the quadratic trend overestimates the impact of aging on performance for older athletes.

When asked to predict the time for a 65-year-old, you are assuming that the physiological “curve” of aging stays exactly the same for an extra 15 years. The model is likely to be inaccurate due to an extrapolation error; the quadratic trend established in the 30-50 age bracket overestimates the slowdown associated with aging, failing to account for the high performance levels possible in older, dedicated athletes.

A 2nd order polynomial is also very sensitive.

  • If your \(c\) coefficient (the \(age^2\) term) is positive, the curve starts to bend upward increasingly fast.

  • By the time it reaches age 65, the “bend” might be so steep that it predicts a much slower time than is realistic.