#' Calculate competition ranking by elo
#' @description Calculate the competitive ranking
#' @name elo_rank
#' @param network igraph object generated by `network_make()`
#' @return ...
elo_rank <- function(network) {
net <- network
# Remove duplicate coexistence
## Coexistence links
edges_coext <- subgraph.edges(net, eids = which(E(net)$interaction == "coexistence"), delete.vertices = F) %>%
as.undirected() %>%
as.directed(mode = "arbitrary") %>%
get.edgelist()
## Competition links
net_compt <- subgraph.edges(net, eids = which(E(net)$interaction == "exclusion"), delete.vertices = F)
net_comb <- add.edges(net_compt, as.vector(t(edges_coext)))
E(net_comb)$interaction[is.na(E(net_comb)$interaction)] <- "coexistence"
# Elo input table
pairs_elo <- as.data.frame(cbind(1, get.edgelist(net_comb), E(net_comb)$interaction)) %>% setNames(c("period", "from", "to", "result"))
pairs_elo$period <- as.numeric(pairs_elo$period)
pairs_elo$from <- as.character(pairs_elo$from)
pairs_elo$to <- as.character(pairs_elo$to)
pairs_elo$result <- as.character(pairs_elo$result)
pairs_elo$result[pairs_elo$result == "exclusion"] <- 1 # Player 1 wins
pairs_elo$result[pairs_elo$result == "coexistence"] <- 0.5 # Draw
pairs_elo$result <- as.numeric(pairs_elo$result)
# Elo rank
elo_ranking <- PlayerRatings::elo(pairs_elo)$rating %>% dplyr::select(-Lag)
# Standardize rating
min_rate <- min(elo_ranking$Rating)
max_rate <- max(elo_ranking$Rating)
elo_ranking$Rating <- (elo_ranking$Rating - min_rate) / (max_rate - min_rate)
return(elo_ranking)
}
if (FALSE) {
# Split by community and drop community column. Calculate Elo score
pairs1 <- pairs1[!is.na(pairs1$score), ] %>% # Remove missing value
split.data.frame(., f = .$replicate) %>%
lapply(function(x) {
PlayerRatings::elo(dplyr::select(x, replicate, from, to, score)) %>%
`[[`(1) %>%
mutate(replicate = x$replicate[1])
})
# Standardize the ranking score
# The most competitive isolate in the community scores 1, and the least competitive isolate scores 0.
pairs1 <- lapply(pairs1, function(x) {
min_rate <- min(x$Rating)
max_rate <- max(x$Rating)
x$Rating <- (x$Rating - min_rate) / (max_rate - min_rate)
return(x)
})
# rbind result
pairs1 <- do.call("rbind", pairs1)
#
pairs1 <- pairs1 %>%
dplyr::select(-Lag) %>%
setNames(c("isolate", "rating", "games", "win", "draw", "loss", "replicate")) %>%
dplyr::select(replicate, isolate, rating, games, win, draw, loss)
row.names(pairs1) <- NULL
return(pairs1)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.