Functions to compute Iterative numeric ratings, i.e. which are recomputed after every game, and corresponding rankings.
rate_iterative(cr_data, rate_fun, initial_ratings = 0)
rank_iterative(cr_data, rate_fun, initial_ratings = 0, keep_rating = FALSE,
type = "desc", ties = c("average", "first", "last", "random", "max",
"min"), round_digits = 7)
add_iterative_ratings(cr_data, rate_fun, initial_ratings = 0)
cr_data | Competition results in format ready for as_longcr(). |
---|---|
rate_fun | Rating function (see Details). |
initial_ratings | Initial ratings (see Details). |
keep_rating | Whether to keep rating column in ranking output. |
type | Value for |
ties | Value for |
round_digits | Value for |
rate_iterative()
returns a tibble with columns
player
(player identifier) and rating_iterative
(Iterative
ratings, based on row order, by the end of competition
results). Interpretation of numbers depends on rating function
rate_fun
.
rank_iterative()
returns a tibble
with columns player
,
rating_iterative
(if keep_rating = TRUE
) and ranking_iterative
(Iterative ranking computed with round_rank()
based on
specified type
).
add_iterative_ratings()
returns a widecr form of
cr_data
with four rating columns added:
rating1Before - Rating of player1 before the game.
rating2Before - Rating of player2 before the game.
rating1After - Rating of player1 after the game.
rating2After - Rating of player2 after the game.
Iterative ratings of group of players are recomputed after every
game based on players' game scores and their ratings just before the game.
Theoretically this kind of ratings can be non-numeric and be computed on
competition results with variable number of players but they rarely do. This
package provides functions for computing iterative numeric ratings for
pairgames (competition results with games only
between two players). Error is thrown if cr_data
is not pairgames.
Games in widecr form are arranged in increasing order
of values in column game
(if it is present) and processed from first to
last row.
NA
values in column player
are allowed. These players are treated as
'ghosts': players of the same rating as opponent before the game. 'Ghosts'
are not actual players so they don't appear in the output of
rate_iterative()
. For games between two 'ghosts' ratings before and after
the game are set to 0.
The core of the rating system is rate_fun
. It should take the
following arguments:
rating1 - Rating of player1 before the game.
score1 - Score of player1 in the game.
rating2 - Rating of player2 before the game.
score2 - Score of player2 in the game.
rate_fun
should return a numeric vector of length 2: first element being a
rating of player1 after the game, second - of player2.
Ratings are computed based only on games between players of interest (see
Players) and NA
values.
Initial ratings should be defined with argument initial_ratings
. It
can be:
A single numeric value. In this case initial ratings for all players are set to this value.
A named vector of ratings. All non-NA
players, for which rating is
computed, should be present in its names (as character representation of
players' actual identifiers).
A data frame with first column representing player and second - initial
rating. It will be converted to named vector with
deframe() from tibble
package.
comperank
offers a possibility to handle certain set of players. It is done
by having player
column (in longcr format) as factor
with levels specifying all players of interest. In case of factor the result
is returned only for players from its levels. Otherwise - for all present
players.
test_rate_fun <- function(rating1, score1, rating2, score2) {
c(rating1, rating2) + ((score1 >= score2) * 2 - 1) * c(1, -1)
}
set.seed(1002)
cr_data <- data.frame(
game = rep(1:10, each = 2),
player = rep(1:5, times = 4),
score = runif(20)
)
cr_data$player[c(6, 8)] <- NA
# Different settings of add_iterative_ratings
add_iterative_ratings(cr_data, test_rate_fun)#> # A widecr object:
#> # A tibble: 10 x 9
#> game player1 score1 player2 score2 rating1Before rating2Before rating1After
#> <int> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.445 2 0.791 0 0 -1
#> 2 2 3 0.612 4 0.227 0 0 1
#> 3 3 5 0.374 NA 0.711 0 0 -1
#> 4 4 2 0.972 NA 0.642 1 1 2
#> 5 5 4 0.358 5 0.892 -1 -1 -2
#> 6 6 1 0.616 2 0.880 -1 2 -2
#> 7 7 3 0.559 4 0.499 1 -2 2
#> 8 8 5 0.0569 1 0.314 0 -2 -1
#> 9 9 2 0.632 3 0.827 3 2 2
#> 10 10 4 0.930 5 0.511 -3 -1 -2
#> # … with 1 more variable: rating2After <dbl>
add_iterative_ratings(cr_data, test_rate_fun, initial_ratings = 10)#> # A widecr object:
#> # A tibble: 10 x 9
#> game player1 score1 player2 score2 rating1Before rating2Before rating1After
#> <int> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.445 2 0.791 10 10 9
#> 2 2 3 0.612 4 0.227 10 10 11
#> 3 3 5 0.374 NA 0.711 10 10 9
#> 4 4 2 0.972 NA 0.642 11 11 12
#> 5 5 4 0.358 5 0.892 9 9 8
#> 6 6 1 0.616 2 0.880 9 12 8
#> 7 7 3 0.559 4 0.499 11 8 12
#> 8 8 5 0.0569 1 0.314 10 8 9
#> 9 9 2 0.632 3 0.827 13 12 12
#> 10 10 4 0.930 5 0.511 7 9 8
#> # … with 1 more variable: rating2After <dbl>
add_iterative_ratings(
cr_data, test_rate_fun,
initial_ratings = c("1" = 1, "2" = 2, "3" = 3, "4" = 4, "5" = 5)
)#> # A widecr object:
#> # A tibble: 10 x 9
#> game player1 score1 player2 score2 rating1Before rating2Before rating1After
#> <int> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.445 2 0.791 1 2 0
#> 2 2 3 0.612 4 0.227 3 4 4
#> 3 3 5 0.374 NA 0.711 5 5 4
#> 4 4 2 0.972 NA 0.642 3 3 4
#> 5 5 4 0.358 5 0.892 3 4 2
#> 6 6 1 0.616 2 0.880 0 4 -1
#> 7 7 3 0.559 4 0.499 4 2 5
#> 8 8 5 0.0569 1 0.314 5 -1 4
#> 9 9 2 0.632 3 0.827 5 5 4
#> 10 10 4 0.930 5 0.511 1 4 2
#> # … with 1 more variable: rating2After <dbl>#> # A widecr object:
#> # A tibble: 10 x 9
#> game player1 score1 player2 score2 rating1Before rating2Before rating1After
#> <int> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.445 2 0.791 0 1 -1
#> 2 2 3 0.612 4 0.227 2 3 3
#> 3 3 5 0.374 NA 0.711 4 4 3
#> 4 4 2 0.972 NA 0.642 2 2 3
#> 5 5 4 0.358 5 0.892 2 3 1
#> 6 6 1 0.616 2 0.880 -1 3 -2
#> 7 7 3 0.559 4 0.499 3 1 4
#> 8 8 5 0.0569 1 0.314 4 -2 3
#> 9 9 2 0.632 3 0.827 4 4 3
#> 10 10 4 0.930 5 0.511 0 3 1
#> # … with 1 more variable: rating2After <dbl>
# Ratings and ranking at the end of competition results.
rate_iterative(cr_data, test_rate_fun)#> # A tibble: 5 x 2
#> player rating_iterative
#> <int> <dbl>
#> 1 1 -1
#> 2 2 2
#> 3 3 3
#> 4 4 -2
#> 5 5 -2
rank_iterative(cr_data, test_rate_fun, type = "desc")#> # A tibble: 5 x 2
#> player ranking_iterative
#> <int> <dbl>
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> 4 4 4.5
#> 5 5 4.5
rank_iterative(cr_data, test_rate_fun, type = "desc", keep_rating = TRUE)#> # A tibble: 5 x 3
#> player rating_iterative ranking_iterative
#> <int> <dbl> <dbl>
#> 1 1 -1 3
#> 2 2 2 2
#> 3 3 3 1
#> 4 4 -2 4.5
#> 5 5 -2 4.5