# Methods for comparing two dissimilarity matrices

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.

• you want to compare elements from the same group (matrix) and then assess the smaller/larger comparison? Feb 12, 2019 at 19:12
• @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 Feb 13, 2019 at 11:12
• 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. Feb 13, 2019 at 12:01
• @LucasFarias Thank you for this insight. Do you have any suggestions for which methods to apply here? Feb 13, 2019 at 12:06

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:

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):

# DATAFRAME FROM MATRICES
data_aux1 <- as.data.frame(as.vector(M1))
data_aux1$$group <- 1 colnames(data_aux1)=c("obs","group") data_aux2 <- as.data.frame(as.vector(M2)) data_aux2$$group <- 2
colnames(data_aux2)=c("obs","group")
data <- rbind(data_aux1, data_aux2)

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

# SUMMARY OF RESULTS
summary(anova_test)


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.