-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDistance.R
97 lines (75 loc) · 2.9 KB
/
Distance.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#Distance measure for two vector(x,y)
#-------------------------------------------------------------------#
#Euclidean Distance
#-------------------------------------------------------------------#
euclidean <- function(x,y){
sqrt(sum((x-y)^2,na.rm = TRUE))
}
#-------------------------------------------------------------------#
#Manhattan Distance
#-------------------------------------------------------------------#
manhattan <- function(x){
sum(abs(x1 - x2),na.rm = TRUE)
}
#-------------------------------------------------------------------#
#Chebychev Distance
#-------------------------------------------------------------------#
chebychev <- function(x,y){
sum(max(abs(x - y),na.rm = TRUE))
}
#-------------------------------------------------------------------#
#Canberra Distance
#-------------------------------------------------------------------#
canberra <- function(x,y){
sum(abs(x - y)/(abs(x) + abs(y)),na.rm = TRUE)
}
#-------------------------------------------------------------------#
#Hamming Distance
#-------------------------------------------------------------------#
hamming <- function(x,y){
length(which(x != y))/length(x)
}
#-------------------------------------------------------------------#
#Braycurtis Distance
#-------------------------------------------------------------------#
braycurtis <- function(x,y){
index <- c(which(is.na(x)),which(is.na(y)))
x <- x[-index]
y <- y[-index]
sum(abs(x - y))/(sum(abs(x)) + sum(abs(y)))
}
#-------------------------------------------------------------------#
#Minkowki Distance
#p = 1 ~ manhattan
#p = 2 ~ euclidean
#-------------------------------------------------------------------#
minkowski <- function(x,y,p){
sum(abs(x - y)^p,na.rm = TRUE)^(1/p)
}
#-------------------------------------------------------------------#
#Weighted Minkowski Distance
#-------------------------------------------------------------------#
wminkowski <- function(x,y,p,w){
sum((w * abs(x - y)^p),na.rm = TRUE)^(1/p)
}
#-------------------------------------------------------------------#
#Cosine Distance
#-------------------------------------------------------------------#
cosine <- function(x,y){
index <- c(which(is.na(x)),which(is.na(y)))
x <- x[-index]
y <- y[-index]
1- (sum(x1*x2))/(sqrt(sum(x1^2))*sqrt(sum(x2^2)))
}
#-------------------------------------------------------------------#
#Mean square error
#-------------------------------------------------------------------#
meansqerror <- function(x,y){
sum((x - y)^2,na.rm = TRUE)/length(which(!is.na(x) & !is.na(y)))
}
#-------------------------------------------------------------------#
#Mean absolute error
#-------------------------------------------------------------------#
meanabserror <- function(x,y){
sum(abs(x - y),na.rm = TRUE)/length(which(!is.na(x) & !is.na(y)))
}