execute: echo: false warning: false


#|label: setup
#|message: false

suppressPackageStartupMessages({
  library(tidyverse)
  library(dplyr)
  library(ggplot2)  
  library(brms)
  library(forcats)
  library(tidybayes)
  })
Warning: package 'tidyverse' was built under R version 4.4.1
Warning: package 'brms' was built under R version 4.4.1
Warning: package 'tidybayes' was built under R version 4.4.1
#|label: British tibble
British <- tibble(
  Position = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
  Driver = c("Max Verstappen", "Lando Norris", "Lewis Hamilton", "Oscar Piastri", "George Russell", "Sergio Perez", "Fernando Alonso", "Alexander Albon", "Charles Leclerc", "Carlos Sainz", "Logan Sargeant", "Valtteri Bottas", "Nico Hulkenberg", "Lance Stroll", "Zhou Guanyu", "Yuki Tsunoda", "Nyck de Vries", "Pierre Gasly", "Kevin Magnussen", "Esteban Ocon"),
  Team = c("Red Bull Racing", "McLaren", "Mercedes", "McLaren", "Mercedes", "Red Bull Racing", "Aston Martin", "Williams", "Ferrari", "Ferrari", "Williams", "Alfa Romeo", "Haas", "Aston Martin", "Alfa Romeo", "RB", "RB", "Alpine", "Haas", "Alpine"),
  Laps = c(52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 46, 31, 19),
  Time_Retired = c("1:25:16.938", "1:25:20.736", "1:25:23.721", "1:25:24.714", "1:25:28.144", "1:25:29.820", "1:25:34.131", "1:25:34.816", "1:25:35.627", "1:25:36.386", "1:25:40.570", "1:25:42.768", "1:25:43.601", "1:25:44.421", "1:25:46.758", "1:25:48.163", "1:25:50.066", "DNF", "DNF", "DNF"),
  Gap = c(NA, "+3.798s", "+6.783s", "+7.776s", "+11.206s", "+12.882s", "+17.193s", "+17.878s", "+18.689s", "+19.448s", "+23.632s", "+25.830s", "+26.663s", "+27.483s", "+29.820s", "+31.225s", "+33.128s", "DNF", "DNF", "DNF"),
    Points = c(26, 18, 15, 12, 10, 8, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Speed_mph = c(133.9, 133.7, 133.7, 133.6, 133.5, 133.5, 133.4, 133.4, 133.4, 133.3, 133.2, 133.2, 133.2, 133.1, 133.1, 133.0, 133.0, 130.1, 139.0, 138.2)
)

British$Time_Retired[British$Time_Retired == "DNF"] <- 0
British$Gap[British$Gap == "0s"] <- NA
British <- British %>%
  arrange(Time_Retired)

British$Driver <- factor(British$Driver, levels = British$Driver[order(British$Time_Retired)])
convert_to_seconds <- function(time_str) {
  if (is.na(time_str) || time_str == "DNF") return(NA)
  parts <- unlist(strsplit(time_str, ":"))
  minutes <- as.numeric(parts[1])
  seconds <- as.numeric(parts[2])
  milliseconds <- as.numeric(parts[3])
  total_seconds <- minutes * 60 + seconds + milliseconds / 1000
  return(total_seconds)
}

British <- British %>%
  mutate(Time_Retired = sapply(Time_Retired, convert_to_seconds))


British$Team <- as.factor(British$Team)


fit_race_1 <- brm(formula = Time_Retired ~ Position + Team + Laps + Points + Speed_mph,
                  data = British,
                  family = gaussian(),
                  refresh = 0,
                  silent = 2,
                  seed = 9)
Warning: Rows containing NAs were excluded from the model.
Warning: There were 57 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
Warning: There were 823 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded
Warning: Examine the pairs() plot to diagnose sampling problems
Warning: The largest R-hat is 3.04, indicating chains have not mixed.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#r-hat
Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#bulk-ess
Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
fixef(fit_race_1)
                       Estimate    Est.Error          Q2.5         Q97.5
Intercept         -7.937395e+05 1.319279e+06 -4.365717e+06  4.721828e+05
Position           1.196634e-03 3.950925e-04  4.388342e-04  2.002721e-03
TeamAstonMartin   -7.587071e-04 9.444113e-04 -2.644333e-03  1.074137e-03
TeamFerrari       -3.327109e-03 1.055080e-03 -5.396480e-03 -1.275682e-03
TeamHaas          -5.608597e-04 1.010964e-03 -2.705026e-03  1.410034e-03
TeamMcLaren       -3.829953e-03 1.482380e-03 -6.673597e-03 -7.953884e-04
TeamMercedes      -2.746840e-03 1.367935e-03 -5.374008e-03 -1.671871e-05
TeamRB             7.079605e-04 1.049570e-03 -1.213880e-03  2.707578e-03
TeamRedBullRacing -3.019239e-03 1.347328e-03 -5.542189e-03 -2.918425e-04
TeamWilliams      -1.479629e-03 9.527706e-04 -3.322274e-03  3.561939e-04
Laps               1.526586e+04 2.537074e+04 -9.078819e+03  8.395771e+04
Points            -3.613510e-04 1.602858e-04 -6.878473e-04 -5.733868e-05
Speed_mph         -4.401931e-04 1.034908e-02 -2.012513e-02  1.981088e-02