Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

correlation_distance

Correlation and cosine distance


Description

The correlation and cosine distances, which are derived from the dot product of the two vectors.

Usage

correlation_distance(x, y)

cosine_distance(x, y)

Arguments

x, y

Numeric vectors

Details

For vectors x and y, the cosine distance is defined as the cosine of the angle between the vectors,

d(x, y) = 1 - \frac{x \cdot y}{|x| |y|},

where |x| is the magnitude or L2 norm of the vector, |x| = √{∑_i x_i^2}. Relation to other definitions:

  • Equivalent to the cosine() function in scipy.spatial.distance.

The correlation distance is simply equal to one minus the Pearson correlation between vectors. Mathematically, it is equivalent to the cosine distance between the vectors after they are centered (x - \bar{x}). Relation to other definitions:

  • Equivalent to the correlation() function in scipy.spatial.distance.

  • Equivalent to the 1 - mempearson calculator in Mothur.

Value

The correlation or cosine distance. These are undefined if either x or y contain all zero elements, that is, if |x| = 0 or |y| = 0. In this case, we return NaN.

Examples

x <- c(2, 0)
y <- c(5, 5)
cosine_distance(x, y)
# The two vectors form a 45 degree angle, or pi / 4
1 - cos(pi / 4)

v <- c(3.5, 0.1, 1.4)
w <- c(3.3, 0.5, 0.9)
correlation_distance(v, w)
1 - cor(v, w)

abdiv

Alpha and Beta Diversity Measures

v0.2.0
MIT + file LICENSE
Authors
Kyle Bittinger [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.