Last updated: 2024-06-19
Checks: 7 0
Knit directory: muse/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20200712)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 0c163e0. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: r_packages_4.3.3/
Ignored: r_packages_4.4.0/
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/adjusted_rand_index.Rmd
)
and HTML (docs/adjusted_rand_index.html
) files. If you’ve
configured a remote Git repository (see ?wflow_git_remote
),
click on the hyperlinks in the table below to view the files as they
were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 0c163e0 | Dave Tang | 2024-06-19 | Adjusted Rand Index |
In my last post, I wrote about the Rand index. This post will be on the Adjusted Rand index (ARI), which is the corrected-for-chance version of the Rand index:
\[ AdjustedIndex = \frac{Index - ExpectedIndex}{MaxIndex - ExpectedIndex} \]
From Wikipedia:
The adjusted Rand index is the corrected-for-chance version of the Rand index. Such a correction for chance establishes a baseline by using the expected similarity of all pair-wise comparisons between clusterings specified by a random model.
Traditionally, the Rand Index was corrected using the Permutation Model for clusterings (the number and size of clusters within a clustering are fixed, and all random clusterings are generated by shuffling the elements between the fixed clusters). However, the premises of the permutation model are frequently violated; in many clustering scenarios, either the number of clusters or the size distribution of those clusters vary drastically. For example, consider that in k-means the number of clusters is fixed by the practitioner, but the sizes of those clusters are inferred from the data. Variations of the adjusted Rand Index account for different models of random clusterings.
Though the Rand Index may only yield a value between 0 and 1, the adjusted Rand index can yield negative values if the index is less than the expected index.
Given a set \(S\) of \(n\) elements, and two groupings or partitions (e.g. clusterings) of these elements, namely \(X = \{X_1, X_2, \ldots, X_r\}\) and \(Y = \{Y_1, Y_2, \ldots, Y_s\}\), the overlap between \(X\) and \(Y\) can be summarised in a contingency table \([n_{ij}]\) where each entry \(n_{ij}\) denotes the number of objects in common between \(X_i\) and \(Y_j : n_{ij} = | X_i \cap Y_j |.\)
\(Y_1\) | \(Y_2\) | \(\cdots\) | \(Y_s\) | \(Sums\) | |
---|---|---|---|---|---|
\(X_1\) | \(n_{11}\) | \(n_{12}\) | \(\cdots\) | \(n_{1s}\) | \(a_1\) |
\(X_2\) | \(n_{21}\) | \(n_{22}\) | \(\cdots\) | \(n_{2s}\) | \(a_2\) |
\(\vdots\) | \(\vdots\) | \(\vdots\) | \(\ddots\) | \(\vdots\) | \(\vdots\) |
\(X_r\) | \(n_{r1}\) | \(n_{r2}\) | \(\cdots\) | \(n_{rs}\) | \(a_r\) |
\(Sums\) | \(b_1\) | \(b_2\) | \(\cdots\) | \(b_s\) |
the adjusted index is:
\[ARI = \frac{ \sum_{ij} { {n_{ij}}\choose{2} } - [ \sum_{i} { {a_{i}}\choose{2} } \sum_{j} { {b_{j}}\choose{2} } ] / { {n}\choose{2} } } { \frac{1}{2} [ \sum_{i} { a_{i}\choose{2} } + \sum_{j} { {b_{j}}\choose{2} } ] - [ \sum_{i} { {a_{i}}\choose{2} } \sum_{j} { {b_{j}}\choose{2} } ] / { {n}\choose{2} } } \]
where \(n_{ij}\), \(a_i\), \(b_j\) are values from the contingency table.
As per usual, it’ll be easier to understand with an example. I’ll use R to create two random sets of elements, which represent clustering results.
x <- c(1, 2, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2)
y <- c(3, 2, 3, 2, 2, 1, 1, 2, 3, 1, 3, 1)
which(x == 1 & x == y)
[1] 6 7 10
In this example there are 3 clusters in both sets, so our contingency table will have three rows and three columns. We just need to count the co-occurrences to build the contingency table. \(n_{11}\) would be the number of times an element occurs in cluster 1 of X and cluster 1 of Y; this occurs three times: the sixth, seventh, and tenth elements.
Here’s the full contingency table:
\(Y_1\) | \(Y_2\) | \(Y_3\) | \(Row sums\) | |
---|---|---|---|---|
\(X_1\) | \(3\) | \(0\) | \(1\) | \(4\) |
\(X_2\) | \(1\) | \(2\) | \(1\) | \(4\) |
\(X_3\) | \(0\) | \(2\) | \(2\) | \(4\) |
\(Column sums\) | \(4\) | \(4\) | \(4\) |
table(x, y) |>
addmargins()
y
x 1 2 3 Sum
1 3 0 1 4
2 1 2 1 4
3 0 2 2 4
Sum 4 4 4 12
If you look closely at the ARI formula, there’s really just three different parts:
\(\sum\) means the sum, \(i\) refers to the row number, \(j\) refers to the column number, \(a\) refers to the row sum, and \(b\) refers to the column sum. Now let’s work out each part.
Substituting the values into the ARI formula we get:
\[ARI = \frac{6 - [18 \times 18] / { {12}\choose{2} } } {\frac{1}{2} [18+18] - [18 \times 18] / { {12}\choose{2} }} = \frac{6 - 4.909091}{18 - 4.909091} = 0.08333333\]
The {mclust}
package contains the adjustedRandIndex()
function that
can calculate the Adjusted Rand index.
if(!require("mclust")){
install.packages("mclust")
}
set.seed(1)
x <- sample(x = rep(1:3, 4), 12)
set.seed(2)
y <- sample(x = rep(1:3, 4), 12)
suppressMessages(
mclust::adjustedRandIndex(x, y)
)
[1] 0.08333333
sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
time zone: Etc/UTC
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] mclust_6.1.1 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1
[5] dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1
[9] tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] sass_0.4.9 utf8_1.2.4 generics_0.1.3 stringi_1.8.4
[5] hms_1.1.3 digest_0.6.35 magrittr_2.0.3 timechange_0.3.0
[9] evaluate_0.23 grid_4.4.0 fastmap_1.2.0 rprojroot_2.0.4
[13] jsonlite_1.8.8 processx_3.8.4 whisker_0.4.1 ps_1.7.6
[17] promises_1.3.0 httr_1.4.7 fansi_1.0.6 scales_1.3.0
[21] jquerylib_0.1.4 cli_3.6.2 rlang_1.1.3 munsell_0.5.1
[25] withr_3.0.0 cachem_1.1.0 yaml_2.3.8 tools_4.4.0
[29] tzdb_0.4.0 colorspace_2.1-0 httpuv_1.6.15 vctrs_0.6.5
[33] R6_2.5.1 lifecycle_1.0.4 git2r_0.33.0 fs_1.6.4
[37] pkgconfig_2.0.3 callr_3.7.6 pillar_1.9.0 bslib_0.7.0
[41] later_1.3.2 gtable_0.3.5 glue_1.7.0 Rcpp_1.0.12
[45] xfun_0.44 tidyselect_1.2.1 rstudioapi_0.16.0 knitr_1.46
[49] htmltools_0.5.8.1 rmarkdown_2.27 compiler_4.4.0 getPass_0.2-4