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 packagelibrary(philentropy)
#define the vectors needed calculatingv1 <- (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 | Metric: 'manhattan'; comparing: 2 vectors. |
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
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.
install_url
to use ShareThis. Please set it in _config.yml
.