fastELO <- function(games, k = 30, initialValue = 1500, elos = numeric()) { games <- na.omit(games) games <- games[order(games$Date),] for (i in 1:nrow(games)) # loop over all rows (games) { homeTeam <- games$HomeTeam[i] awayTeam <- games$AwayTeam[i] # set initial ELO value if necessary if (!(homeTeam %in% names(elos))) { elos[homeTeam]=initialValue } if (!(awayTeam %in% names(elos))) { elos[awayTeam]=initialValue } # compute elo rating as described on https://en.wikipedia.org/wiki/Elo_rating_system gd <- games$FTHG[i] - games$FTAG[i] P <- 0 if (gd > 0) { W_home <- 1 We_home <- 1/(1+10^((elos[awayTeam]-elos[homeTeam])/400)) # expected result P <- k*(W_home-We_home) # change } else if (gd < 0) { W_home <- 0 We_home <- 1/(1+10^((elos[awayTeam]-elos[homeTeam])/400)) # expected result P <- k*(W_home-We_home) # change } elos[homeTeam] <- elos[homeTeam] + P elos[awayTeam] <- elos[awayTeam] - P } return(elos) }