R/get_day_elo.R

Defines functions get_day_elo

Documented in get_day_elo

#' Get Elo at the end of day
#' 
#' function to get elo-rating at the end of the day per individual
#' 
#' @param elo_sequence elo sequence generated by elo_model3()
#' @param ids vector of unique individual IDs
#'
#' @return A data frame with elo-ratings per day per ID
#' @export
#' @import dplyr
#' @examples
#' elo.ama <- get_day_elo(ama_seq3.2, ama_c)
#' 
get_day_elo <- function(elo_sequence, ids) {
 
  # create empty list to fill with elo ratings per individual
  elo.all <- vector(mode = "list", length = length(ids))
  
  for(i in seq_along(ids)){
    ID <- ids[i]
    li <- i
    
    ID.elo <- 
      elo_sequence %>% 
      filter(loser == ID | winner == ID) %>% 
      # get focal's elo after the interaction in new column
      mutate(elo_after = ifelse(loser == ID, elo_l_after, elo_w_after)) %>% 
      group_by(date) %>% 
      # keep only elo rating at the end of the day
      filter(row_number() == n()) %>% 
      ungroup() %>% 
      mutate(id = ID) %>% 
      rename(elo_rating = elo_after) %>% 
      select(date, id, elo_rating)
    
    elo.all[[i]] <- ID.elo
    names(elo.all)[i] <- ID
  }
  elo.all <- elo.all %>% bind_rows() %>% arrange(date, id)
  return(elo.all)
}
avrincon/phdfuns documentation built on Nov. 13, 2022, 10:34 a.m.