Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Manage Competition Results

License

NotificationsYou must be signed in to change notification settings

echasnovski/comperes

Repository files navigation

Build StatusR-CMD-checkCodecov test coverageCRANDependenciesDownloads

comperes offers a pipe (%>%) friendly set of tools for storing andmanaging competition results. Understanding ofcompetition is quitegeneral: it is a set ofgames (abstract event) in whichplayers(abstract entity) gain some abstractscores (typically numeric). Themost natural example is sport results, however not the only one. Forexample, product rating can be considered as a competition betweenproducts as “players”. Here a “game” is a customer that reviews a set ofproducts by rating them with numerical “score” (stars, points, etc.).

This package leveragesdplyr’s grammar ofdata manipulation. Only basic knowledge is enough to usecomperes.

Overview

comperes provides the following functionality:

  • Store and convert competition results:
    • Inlong format as atibble withone row per game-player pair. Functions:as_longcr(),is_longcr().
    • Inwide format as atibble with one row per game with fixedamount of players. Functions:as_widecr(),is_widecr().
  • Summarise:
    • Computeitem summaries with functions usingdplyr’s grammar.Functions:summarise_item(),summarise_game(),summarise_player().
    • Compute andjoin item summaries to data for easy transformation.Functions:join_item_summary(),join_game_summary(),join_player_summary().
  • Compute Head-to-Head values (a summary statistic of directconfrontation between two players) with functions also usingdplyr’sgrammar:
    • Store output inlong format as atibble with one row per pair ofplayers. Function:h2h_long().
    • Store output inmatrix format as a matrix with rows and columnsdescribing players and entries - Head-to-Head values. Function:h2h_mat().
    • Usecommon Head-to-Head functions with rlang’s unquotingmechanism. Example:. %>% h2h_mat(!!!h2h_funs["num_wins"]).

Installation

You can installcomperes from CRAN with:

install.packages("comperes")

To install the most recent development version from GitHub use:

# install.packages("devtools")devtools::install_github("echasnovski/comperes")

Examples

Store and Convert

We will be usingncaa2005, data fromcomperes package. It is anexample competition results (hereafter - results) of an isolated groupof Atlantic Coast Conference teams provided in book“Who’s#1”by Langville and Meyer. It looks like this:

library(comperes)ncaa2005#> # A longcr object:#> # A tibble: 20 × 3#>    game player score#>   <int> <chr>  <int>#> 1     1 Duke       7#> 2     1 Miami     52#> 3     2 Duke      21#> 4     2 UNC       24#> 5     3 Duke       7#> 6     3 UVA       38#> # … with 14 more rows

This is an object of classlongcr which describes results in long form(each row represents the score of particular player in particulargame). Because in this competition a game always consists from twoplayers, more natural way to look atncaa2005 is in wide format:

as_widecr(ncaa2005)#> # A widecr object:#> # A tibble: 10 × 5#>    game player1 score1 player2 score2#>   <int> <chr>    <int> <chr>    <int>#> 1     1 Duke         7 Miami       52#> 2     2 Duke        21 UNC         24#> 3     3 Duke         7 UVA         38#> 4     4 Duke         0 VT          45#> 5     5 Miami       34 UNC         16#> 6     6 Miami       25 UVA         17#> # … with 4 more rows

This convertedncaa2005 into an object ofwidecr class whichdescribes results in wide format (each row represents scores of allplayers in particular game). Allcomperes functions expect either adata frame with results structured in long format or one of supportedclasses:longcr,widecr.

Summarise

Withcompere the following summaries are possible:

ncaa2005 %>%  summarise_player(min_score= min(score),mean_score= mean(score))#> # A tibble: 5 × 3#>   player min_score mean_score#>   <chr>      <int>      <dbl>#> 1 Duke           0       8.75#> 2 Miami         25      34.5#> 3 UNC            3      12.5#> 4 UVA            5      18.5#> 5 VT             7      33.5# Using list of common summary functionslibrary(rlang)ncaa2005 %>%  summarise_game(!!!summary_funs[c("sum_score","num_players")])#> # A tibble: 10 × 3#>    game sum_score num_players#>   <int>     <int>       <int>#> 1     1        59           2#> 2     2        45           2#> 3     3        45           2#> 4     4        45           2#> 5     5        50           2#> 6     6        42           2#> # … with 4 more rows

Supplied list of common summary functions has 8 entries, which arequoted expressions to be used indplyr grammar:

summary_funs#> $min_score#> min(score)#>#> $max_score#> max(score)#>#> $mean_score#> mean(score)#>#> $median_score#> median(score)#>#> $sd_score#> sd(score)#>#> $sum_score#> sum(score)#>#> $num_games#> length(unique(game))#>#> $num_players#> length(unique(player))ncaa2005 %>% summarise_player(!!!summary_funs)#> # A tibble: 5 × 9#>   player min_score max_score mean_score median_score sd_score sum_score num_games num_players#>   <chr>      <int>     <int>      <dbl>        <dbl>    <dbl>     <int>     <int>       <int>#> 1 Duke           0        21       8.75          7       8.81        35         4           1#> 2 Miami         25        52      34.5          30.5    12.3        138         4           1#> 3 UNC            3        24      12.5          11.5     9.40        50         4           1#> 4 UVA            5        38      18.5          15.5    14.0         74         4           1#> 5 VT             7        52      33.5          37.5    19.9        134         4           1

To modify scores based on the rest of results one can usejoin_*_summary() functions:

suppressPackageStartupMessages(library(dplyr))ncaa2005_mod<-ncaa2005 %>%  join_player_summary(player_mean_score= mean(score)) %>%  join_game_summary(game_mean_score= mean(score)) %>%  mutate(score=player_mean_score-game_mean_score)ncaa2005_mod#> # A longcr object:#> # A tibble: 20 × 5#>    game player score player_mean_score game_mean_score#>   <int> <chr>  <dbl>             <dbl>           <dbl>#> 1     1 Duke   -20.8              8.75            29.5#> 2     1 Miami    5               34.5             29.5#> 3     2 Duke   -13.8              8.75            22.5#> 4     2 UNC    -10               12.5             22.5#> 5     3 Duke   -13.8              8.75            22.5#> 6     3 UVA     -4               18.5             22.5#> # … with 14 more rowsncaa2005_mod %>% summarise_player(mean_score= mean(score))#> # A tibble: 5 × 2#>   player mean_score#>   <chr>       <dbl>#> 1 Duke       -15.5#> 2 Miami       11.4#> 3 UNC         -5#> 4 UVA         -2.12#> 5 VT          11.2

This code modifiesscore to be average player score minus average gamescore. Negative values indicate poor game performance.

Head-to-Head

Computation of Head-to-Head performance is done withh2h_long()(output is a tibble; allows multiple Head-to-Head values per pair ofplayers) orh2h_mat() (output is a matrix; only one value per pair ofplayers).

Head-to-Head functions should be supplied indplyr grammar but forplayers’ matchups: direct confrontation betweenordered pairs ofplayers (including playing with themselves) stored in wide format:

ncaa2005 %>% get_matchups()#> # A widecr object:#> # A tibble: 40 × 5#>    game player1 score1 player2 score2#>   <int> <chr>    <int> <chr>    <int>#> 1     1 Duke         7 Duke         7#> 2     1 Duke         7 Miami       52#> 3     1 Miami       52 Duke         7#> 4     1 Miami       52 Miami       52#> 5     2 Duke        21 Duke        21#> 6     2 Duke        21 UNC         24#> # … with 34 more rows

Typical Head-to-Head computation is done like this:

ncaa2005 %>%  h2h_long(mean_score_diff= mean(score1-score2),num_wins= sum(score1>score2)  )#> # A long format of Head-to-Head values:#> # A tibble: 25 × 4#>   player1 player2 mean_score_diff num_wins#>   <chr>   <chr>             <dbl>    <int>#> 1 Duke    Duke                  0        0#> 2 Duke    Miami               -45        0#> 3 Duke    UNC                  -3        0#> 4 Duke    UVA                 -31        0#> 5 Duke    VT                  -45        0#> 6 Miami   Duke                 45        1#> # … with 19 more rowsncaa2005 %>% h2h_mat(mean(score1-score2))#> # A matrix format of Head-to-Head values:#>       Duke Miami UNC UVA  VT#> Duke     0   -45  -3 -31 -45#> Miami   45     0  18   8  20#> UNC      3   -18   0   2 -27#> UVA     31    -8  -2   0 -38#> VT      45   -20  27  38   0

Supplied list of common Head-to-Head functions has 9 entries, which arealso quoted expressions:

h2h_funs#> $mean_score_diff#> mean(score1 - score2)#>#> $mean_score_diff_pos#> max(mean(score1 - score2), 0)#>#> $mean_score#> mean(score1)#>#> $sum_score_diff#> sum(score1 - score2)#>#> $sum_score_diff_pos#> max(sum(score1 - score2), 0)#>#> $sum_score#> sum(score1)#>#> $num_wins#> num_wins(score1, score2, half_for_draw = FALSE)#>#> $num_wins2#> num_wins(score1, score2, half_for_draw = TRUE)#>#> $num#> dplyr::n()ncaa2005 %>% h2h_long(!!!h2h_funs)#> # A long format of Head-to-Head values:#> # A tibble: 25 × 11#>   player1 player2 mean_score_diff mean_…¹ mean_…² sum_s…³ sum_s…⁴ sum_s…⁵ num_w…⁶ num_w…⁷   num#>   <chr>   <chr>             <dbl>   <dbl>   <dbl>   <int>   <dbl>   <int>   <dbl>   <dbl> <int>#> 1 Duke    Duke                  0       0    8.75       0       0      35       0       2     4#> 2 Duke    Miami               -45       0    7        -45       0       7       0       0     1#> 3 Duke    UNC                  -3       0   21         -3       0      21       0       0     1#> 4 Duke    UVA                 -31       0    7        -31       0       7       0       0     1#> 5 Duke    VT                  -45       0    0        -45       0       0       0       0     1#> 6 Miami   Duke                 45      45   52         45      45      52       1       1     1#> # … with 19 more rows, and abbreviated variable names ¹​mean_score_diff_pos, ²​mean_score,#> #   ³​sum_score_diff, ⁴​sum_score_diff_pos, ⁵​sum_score, ⁶​num_wins, ⁷​num_wins2

To compute Head-to-Head for only subset of players or include values forplayers that are not in the results, use factorplayer column:

ncaa2005 %>%  mutate(player=factor(player,levels= c("Duke","Miami","Extra"))) %>%  h2h_mat(!!!h2h_funs["num_wins"],fill=0)#> # A matrix format of Head-to-Head values:#>       Duke Miami Extra#> Duke     0     0     0#> Miami    1     0     0#> Extra    0     0     0

About

Manage Competition Results

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp