R/random_2014_ESC.R

#' @title Generate a Random Voting Network for the 2014 ESC Final
#' 
#' @description Generate a Random Voting Network for the 2014 ESC Final
#' 
#' @return Outputs a list of objects related to Random 2014 ESC Final Voting Network as an igraph Object.
#' 
#' @import igraph
#' 
#' @export
#'
#' @seealso 
#' 
#' @keywords Random Network, 2014 ESC Final
#' 
#' @references Gregor, jprockbelly and Williams, K. (2012). Is there a built-in function for finding the mode? Retrieved from https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode
#'
#' @examples 
#' # Example Data
#'  rand_graph = random_2014_ESC()
#' 
random_2014_ESC = function(){
  
  # create a list to hold the data of the random networks
  randESC = list()
  
  # Algorithmic appraoch
  # Create a data frame that represents a random network
  # (1) create a vector of 37 voting countries
  # (2) have a subset of 26 performing countries
  # (3) with probability 1/26 randomly assign 10 votes from each of the 37 voting countries to the 26 participating countries
  # Performing countries cannot vote for themselves
  
  # STEP 1: create Country_From Column
  # NOTE assume 1:26 are the performing countries and 27:37 are the non-performing countries
  Country_From <- rep(seq(from = 1, 
                          to = 37, 
                          by = 1), 
                      each = 10)
  
  # Step 2: create Country_To Column
  # random votes for performers
  # NOTE performers cannot vote for themselves
  performer_votes <- numeric(0)
  
  for(i in 1:26) {
    
    v <- sample(x = c(1:26)[-i], 
                size = 10, 
                replace = F)
    
    performer_votes <- c(performer_votes, v)
    
  } 
  
  # random votes for non-performers
  non_performer_votes <- rep(sample(x = 1:26, 
                                    size = 10, 
                                    replace = F), 
                             times = 11)
  
  # combine the votes for both performers and non performers
  Country_To <- c(performer_votes, non_performer_votes)
  
  # Step 3: create Points Column
  # rep point sequence
  Points <- rep(x = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 12), 
                times = 37)
  
  # Step 4: Create a Data Frame from Country_From, Country_TO and Points
  RG_DF <- cbind(Country_From, Country_To, Points)
  
  # create the node_list for the random graph
  RG_DF_nodes = as.data.frame(cbind(1:37, c(rep('Y', times = 26), rep('N', times = 11)))) 
  colnames(RG_DF_nodes) = c('Country', 'Perform')
  
  # add the RG_DF to the list
  randESC[[1]] = RG_DF
  
  # add the RG_DF_nodes to the list
  randESC[[2]] = RG_DF_nodes
  
  # Step 5: Create the Random Graph using the Data Frame
  RG <- graph_from_data_frame(d = RG_DF,
                              vertices = RG_DF_nodes,
                              directed = T) 
  
  # add the RG to the list
  randESC[[3]] = RG
  
  # Step 7: Calculate degree distibution for random graph
  RG_degree_dist <- degree_distribution(graph = RG, mode = "in")
  
  # add the RG_degree_dist to the list
  randESC[[4]] = RG_degree_dist
  
  # Step 8: Calculate the final scores of the random graph
  final_scores <- strength(graph = RG,
                           mode = "in",
                           weights = E(RG)$Points)
  
  # add final scores to the list
  randESC[[5]] = final_scores
  
  # Step 9: Find the winnering total
  winning_score = final_scores[which.max(final_scores)]
  
  # add the winning score to the list
  randESC[[6]] = winning_score
  
  # Step 10: Find the loosing score
  loosing_score = final_scores[which.min(final_scores[[5]][final_scores[[5]] != 0])]
  
  # add the winning score to the list
  randESC[[7]] = loosing_score
  
  # return the list 
  return(randESC)
}
oislen/BuenaVista documentation built on May 16, 2019, 8:12 p.m.