Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coordinates from distance matrix in R

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.

like image 918
Avinash Avatar asked Jun 24 '13 08:06

Avinash


2 Answers

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

like image 128
Teemu Daniel Laajala Avatar answered Oct 20 '22 01:10

Teemu Daniel Laajala


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.

like image 22
flodel Avatar answered Oct 19 '22 23:10

flodel