ab.dist: Distance Functions for nearest neighbours

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/knn_header.R

Description

These functions compute Absolute (ab.dist), Euclidean (eu.dist) or Mahalanobis (mh.dist) distances between two points. The variant functions (*.matY), accomplish the same task, but between a point on the one hand, and every point specified as rows of a matrix on the other.

Usage

1
2
3
4
5
6
ab.dist(x, y)
eu.dist(x, y)
mh.dist(x, y, A)
ab.dist.matY(x, Y)
eu.dist.matY(x, Y)
mh.dist.matY(x, Y, A)

Arguments

x

The vector (point) from which distance is sought.

y

The vector (point) to which distance is sought.

Y

A set of points, specified as rows of a matrix, to which distances are sought.

A

The inverse matrix to use for the Mahalanobis distance.

Details

These functions are used internally to decide how the nearest neighbours shall be calculated; the user need not call any of these functions directly. Rather, the choice of distance is specified as a string ('euclidean' or 'absolute' or 'mahal').

Value

Either a single number for the distance, or a vector of distances, corresponding to each row of Y.

Author(s)

Mohit Dayal

See Also

get.NN

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
x <- c(1,2)
y <- c(0,3)
mu <- c(1,3)
Sigma <- rbind(c(1,0.2),c(0.2,1))
Y <- MASS::mvrnorm(20, mu = mu, Sigma = Sigma)
ab.dist(x,y)
eu.dist(x,y)
mh.dist(x,y,Sigma)
ab.dist.matY(x,Y)
eu.dist.matY(x,Y)
mh.dist.matY(x,Y,Sigma)

Example output

Warning message:
no DISPLAY variable so Tk is not available 
[1] 2
[1] 2
     [,1]
[1,]  2.5
 [1] 2.2284123 2.2378047 1.1964977 0.9432708 1.8059748 2.3870570 1.6303574
 [8] 2.5202570 0.7032441 2.7113114 1.7023114 2.7414863 2.1732236 1.5718124
[15] 2.3493425 3.6400791 3.2157438 1.9691935 1.5027929 1.9431484
 [1]  2.7771813  3.4846290  0.9536751  0.4452661  1.9816146  3.2541393
 [7]  1.4036025  3.2927857  0.2529855  3.9744493  1.4643519  4.1670429
[13]  2.3624025  1.8209725  2.8782691 11.6991019  5.3710730  1.9578152
[19]  1.4690891  2.6461325
 [1]  3.3488639  3.9471429  0.8938424  0.3712160  1.7975297  2.8805821
 [7]  1.7234324  4.0672579  0.2132002  3.4365597  1.2267182  5.0383164
[13]  2.9526064  2.0321842  3.5484347 11.8634240  4.5594646  2.4393717
[19]  1.6947381  2.5210353

MuViCP documentation built on May 1, 2019, 7:56 p.m.