10

Is there a package to convert a distance matrix to a set of coordinates? I have gone throught the below question. I was hoping there would be a package for this.

Finding the coordinates of points from distance matrix

I have considered Sammons Projection for this but from what I understand, it is an optimizer and gets you an optimum solution. I think there should be an algorithm to get a unique solution for this.

Community
  • 1
  • 1
Avinash
  • 2,521
  • 4
  • 21
  • 35
  • 1
    There is not a unique solution to this problem since you can rotate your dataset without affecting the distance matrix. – Backlin Jun 24 '13 at 09:38

2 Answers2

9

Multidimensional scaling (MDS) aims to project the distance matrix of your data to a lower dimension k, where desired k = 2 in your case, while trying to preserve the distances between data points:

# Multidimensional scaling
library(MASS)
set.seed(1)
labels <- as.factor(sample(LETTERS[1:5], 100, replace=TRUE))
dat <- mvrnorm(n=100, mu = c(1:4), Sigma=matrix(1:16, ncol=4)) + as.numeric(labels)^2
#> dim(dat)
#[1] 100   4

# Euclidean distance matrix (100x100)
d <- dist(dat)

# Classical MDS for distance matrix d
# http://en.wikipedia.org/wiki/Multidimensional_scaling
mds <- cmdscale(d, k = 2)
x <- mds[,1]
y <- mds[,2]

plot(x,y, col=rainbow(5)[as.numeric(labels)], pch=16, main="MDS for object 'dat'")
legend("topright", legend=unique(labels), col=rainbow(5)[unique(as.numeric(labels))], pch=16)

Further reading: https://stats.stackexchange.com/questions/14002/whats-the-difference-between-principal-components-analysis-and-multidimensional

mds projection

Community
  • 1
  • 1
Teemu Daniel Laajala
  • 2,316
  • 1
  • 26
  • 37
  • Thanks for a great answer. But as i commented above, is this an exact solution or just another optimizer? – Avinash Jun 24 '13 at 12:31
  • 1
    It is an eigenvalue problem (as in [this answer](http://stackoverflow.com/a/17177833/1129973)) so it can be seen as an optimization. If there is an exact solution, it returns an exact solution, and if there is no exact solution, it does not fail, but gives an approximate solution. – Vincent Zoonekynd Jun 24 '13 at 13:27
2

Look-up an algorithm called Multi-Dimensional Scaling (MDS). An implementation in R is the cmdscale function from the stats package:

Multidimensional scaling takes a set of dissimilarities and returns a set of points such that the distances between the points are approximately equal to the dissimilarities.

The documentation also has an example where a distance matrix is turned into two vectors of x and y coordinates, then plotted.

flodel
  • 87,577
  • 21
  • 185
  • 223
  • Isn't this another optimization? The algorithm defined in the other question which I linked makes sense to me. Does MDS do the same thing? As it has been pointed out, there is no unique solution but it has a solution so I don't think an approximate/optimum solution is needed when exact solution can be achieved. Unless I am completely misunderstanding MDS here then please pardon my ignorance. – Avinash Jun 24 '13 at 12:30