I have run cgMLST on E. coli isolates, then i ran the resulting allele labels through the "daisy" function in R with the "gower" setting to generate a dissimilarity matrix.

I have subsets of isolates isolated from different sources. What I want to do is to compare two dissimilarity matrices and see if the isolates in matrix1 is more similar to themselves (i.e. less distance from one another) than the isolates in matrix2.In other words, I'd like to see if the distances in matrix 1 is closer to 0 or 1 than the other matrix.

However, since there are more isolates in matrix1 than matrix2, they are not of the same size, thus the mantel test does not work. Is there any way of doing a similar comparison for two matrices that aren't the same size?

Example data:

Matrix 1:

structure(c(0, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0, 0.8, 0.8, 0.8, 
0.6, 0.8, 0.8, 0, 0.6, 0.6, 0.8, 0.8, 0.8, 0.6, 0, 0, 0.8, 0.8, 
0.8, 0.6, 0, 0, 0.8, 0.8, 0.6, 0.8, 0.8, 0.8, 0), .Dim = c(6L, 
6L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), c("1", 
"2", "3", "4", "5", "6")))

Matrix 2:

structure(c(0, 0.2, 0.5, 0.9, 0.9, 0.2, 0, 0.5, 0.9, 0.9, 0.5, 
0.5, 0, 0.9, 0.9, 0.9, 0.9, 0.9, 0, 0.8, 0.9, 0.9, 0.9, 0.8, 
0), .Dim = c(5L, 5L), .Dimnames = list(c("1", "2", "3", "4", 
"5"), c("1", "2", "3", "4", "5")))

It would be preferable if it was possible to do in R.

  • $\begingroup$ you want to compare elements from the same group (matrix) and then assess the smaller/larger comparison? $\endgroup$ Feb 12, 2019 at 19:12
  • $\begingroup$ @LucasFarias I want to compare the distances in each matrix and find out if the first one is closer to 0 or 1 than the other matrix $\endgroup$
    – Haakonkas
    Feb 13, 2019 at 11:12
  • $\begingroup$ So I believe you should not be looking for a metric between matrices, but one for within a matrix. As long as it controls for the dimensionality, you'd be good to compare such metric for two different sized matrix. $\endgroup$ Feb 13, 2019 at 12:01
  • $\begingroup$ @LucasFarias Thank you for this insight. Do you have any suggestions for which methods to apply here? $\endgroup$
    – Haakonkas
    Feb 13, 2019 at 12:06

1 Answer 1


Given the data and the description of your problem, I believe an ANOVA test could answer the question you have at hand.

You have data from two groups (matrices $A$ and $B$), and want to know whether their variability observed comes from their respective groups (variance within groups) or from the difference in the groups themselves (variability between groups). This setup fits what ANOVA is designed to assess.

There's a caveat though: ANOVA assumes the data fit the normal distribution, and you do not have many data points to count on that. The histograms for the data in each matrix also doesn't support the claim:

            enter image description here

ANOVA is robust you might read somewhere, but there is criticism against using it in this setup.

Still, this is the only approach I can think of for your problem. For the test, after loading your matrices 1 and 2 (M1 and M2, respectively):

data_aux1 <- as.data.frame(as.vector(M1))
data_aux1$group <- 1
data_aux2 <- as.data.frame(as.vector(M2))
data_aux2$group <- 2
data <- rbind(data_aux1, data_aux2)

anova_test <- aov(obs ~ group, data = data)


For which the output is:

            Df Sum Sq Mean Sq F value Pr(>F)
group        1  0.000 0.00014   0.001  0.973
Residuals   59  7.074 0.11990 

Suggesting there is no significant differences between the groups (i.e. p-value > 0.05). Hence, the dissimilarity values are statistically equivalent in both matrices.

Here I make available the whole code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.