jsd: Jensen-Shannon divergence

View source: R/stats.R

jsdR Documentation

Jensen-Shannon divergence

Description

Calculates a distance matrix from a matrix of probability distributions using Jensen-Shannon divergence. Adapted from https://enterotype.embl.de/.

Usage

jsd(M, pseudocount = 1e-06, normalizeCounts = FALSE)

Arguments

M

a probability distribution matrix, e.g., normalized transcript compatibility counts.

pseudocount

a small number to avoid division by zero errors.

normalizeCounts

logical, whether to attempt to normalize by dividing by the column sums. Set to TRUE if this is, e.g., a count matrix.

Value

A Jensen-Shannon divergence-based distance matrix.

References

https://web.archive.org/web/20240131141033/https://enterotype.embl.de/enterotypes.html#dm.

Examples

set.seed(42)
M <- matrix(rpois(100, lambda=100), ncol=5)
colnames(M) <- paste0("sample", 1:5)
rownames(M) <- paste0("gene", 1:20)
Mnorm <- apply(M, 2, function(x) x/sum(x))
Mjsd <- jsd(Mnorm)
# equivalently
Mjsd <- jsd(M, normalizeCounts=TRUE)
Mjsd
plot(hclust(Mjsd))
  

stephenturner/Tmisc documentation built on April 21, 2024, 8:31 a.m.