knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(disPack)

Simulated examples

x = matrix(c(1, 2, 4, 3, 0, -4, 5, 0.1, 7), 3, 3)

a = rnorm(10)
b = rnorm(10)

Euclidean distance

Evaluate Euclidean distance on matrix with 3 points with 3 dimensions.

euclidean(x)

Add weight component and return as dist object.

euclidean(x, w = 0.5, output = "dist")

Evaluate Euclidean distance between two points with dimension 10 with weight 0.5

euclidean(a, b, w = 0.5)

Manhattan distance

Evaluate Manhattan distance on matrix with 3 points with 3 dimensions.

manhattan(x)

Evaluate Manhattan distance between two points with dimension 10 with return of a dist object

euclidean(a, b, output = "dist")

Minkowski distance

Evaluate Minkowski distance with one more parameters, p(required) and w (optional)

minkowski(x, p = 3)
minkowski(a, b, p = 0.5, w = 2)

Other examples

Cosine, correlation, Chebyshev, and Chi-square distance are used in the same way as Manhattan. Here are some examples:

cos_dis(x)
corr_dis(x)
chebyshev(a, b)
chiSq_dis(x)

Real world case

Distance data is from: https://people.sc.fsu.edu/~jburkardt/datasets/cities/wg22_xy.txt

data <- read.table("../dataset/wg22_xy.txt", quote="\"")
data <- as.matrix(data)
dimnames(data) <- NULL
name_list <- c("Aachen", "Augsburg", "Braunschweig", "Bremen", "Essen", "Freiburg", "Hamburg", "Hof", "Karlsruhe", "Kassel", "Kiel", "Koeln", "Mannheim", "Muenchen", "Nuernberg", "Passau", "Regensburg", "Saarbruecken", "Wuerzburg", "Bielefeld", "Luebeck","Muenster")
euclidean_dist = euclidean(data)
row.names(euclidean_dist) <- name_list
colnames(euclidean_dist) <- name_list
heatmap(euclidean_dist, Rowv = NA, Colv = NA, scale = "none")
cosine_dist = cos_dis(data)
row.names(cosine_dist) <- name_list
colnames(cosine_dist) <- name_list
heatmap(cosine_dist, Rowv = NA, Colv = NA, scale = "none")


yingtliu/distPack documentation built on Jan. 1, 2021, 1:45 p.m.