From Distance Matrix to 2D coordinates

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

From Distance Matrix to 2D coordinates

Lorenzo Isella
Dear All,
I am struggling with the following problem: I am given a NxN symmetric
matrix P ( P[i,i]=0, i=1...N and P[i,j]>0 for i!=j) which stands for the
relative distances of N points.
I would like use it to get the coordinates of the N points in a 2D
plane. Of course, the solution is not unique (given one solution, I can
translate or rotate all the points by the same amount and generate
another solution), but any correct solution will do for me.
Any idea about how I can achieve that? Is there any clustering package
that can help me?
Many thanks.

Lorenzo

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: From Distance Matrix to 2D coordinates

Sarah Goslee
That's exactly what ordination is for (not clustering).

I'd try principal coordinates analysis, or non-metric multidimensional
scaling, depending on whether the dissimilarity you'v been given is
metric or nonmetric.

There are implementations of both in the ecodist package, and in
various other packages as well, so you have lots of choice.

Sarah

On Thu, Dec 15, 2011 at 1:08 PM, Lorenzo Isella
<[hidden email]> wrote:

> Dear All,
> I am struggling with the following problem: I am given a NxN symmetric
> matrix P ( P[i,i]=0, i=1...N and P[i,j]>0 for i!=j) which stands for the
> relative distances of N points.
> I would like use it to get the coordinates of the N points in a 2D plane. Of
> course, the solution is not unique (given one solution, I can translate or
> rotate all the points by the same amount and generate another solution), but
> any correct solution will do for me.
> Any idea about how I can achieve that? Is there any clustering package that
> can help me?
> Many thanks.
>
> Lorenzo
>


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

From Distance Matrix to 2D coordinates

plangfelder
In reply to this post by Lorenzo Isella
On Thu, Dec 15, 2011 at 10:08 AM, Lorenzo Isella
<[hidden email]> wrote:

> Dear All,
> I am struggling with the following problem: I am given a NxN symmetric
> matrix P ( P[i,i]=0, i=1...N and P[i,j]>0 for i!=j) which stands for the
> relative distances of N points.
> I would like use it to get the coordinates of the N points in a 2D plane. Of
> course, the solution is not unique (given one solution, I can translate or
> rotate all the points by the same amount and generate another solution), but
> any correct solution will do for me.
> Any idea about how I can achieve that? Is there any clustering package that
> can help me?
> Many thanks.

If your matrix really corresponds to distances of points (in 2
dimensions), you can try multidimensional scaling, function
cmdscale().

This little code illustrates that cmdscale recovers the 2-dimensional
points used to generate a distance matrix, up to a shift and rotation:

# Generate 10 random points in 2 dimensions
nPoints = 10;
nDim = 2;

set.seed(10);
points = matrix(runif(nPoints * nDim), nPoints, nDim);

# Their distance:
dst = dist(points)

# Classical multidimensional scaling
mds = cmdscale(dst);

# Distance of the points calculated by mds
dst2 = dist(mds);

# The two distances are equal
all.equal(as.vector(dst), as.vector(dst2))

HTH,

Peter

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: From Distance Matrix to 2D coordinates

Lorenzo Isella
In reply to this post by Lorenzo Isella
Thanks a lot!
Precisely what I had in mind.
One last question (an extension of the previous one): can this be
extended to points in 3D? Once again, given the distance matrix, can I
reconstruct a set of coordinates (among many possible) for the points in
three-dimensional space?
Cheers

Lorenzo

On 12/15/2011 07:22 PM, Peter Langfelder wrote:

> On Thu, Dec 15, 2011 at 10:08 AM, Lorenzo Isella
> <[hidden email]>  wrote:
>> Dear All,
>> I am struggling with the following problem: I am given a NxN symmetric
>> matrix P ( P[i,i]=0, i=1...N and P[i,j]>0 for i!=j) which stands for the
>> relative distances of N points.
>> I would like use it to get the coordinates of the N points in a 2D plane. Of
>> course, the solution is not unique (given one solution, I can translate or
>> rotate all the points by the same amount and generate another solution), but
>> any correct solution will do for me.
>> Any idea about how I can achieve that? Is there any clustering package that
>> can help me?
>> Many thanks.
>
> If your matrix really corresponds to distances of points (in 2
> dimensions), you can try multidimensional scaling, function
> cmdscale().
>
> This little code illustrates that cmdscale recovers the 2-dimensional
> points used to generate a distance matrix, up to a shift and rotation:
>
> # Generate 10 random points in 2 dimensions
> nPoints = 10;
> nDim = 2;
>
> set.seed(10);
> points = matrix(runif(nPoints * nDim), nPoints, nDim);
>
> # Their distance:
> dst = dist(points)
>
> # Classical multidimensional scaling
> mds = cmdscale(dst);
>
> # Distance of the points calculated by mds
> dst2 = dist(mds);
>
> # The two distances are equal
> all.equal(as.vector(dst), as.vector(dst2))
>
> HTH,
>
> Peter

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: From Distance Matrix to 2D coordinates

plangfelder
On Thu, Dec 15, 2011 at 10:47 AM, Lorenzo Isella
<[hidden email]> wrote:
> Thanks a lot!
> Precisely what I had in mind.
> One last question (an extension of the previous one): can this be extended
> to points in 3D? Once again, given the distance matrix, can I reconstruct a
> set of coordinates (among many possible) for the points in three-dimensional
> space?
> Cheers
>
> Lorenzo

Yes, you need to specify argument 'k=3' to cmdscale which instructs it
to 'scale' the input into 3 dimensions (the default is 2).

nPoints = 10;
nDim = 3;

set.seed(10);
points = matrix(runif(nPoints * nDim), nPoints, nDim);

# Their distance:
dst = dist(points)

# Classical multidimensional scaling
mds = cmdscale(dst, k=nDim);

# Distance of the points calculated by mds
dst2 = dist(mds);

# The two distances are equal
all.equal(as.vector(dst), as.vector(dst2))

HTH

Peter

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.