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

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

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

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, rrank() returns a vector; if n>1 it returns 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_j\), 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.


> 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)
[1] 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[2], the next smallest is x[3], 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:


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 \( \frac{p_2}{p_2+p_3+p_1+p_4}\cdot \frac{p_3}{p_3+p_1+p_4}\cdot \frac{p_1}{p_1+p_4}\cdot \frac{p_4}{p_4}\).

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. This can be confusing. Further details are given at ranktable.Rd.

Function rrank_single() is a low-level helper function:


> p <- c(0.02,0.02,0.9,0.02,0.02,0.02)  # competitor 3 the strongest
> rank_single(p)
[1] 3 2 4 6 4 1

Above, we see from p that competitor 3 is the strongest, coming first with 90% probability. And indeed the resulting rank statistic given by rorder_single() shows competitor 3 coming first, 2 coming second, and so on. Compare rrank_single():


> rorder_single(p)
[1] 6 3 1 4 5 2
>

Above we see see from rrank_single(p) that competitor 1 came sixth, competitor 2 came third, and competitor 3 came first (as you might expect, as competitor 3 is the strongest). Note that the R idiom for rorder_single() is the same as that used in the permutations package for inverting a permutation: o[o] <- seq_along(o).

Author

Robin K. S. Hankin

Note

Similar functionality is given by rrace(), documented at rhyper3.

Examples


rrank_single(zipf(9))
#> [1] 3 6 4 2 1 9 8 7 5

ptrue <- (4:1)/10
names(ptrue) <- letters[1:4]
rrank(10,p=ptrue)
#>       c1 c2 c3 c4
#>  [1,] d  b  a  c 
#>  [2,] b  c  a  d 
#>  [3,] c  b  a  d 
#>  [4,] d  a  b  c 
#>  [5,] b  a  c  d 
#>  [6,] b  a  c  d 
#>  [7,] b  a  c  d 
#>  [8,] c  a  d  b 
#>  [9,] a  b  d  c 
#> [10,] b  a  d  c 

H <- rank_likelihood(rrank(40,p=ptrue))

## Following code commented out because they take too long:

# mH <- maxp(H)   # should be close to ptrue
# H <- H + rank_likelihood(rrank(30,mH)) # run some more races
# maxp(H)  # revised estimate with additional data