knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

options(tibble.print_min = 6, tibble.print_max = 6)

comperes: Manage Competition Results

Build Status R-CMD-check Codecov test coverage CRAN Dependencies Downloads

comperes offers a pipe (%>%) friendly set of tools for storing and managing competition results. Understanding of competition is quite general: it is a set of games (abstract event) in which players (abstract entity) gain some abstract scores (typically numeric). The most natural example is sport results, however not the only one. For example, product rating can be considered as a competition between products as "players". Here a "game" is a customer that reviews a set of products by rating them with numerical "score" (stars, points, etc.).

This package leverages dplyr's grammar of data manipulation. Only basic knowledge is enough to use comperes.

Overview

comperes provides the following functionality:

Installation

You can install comperes 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 using ncaa2005, data from comperes package. It is an example competition results (hereafter - results) of an isolated group of Atlantic Coast Conference teams provided in book "Who's #1" by Langville and Meyer. It looks like this:

library(comperes)
ncaa2005

This is an object of class longcr which describes results in long form (each row represents the score of particular player in particular game). Because in this competition a game always consists from two players, more natural way to look at ncaa2005 is in wide format:

as_widecr(ncaa2005)

This converted ncaa2005 into an object of widecr class which describes results in wide format (each row represents scores of all players in particular game). All comperes functions expect either a data frame with results structured in long format or one of supported classes: longcr, widecr.

Summarise

With compere the following summaries are possible:

ncaa2005 %>%
  summarise_player(min_score = min(score), mean_score = mean(score))

# Using list of common summary functions
library(rlang)
ncaa2005 %>%
  summarise_game(!!!summary_funs[c("sum_score", "num_players")])

Supplied list of common summary functions has r length(summary_funs) entries, which are quoted expressions to be used in dplyr grammar:

summary_funs

ncaa2005 %>% summarise_player(!!!summary_funs)

To modify scores based on the rest of results one can use join_*_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

ncaa2005_mod %>% summarise_player(mean_score = mean(score))

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

Head-to-Head

Computation of Head-to-Head performance is done with h2h_long() (output is a tibble; allows multiple Head-to-Head values per pair of players) or h2h_mat() (output is a matrix; only one value per pair of players).

Head-to-Head functions should be supplied in dplyr grammar but for players' matchups: direct confrontation between ordered pairs of players (including playing with themselves) stored in wide format:

ncaa2005 %>% get_matchups()

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

ncaa2005 %>%
  h2h_long(
    mean_score_diff = mean(score1 - score2),
    num_wins = sum(score1 > score2)
  )

ncaa2005 %>% h2h_mat(mean(score1 - score2))

Supplied list of common Head-to-Head functions has r length(h2h_funs) entries, which are also quoted expressions:

h2h_funs

ncaa2005 %>% h2h_long(!!!h2h_funs)

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

ncaa2005 %>%
  mutate(player = factor(player, levels = c("Duke", "Miami", "Extra"))) %>%
  h2h_mat(!!!h2h_funs["num_wins"], fill = 0)


echasnovski/comperes documentation built on March 5, 2023, 4:27 p.m.