| compute_nmi | R Documentation |
Computes the normalized mutual information (NMI) between two discrete
variables, a name-blind, information-theoretic measure of association in
[0, 1]. NMI is the mutual information divided by the smaller of the two
marginal Shannon entropies; it is 0 for independent variables and 1 for a
perfect (deterministic) association, and unlike a raw chi-squared it is
comparable across variables with different numbers of levels.
compute_nmi(x, y = NULL)
x |
Either a two-way contingency table / matrix of counts, or a vector (factor, character, or numeric) of the first variable. |
y |
Optional. If |
A single numeric in [0, 1]. Returns 0 for degenerate input
(fewer than two rows/columns, zero total, or near-zero marginal entropy).
set.seed(1)
g <- sample(c("A", "B", "C"), 200, replace = TRUE)
y <- ifelse(g == "A", "yes", sample(c("yes", "no"), 200, replace = TRUE))
compute_nmi(g, y) # > 0: g carries information about y
compute_nmi(g, sample(g)) # ~0: shuffled -> independent
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.