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/enterotypes.html#dm.

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.

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))
  

Tmisc documentation built on Aug. 23, 2023, 1:07 a.m.