Skip to content

reorder vegdist objects

5 messages · Bede-Fazekas Ákos, Gian Maria Niccolò Benucci, Juan Antonio Balbuena

#
Dear Gian,
Let's do a small reproducible example:
library(vegan)
data(varespec)
set.seed(12345)
df_a <- t(varespec)
df_b <- t(varespec[, sample.int(ncol(varespec))]) # shuffle the original 
column order
dist_a <- vegdist(df_a); dist_b <- vegdist(df_b)
all.equal(dist_a, dist_b, check.attributes = FALSE)

Now, you have two options. If you have access to the original 
data.frame/matrix from which the dist object was created, you can 
reorder the rows of the second dataset to match that of the first one. 
Then do the distance matrix calculation again.
df_b_reordered <- df_b[rownames(df_a), ]
dist_b_reordered1 <- vegdist(df_b_reordered)
all.equal(dist_a, dist_b_reordered1, check.attributes = FALSE)

If you have no access to the original dataset and/or can not recalculate 
the distance matrix (e.g. due to computer capacity or time), you can 
reorder the distance matrix (both the rows and columns).
dist_b_reordered2 <- as.dist(as.matrix(dist_b)[attr(dist_a, "Labels"), 
attr(dist_a, "Labels")])
all.equal(dist_a, dist_b_reordered2, check.attributes = FALSE)

HTH,
?kos Bede-Fazekas
Hungarian Academy of Sciences


2019.06.01. 1:53 keltez?ssel, Gian Maria Niccol? Benucci ?rta:
2 days later
#
Hi Akos,

Thank you for the code. It worked. I reordered the dist objects as I cannot
access the dataset of one of the two distance matrix.
attr(dist_a, "Labels")])
[1] "Mean relative difference: 0.8977224"

There is any statistical test that you suggest to compare the two
distances? I would like to see if the two matrices show a similar pattern,
e.g. mantel test?

Thanks,

Gian
On Sat, 1 Jun 2019 at 00:55, Bede-Fazekas ?kos <bfalevlist at gmail.com> wrote:

            
*----- Do not print this email unless you really need to. Save paper and
protect the environment! -----*
#
Dear Gian,
I'm not a statistician but software developer... hope that other members 
of the mailing list are expert of distance matrices and can answer your 
question.
Have a nice week,
?kos

2019.06.03. 17:44 keltez?ssel, Gian Maria Niccol? Benucci ?rta:

  
  
#
Hi Gian,
A Mantel test looks right for your problem. Check also protest in vegan.
Best,

Juan A. Balbuena
--
--
Dr. Juan A. Balbuena
Cavanilles Institute of Biodiversity and Evolutionary
Biology
University of Valencia
P.O. Box 22085
46071 Valencia, Spain
e-mail: j.a.balbuena at uv.es    tel. +34 963 543 658
+++++++++++++++++++++++++++++++++++++++++++++++++++
NOTE! For shipments by EXPRESS COURIER use the
following street address:
C/ Catedr?tico Jos? Beltr?n 2, 46980 Paterna
(Valencia), Spain.
+++++++++++++++++++++++++++++++++++++++++++++++++++
3 days later
#
Thank you Juan and Akos for all your help. It worked.

Gian

On Mon, 3 Jun 2019 at 18:03, Juan Antonio Balbuena <J.A.Balbuena at uv.es>
wrote: