correlation_distance {abdiv} R Documentation

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


[Package abdiv version 0.2.0 Index]