Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance between vectors with missing values

Tags:

r

distance

For vectors A and B, euclidean distance is:sqrt((A1-B1)^2+(A2-B2)^2+...+(An-Bn)^2)

A <- c(5, 4, 3, 2, 1, 1, 2, 3, 5)
B <- c(1, 0, 6, 4, 3, 2, 3, 1, 3)
dist(rbind(A,B), method= "euclidean") 
7.681146

How is distance calculated when vectors A and B contain missing values? Here is an example: R output for distance is 8.485281 but how is it calculated?

A <- c(5, NA, NA, NA, 1, 1, 2, 3, 5)
B <- c(1, 0, 6, NA, NA, NA, NA, 1, 3)
dist(rbind(A,B), method= "euclidean")
8.485281
like image 564
Filly Avatar asked Apr 19 '14 23:04

Filly


1 Answers

Entries with NA are first removed, then the distance is scaled up to account for the larger dimension of the full sample:

i <- is.na(A) | is.na(B)
dist(rbind(A[!i], B[!i])) * sqrt(length(A) / length(A[!i]))
#          A2
# B2 8.485281
like image 67
flodel Avatar answered Sep 21 '22 05:09

flodel