Using R to Measure Distance

1.question description:

using R code to calculate the distance, such as “euclidean” “chebyshev” “cosine”

2.solutions :

using the key words “R package” and “distance” searching for the packages, and
we got the package of philentropy

2.1.code :

#install package in console
install.packages("philentropy")
#load the package
library(philentropy)
#define the vectors needed calculating
v1 <- (1,3,4)
v2 <- (2,3,2)
#calculate with the default parameters
#manhattan distance,euclidean,cosine,lin.cor(pearson)
distance(x, method = "manhattan")
distance(x, method = "euclidean")

2.2.output :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Metric: 'manhattan'; comparing: 2 vectors.
manhattan
3
> distance(x, method = "euclidean")
Metric: 'euclidean'; comparing: 2 vectors.
euclidean
2.236068
> distance(x, method = "chebyshev")
Metric: 'chebyshev'; comparing: 2 vectors.
chebyshev
2
> distance(x, method = "cosine")
Metric: 'cosine'; comparing: 2 vectors.
cosine
0.9037378
> lin.cor(A,B)
pearson
0.1889822

3.supplyment materials:

the difference between different type of distance

3.1. euclidean

https://en.wikipedia.org/wiki/Euclidean_distance

In mathematics, the Euclidean distance between two points in Euclidean space is a number, the length of a line segment between the two points. It can be calculated from the Cartesian coordinates of the points using the Pythagorean theorem, and is occasionally called the Pythagorean distance.

3.2. cosine distance

cosine distance (or cosine similarity, angular cosine distance, angular cosine similarity) between two variables.
https://en.wikipedia.org/wiki/Cosine_similarity

Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space. It is defined to equal the cosine of the angle between them, which is also the same as the inner product of the same vectors normalized to both have length 1. The cosine of 0° is 1, and it is less than 1 for any angle in the interval (0, π] radians. It is thus a judgment of orientation and not magnitude: two vectors with the same orientation have a cosine similarity of 1, two vectors oriented at 90° relative to each other have a similarity of 0, and two vectors diametrically opposed have a similarity of -1, independent of their magnitude. The cosine similarity is particularly used in positive space, where the outcome is neatly bounded in {\displaystyle [0,1]}[0,1].

3.3. cosine distance

https://medium.com/swlh/is-correlation-distance-a-metric-5a383973978f#:~:text=Correlation%20distance%20is%20a%20popular,as%20d%3D1%2Dr.

Correlation distance is a popular way of measuring the distance between two random variables with finite variances¹. If the correlation² between two random variables is r, then their correlation distance is defined as d=1-r. However, a proper distance measure needs to have a few properties, i.e. should be a metric, and it is not trivial whether correlation distance has these properties. In this note, we ask whether correlation distance is a metric or not.

Author

John Doe

Posted on

2020-11-23

Updated on

2020-11-23

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.
You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.