Skip to content

Commit

Permalink
commit analysis' script finished
Browse files Browse the repository at this point in the history
  • Loading branch information
ffalves committed Aug 29, 2023
1 parent 637e012 commit 83b23cc
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Preparing dataset and working on ordinary methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

### Libraries that will be used (and that you should install them)
library(dplyr)
library(igraph)

######## PART 1
### Preparing dataset to analyse
Expand Down Expand Up @@ -47,6 +48,71 @@ borda = apply(borda, 1, sum)
borda_df = data.frame(cbind(sofifa_id, borda))
fifa_om = left_join(fifa_br, borda_df, by = 'sofifa_id')

# You can see the players ranking by Borda's scores just ordering the borda's column


### Condorcet Count Method

# Creating a Win matrix and initializing with zeros
wins = matrix(0, ncol = nrow(matrix_players), nrow = nrow(matrix_players))
rownames(wins) = matrix_df$sofifa_id
colnames(wins) = matrix_df$sofifa_id

# Creating a loop to count scores
for ( player in 1:(ncol(matrix_players)-1) ) {
for ( next_player in (player+1):ncol(matrix_players) ) {
# Wins
win_player = 0
win_next_player = 0

for ( duel in 1:nrow(matrix_players) ) {
if ( matrix_players[duel, player] > matrix_players[ duel, next_player] ) {
win_player = win_player+1
} else if ( matrix_players[duel, player] < matrix_players[duel, next_player] ) {
win_next_player = win_next_player+1
} else {
win_player = win_player+0
win_next_player = win_next_player+0
}
}

# Wins attribuitions
if ( win_player > win_next_player ) {
wins[player, next_player] = 1
wins[next_player, player] = -1
} else if ( win_player < win_next_player ) {
wins[player, next_player] = -1
wins[next_player, player] = 1
}
}
}

# We usually see Condorcet's results in a graph, so we have to build one
grafo = graph.adjacency(wins, mode = 'directed')

# Finding and removing the isolated players
players_off = which(degree(grafo, mode = 'total') == 0)
new_grafo = delete_vertices(grafo, players_off)

# Finding the winner
winner = which.max(degree(new_grafo, mode = 'out'))

# Changing the winner's color
V(new_grafo)$color = 'grey'
V(new_grafo)$color[winner] = 'green'

# Graph visualization to see the winner
plot(new_grafo)


### Copeland Count Method

# Adding player's scores from Condorcet's wins
copeland = apply(wins, 1, sum)
copeland = copeland[order(copeland, decreasing = T)]


# Assigning the Copeland's score to each player
copeland_df = data.frame(cbind(sofifa_id, copeland))
fifa_om = left_join(fifa_om, copeland_df, by = 'sofifa_id')

0 comments on commit 83b23cc

Please sign in to comment.