# Minimum average rank


Find this notebook on the web at
<a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/testing_counts_2.html#nte-minimum_average_rank">Note <span>23.9</span></a>.

The notebook implements the following procedure:

1.  Put the numbers “1”, “2” and “3” (corresponding to ranks) in a
    bucket
2.  Shuffle the numbers and deal them out to three locations that
    correspond to treatments (call the locations “t1,” “t2,” and “t3”)
3.  Repeat step two another 32 times (for a total of 33 repetitions, for
    33 matched triplets)
4.  Find the average rank for each location (treatment).
5.  Record the minimum (best) score.
6.  Repeat steps 2-4, say, 10,000 times.
7.  Find out how often the minimum average rank for any treatment is as
    low as 1.48.

```{r}
# Set the number of trials
n_iters <- 10000

# An empty array to store the trial results.
results <- numeric(n_iters)

# Step 1 above.
bucket <- c(1, 2, 3)

n_rows <- 33

# Do 10000 trials.
for (i in 1:n_iters) {
    # Prepare vectors to store shuffled row results.
    t1 <- numeric(n_rows)
    t2 <- numeric(n_rows)
    t3 <- numeric(n_rows)
    # Step 3 (for all 33 rows).
    for (row_no in 1:n_rows) {
        # Step 2.
        shuffled <- sample(bucket)
        # Step 2 continued.
        t1[row_no] <- shuffled[1]
        t2[row_no] <- shuffled[2]
        t3[row_no] <- shuffled[3]
    }
    # Step 4.
    m_t1 <- mean(t1)
    m_t2 <- mean(t2)
    m_t3 <- mean(t3)
    # Step 5.
    means <- c(m_t1, m_t2, m_t3)
    min_mean <- min(means)
    results[i] <- min_mean
}

hist(results, breaks=50,
     main='Distribution of minimum average rank')

# Step 7.
k <- sum(results <= 1.49)
kk <- k / n_iters

message('Proportion minimum average rank <= 1.48: ', kk)
```
