# What statistical analysis tests if two matrices are different?

I have two triangular 2D matrices and want to determine if their contents are statistically different.

Let's say I have a collection of 5 different species and two proteins from the 5 species. Let's call the first protein 'A' and second 'B'. I'm interested in testing if protein A is more or less similar across these species than protein B. To determine how similar A proteins are, I generated an alignment of all 5 protein A homologs (A1..A5) and calculated the proportion of amino acids that are shared between them. This generated a triangular matrix (M1) where the diagonal is 1 because A1 = A1, A2 = A2, etc. All other comparisons are between 0 (no similarity) and 1 (identical). I then did the same thing for protein B, comparing the 5 protein B homologs (B1..B5) to generate a second triangular matrix (M2).

How do I test if protein A in M1 is statistically more/less similar among species than protein B in M2. I would like to report a P-value.

The closest thing I've found to this is the Mantel test. However, it tests if M1 and M2 are correlated, not whether they are different.

Ideally, I'd like to use R to run the test.

Here are example matrices for M1 (protein A) and M2 (protein B):

M1 <- matrix(c(1, 0.9, 0.8, 0.7, 0.6,
NA, 1, 0.7, 0.6, 0.5,
NA, NA, 1, 0.1, 0.8,
NA, NA, NA, 1, 0.9,
NA, NA, NA, NA, 1),
ncol = 5)
colnames(M1) <- c("A1", "A2", "A3", "A4", "A5")
rownames(M1) <- c("A1", "A2", "A3", "A4", "A5")

A1  A2  A3  A4  A5
A1 1.0  NA  NA  NA NA
A2 0.9 1.0  NA  NA NA
A3 0.8 0.7 1.0  NA NA
A4 0.7 0.6 0.1 1.0 NA
A5 0.6 0.5 0.8 0.9  1

M2 <- matrix(c(1, 0.8, 0.7, 0.6, 0.5,
NA, 1, 0.6, 0.5, 0.4,
NA, NA, 1, 0.1, 0.7,
NA, NA, NA, 1, 0.8,
NA, NA, NA, NA, 1),
ncol = 5)
colnames(M2) <- c("B1", "B2", "B3", "B4", "B5")
rownames(M2) <- c("B1", "B2", "B3", "B4", "B5")

B1  B2  B3  B4  B5
B1 1.0  NA  NA  NA NA
B2 0.8 1.0  NA  NA NA
B3 0.7 0.6 1.0  NA NA
B4 0.6 0.5 0.1 1.0 NA
B5 0.5 0.4 0.7 0.8  1

• The problem is that these matrices are summaries of data, The "test" of whether the matries are different require uncovering the data that generated the matrices to begin with. Given that, it's just a bunch of tests for each component of the matrix, and hopefully some correction (Bonferroni) for multiple testing Commented Dec 14, 2022 at 6:40
• Thanks for your response @AdamO. Are you suggesting comparing all rows in M1 against all rows in M2 with a t-test and correcting for multiple comparisons? For instance, one t-test would compare Row A in M1 against Row A in M2? Or perhaps an ANOVA to get a single value indicating a difference between the sets of data with the variant name (A, B, C, D, etc) as a blocking variable? Commented Dec 14, 2022 at 16:22
• The test is a bit more subtle than that. Under a homoscedasticity assumption, the offdiagonal elements can be tested with an interaction model. Are you saying that you do have the data? Commented Dec 14, 2022 at 17:21
• @AdamO Presumably the underlying data are (protein) sequence data, and the entries in their matrix are similarity derived from some distance measure on those sequences. Commented Dec 14, 2022 at 17:48
• Hi @EdM. I've revised my original post. I hope it makes it clearer. Please let me know if you'd like more clarity. Commented Dec 15, 2022 at 22:29

This is outside my expertise, but here are some hints that might point you in a better direction.

The matrices of pairwise similarities don't directly demonstrate whether "protein A is more or less similar across these species than protein B." Any simple test between the 2 matrices might show whether the patterns of similarity among species differ between the two proteins, but that doesn't provide an overall similarity measure among species within each protein.

You presumably want an overall measure of protein-sequence conservation for each of the proteins among these species, which you then would compare between the two proteins. That is probably best done by multiple-sequence alignments, which might start with pairwise similarities but then go beyond that to align all sequences of a set of homologs together. The msa package in Bioconductor performs such alignments. It's possible then to generate a consensus sequence and a conservation score for each of your proteins. That package is based on ClustalW and related approaches; you might want to consider T-Coffee as an alternative. If you have a small number of sequences you won't have to worry about computation time.

You might get a rough estimate by working with the pairwise distance matrix for homologs of a protein among species, estimating a phylogenetic tree from it (for example, with neighbor-joining approaches provided by the R ape package), determining the distance estimates of each protein from a common ancestor, and summing them. Then determine the difference in these summed distances (across the same set of species) between the two proteins. For an estimate of the distribution of such differences under the null hypothesis of no difference between the proteins, the approach used in the Mantel test comes to mind: repeat the process on multiple permutations of the matrix entries.* I haven't thought through whether that solution is correct, however.

This Bioinformatics Stack Exchange page suggests further approaches. The literature on comparing phylogenetic trees should have more suggestions.

A final warning: this type of analysis is best reserved for a set of proteins that you know are orthologs directly descended from a common ancestor. Paralogs generated by gene duplication and subsequent divergent evolution will tend to show more differences. If you compare paralogs, an apparent difference between the proteins might represent your choice of sequences to compare rather than a fundamental biological difference. See this Biology Stack Exchange page and the Wikipedia entry on sequence homology.

*Permutation, at least in principle, destroys any correlation structure within an individual matrix, addressing a potential difficulty that you and @BryanKrause raised in comments about the Mantel test. Admittedly, there are situations where permutations don't address correlations adequately. My sense is that is more of a problem when the matrices measure different things, for example comparing a matrix of a biological measure against a matrix of some environmental measure in geospatial analysis. See Methods in Ecology and Evolution 2013, 4, 336–344.