I wrote about the Rand Index (RI) and the Adjusted Rand Index (ARI) in the last two posts but how do we interpret the indices and how are they different?

The RI is:

where $$a$$ and $$b$$ are the number of times a pair of items was clustered concordantly in two different sets. I wrote some code that calculates the RI, as well as returning values for $$a$$, $$b$$, and the denominator.

# for illustrative purposes only # this code is very slow for large sets # use the implementation in fossil::rand.index() rand_index <- function(x, y){ my_pair <- t(combn(1: length(x), 2)) is_equal <- function(x){ return(x[1] == x[2]) } my_a <- 0 my_b <- 0 for(i in 1:nrow(my_pair)){ if(is_equal(x[my_pair[i,]]) == is_equal(y[my_pair[i,]]) && is_equal(y[my_pair[i,]]) == TRUE){ my_a <- my_a + 1 } if(is_equal(x[my_pair[i,]]) == is_equal(y[my_pair[i,]]) && is_equal(y[my_pair[i,]]) == FALSE){ my_b <- my_b + 1 } } my_denom <- choose(length(x), 2) return(list(a = my_a, b = my_b, denom = my_denom, ri = (my_a + my_b) / my_denom)) }

I’ll use the example from my previous post, where I compared a set of known labels with results from *k*-means clustering:

true_label <- as.numeric(iris$Species) my_kmeans <- kmeans(x = iris[,-5], centers = 3) rand_index(true_label, my_kmeans$cluster) $a [1] 3075 $b [1] 6756 $denom [1] 11175 $ri [1] 0.8797315

Since the RI ranges from 0 to 1, an index of 0.8797315 implies that the two sets are similar. But how does this compare with a random set? The ARI takes randomness into account and is defined as:

See my previous post for an explanation of the ARI. I wrote some code below that will return the ARI, as well as each part of the ARI formula.

adjusted_rand_index <- function(x, y){ my_table <- table(x, y) my_choose <- function(x) return(choose(x, 2)) n_ij <- sum(sapply(my_table, my_choose)) a_i <- sum(sapply(rowSums(my_table), my_choose)) b_j <- sum(sapply(colSums(my_table), my_choose)) c <- my_choose(length(x)) e <- a_i*b_j/c ari <- (n_ij - e) / (1/2*(a_i+b_j) - e) return(list(n_ij = n_ij, a_i = a_i, b_j = b_j, c = c, e = e, ari = ari)) }

Here’s the ARI for the previous example.

true_label <- as.numeric(iris$Species) my_kmeans <- kmeans(x = iris[,-5], centers = 3) adjusted_rand_index(true_label, my_kmeans$cluster) $n_ij [1] 3075 $a_i [1] 3675 $b_j [1] 3819 $c [1] 11175 $e [1] 1255.913 $ari [1] 0.7302383

The ARI (0.7302383) is slightly lower than the RI (0.8797315). Now let’s calculate the RI and ARI between the known labels and a random set.

my_random <- sample(x = true_label, size = length(true_label)) rand_index(true_label, my_random) $a [1] 1188 $b [1] 5013 $denom [1] 11175 $ri [1] 0.5548993 adjusted_rand_index(true_label, my_random) $n_ij [1] 1188 $a_i [1] 3675 $b_j [1] 3675 $c [1] 11175 $e [1] 1208.557 $ari [1] -0.008334694

Even with a random set, there is a lot of agreement with the known labels (6201/11175). The ARI provides an index that is close to 0 because it takes into account the chance of overlap. In addition, note that the ARI is a negative value indicating that the amount of overlap is less than expected.

Let’s calculate the RI and ARI for 100,000 randomly sets generated from the known labels of the iris dataset to get a distribution of the indices.

# install if you haven't already install.packages("fossil") my_ari <- matrix(rep(0, 100000), ncol = 1) my_ri <- matrix(rep(0, 100000), ncol = 1) for (i in 1:nrow(my_ari)){ set.seed(i) my_random <- sample(true_label, length(true_label)) my_ari[i,1] <- adjusted_rand_index(true_label, my_random)$ari my_ri[i,] <- fossil::rand.index(true_label, my_random) } library(ggplot2) library(cowplot) df <- data.frame(ri = my_ri[,1], ari = my_ari[,1]) ri_plot <- ggplot(df, aes(ri)) + geom_histogram(bins = 50) ari_plot <- ggplot(df, aes(ari)) + geom_histogram(bins = 50) plot_grid(ri_plot, ari_plot, labels = c("A", "B"))

*Note that the (A) RI and (B) ARI have a very similar distribution; only the scale on the x-axis differs. In addition, note that the ARI distribution is centred around zero*.

I’ll perform another 100,000 calculations on random sets but this time using larger sets (1,000) and more clusters (10).

my_ari <- matrix(rep(0, 100000), ncol = 1) my_ri <- matrix(rep(0, 100000), ncol = 1) for (i in 1:nrow(my_ari)){ set.seed(i) x <- sample(1:10, 1000, replace = TRUE) set.seed(i+1) y <- sample(1:10, 1000, replace = TRUE) my_ari[i,1] <- adjusted_rand_index(x, y)$ari my_ri[i,] <- fossil::rand.index(x, y) } library(ggplot2) library(cowplot) df <- data.frame(ri = my_ri[,1], ari = my_ari[,1]) ri_plot <- ggplot(df, aes(ri)) + geom_histogram(bins = 50) ari_plot <- ggplot(df, aes(ari)) + geom_histogram(bins = 50) plot_grid(ri_plot, ari_plot, labels = c("A", "B"))

*The average (A) RI is around 0.82, while the (B) ARI is around 0*.

## High RI but low ARI

How do two random sets have a RI that is close to 1? The reason is due to the number of clusters. When there are a lot of clusters, there’s a higher chance that a pair of items in both sets are in different clusters. This is still counted as a concordant event in the RI. If we run rand_index() we will see that there’s a large discrepancy between $$a$$ and $$b$$.

set.seed(1) x <- sample(1:10, 1000, replace = TRUE) set.seed(2) y <- sample(1:10, 1000, replace = TRUE) rand_index(x, y) $a [1] 5065 $b [1] 404727 $denom [1] 499500 $ri [1] 0.8204044

The ARI on the other hand considers all cluster pairs in contrast to the RI, which only considers whether a pair of elements are in the same cluster or in different clusters.

adjusted_rand_index(x, y) $n_ij [1] 5065 $a_i [1] 50013 $b_j [1] 49825 $c [1] 499500 $e [1] 4988.784 $ari [1] 0.001696314 # the ARI is based on this contingency table table(x, y) y x 1 2 3 4 5 6 7 8 9 10 1 9 14 12 5 10 9 9 8 7 13 2 9 9 8 7 10 16 12 9 10 14 3 8 11 8 10 6 7 14 5 9 15 4 13 10 12 10 21 7 5 9 15 10 5 21 11 13 8 9 11 9 9 12 12 6 11 5 9 13 8 10 8 7 9 6 7 8 6 10 9 10 11 7 5 16 8 8 10 14 12 15 14 8 5 10 12 6 9 10 8 8 9 4 9 11 9 7 13 10 12 18 5 9 2 14 13 14 12 11

**Conclusion: use the ARI.**

This work is licensed under a Creative Commons

Attribution 4.0 International License.