# rrank: Random ranks In hyper2: The Hyperdirichlet Distribution, Mark 2

## Description

A function for producing ranks randomly, consistent with a specified strength vector

## Usage

 1 2 3 rrank(n = 1, p, pnames=NULL, fill = FALSE, rnames=NULL) ## S3 method for class 'ranktable' print(x, ...) 

## Arguments

 n Number of observations p Strength vector pnames Character vector (“player names”) specifying names of the columns rnames Character vector (“row names” or “race names”) specifying names of the rows

mv

 fill Boolean, with default FALSE meaning to interpret the elements of p as strengths, notionally summing to one; and TRUE meaning to augment p with a fillup value x, ... Arguments passed to the print method

## Value

If n=1, return a vector; if n>1 return a matrix with n rows, each corresponding to a ranking. The canonical example is a race in which the probability of competitor i coming first is p_i/sum p, where the summation is over the competitors who have not already finished.

If, say, the first row of rrank() is c(2,5,1,3,4), then competitor 2 came first, competitor 5 came second, competitor 1 came third, and so on.

Note that function rrank() returns an object of class ranktable, which has its own special print method. The column names appear as “c1, c2, ...” which is intended to be read “came first”, “came second”, and so on. The difference between rank and order can be confusing.

 1 2 3 4 5 6 7 8 9 > x <- c(a=3.01, b=1.04, c=1.99, d=4.1) > x a b c d 3.01 1.04 1.99 4.10 > rank(x) a b c d 3 1 2 4 > order(x)  2 3 1 4 

In the above, rank() shows us that element a of x (viz 3.01) is the third largest, element b (viz 1.04) is the smallest, and so on; order(x) shows us that the smallest element x is x, the next smallest is x, and so on. Thus x[order(x)] == sort(x), and rank(x)[order(x)] == seq_along(x). In the current context we want ranks not orders; we want to know who came first, who came second, and so on:

 1 2 3 4 5 R> rrank(2,(4:1)/10) c1 c2 c3 c4 [1,] 2 3 1 4 [2,] 1 3 2 4 R> 

In the above, each row is a race; we have four runners and two races. In the first race (the top row), runner number 2 came first, runner 3 came second, runner 1 came third, and so on. In the second race (bottom row), runner 1 came first, etc. Taking the first race as an example:

Rank: who came first? runner 2. Who came second? runner 3. Who came third? runner 1. Who came fourth? runner 4. Recall that the Placket-Luce likelihood for a race in which the rank statistic was 2314 (the first race) would be \mjeqn \fracp_2p_2+p_3+p_1+p_4\cdot \fracp_3p_3+p_1+p_4\cdot \fracp_1p_1+p_4\cdot \fracp_4p_4omitted.

Order: where did runner 1 come? third. Where did runner 2 come? first. Where did runner 3 come? second. Where did runner 4 come? fourth. Thus the order statistic would be 3124.

Function rrank() is designed for rank_likelihood(), which needs rank data, not order data. Vignette “skating_analysis” gives another discussion.

Note that function rrank() returns an object of class “rrank”, which has its own print method that returns NA, intentionally. This can be confusing.

## Author(s)

Robin K. S. Hankin

ordertrans,rank_likelihood,skating
  1 2 3 4 5 6 7 8 9 10 11 12 ptrue <- (4:1)/10 names(ptrue) <- letters[1:4] rrank(100,p=ptrue) r1 <- rrank(100,p=ptrue) r2 <- rrank(10,p=ptrue) H <- rank_likelihood(r1) maxp(H) # should be close to ptrue H <- H + rank_likelihood(rrank(100,maxp(H))) maxp(H)