library(ggplot2) # functions source("fast3PRanking.R") source("computeELORanking.R") source("fastELO.R") source("defines.R") source("distances.R") # A1.1 + A1.2 games <- readRDS("db_data.Rda") s201516_start <- as.Date("2015-08-24") s201617_start <- as.Date("2016-08-26") s201718_start <- as.Date("2017-08-18") s201819_start <- as.Date("2018-08-24") starts <- list(s201516_start, s201617_start, s201718_start, s201819_start) s201819_end <- as.Date("2019-05-18") rating3P <- fast3PRanking(subset(games, Date >= s201819_start & Date <= s201819_end & League == "Bundesliga")) Ra_3P <- names(sort(rating3P[[2]], decreasing = TRUE)) spearmannDistances <- data.frame(StartDate=as.Date(character()), k=integer(), SpearmanDistanz=integer()) kenndallDistances <- data.frame(StartDate=as.Date(character()), k=integer(), KendallDistanz=integer()) Ks <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100) for (startDate in starts) { for (k in Ks) { eloRating <- fastELO(subset(games, Date >= startDate & Date <= s201819_end & League == "Bundesliga"), k = k) Rb_elos <- names(sort(eloRating, decreasing = TRUE)) Rb_elos <- Rb_elos[Rb_elos %in% Ra_3P] spearmannDistances <- tibble::add_row(spearmannDistances, StartDate=startDate, k=k, SpearmanDistanz=spearmanDistance(Ra_3P,Rb_elos)) kenndallDistances <- tibble::add_row(kenndallDistances, StartDate=startDate, k=k, KendallDistanz=kendallDistance(Ra_3P,Rb_elos)) } } spearmannDistances <- spearmannDistances[order(spearmannDistances$SpearmanDistanz),] kenndallDistances <- kenndallDistances[order(kenndallDistances$KendallDistanz),] # next ELO according to Kendall- and Spearman-Distance is ELO with k=10 for the last season only elos <- computeELORanking(subset(games, Date >= spearmannDistances$StartDate[[1]] & Date <= s201819_end & League == "Bundesliga"), k = spearmannDistances$k[1]) plotTopNN <- function(Ra, Rb, high = 1, low = 3) { topN <- names(sort(Ra[[2]], decreasing = TRUE))[high:low] ggplot() + plot_style + geom_line(data = subset(Ra[[1]], Team %in% topN), aes(x = Date, y = Points, color = Team), size = 2) + geom_line(data = subset(Rb[[1]], Team %in% topN), aes(x = Date, y = Points-1500, color = Team), size = 2, linetype = "longdash") + scale_colour_manual(values=cbPalette) } plotTopNN(rating3P, elos, low=5)