# Why is my centred log ratio transformation on a distance matrix in R not symmetrical?

I am trying to transform a distance matrix from the qiime2 pathway using a centred log ratio in R. I uploaded the matrix directly from a qiime2 output file, and have converted to a data matrix and have checked that it is symmetrical.

structure(list(X1 = c(0, 0.2177609998, 0.2133361674, 0.1549136105, 0.1400395799), X11 = c(0.2177609998, 0, 0.07805820645, 0.1418994689, 0.1934668819), X12 = c(0.2133361674, 0.07805820645, 0, 0.1475390242, 0.1857477705), X13 = c(0.1549136105, 0.1418994689, 0.1475390242, 0, 0.1046740994), X14 = c(0.1400395799, 0.1934668819, 0.1857477705, 0.1046740994, 0)), row.names = c("X1", "X11", "X12", "X13", "X14"), class = "data.frame")

dm <- read.csv(file = "dm.csv", header = TRUE, row.names = 1)
isSymmetric(as.matrix(dm)) [1] TRUE
dm_matrix <- (data.matrix(dm, rownames.force = NA))


I then tried to transform the dataset using the dm_matrix_clr <- data.frame(clr(dm_matrix)) function from the 'compositions' package. This produced an output, however it was not symmetrical. From my understanding, the transformed distance matrix should be symmetric like the input matrix. The same numbers on opposing sides of the diagonal are being transformed, but are resulting in different outputs.

structure(list(X1 = c(0, 0.0457712675241166, 0.0645864307368655, -0.132814225855358, -0.169506999372974), X11 = c(0.145063977547214, 0, -0.940827956615993, -0.220562994401166, 0.153674249252163),  X12 = c(0.124535019646299, -0.980172077729657, 0, -0.181589124148918, 0.11295758523629), X13 = c(-0.195466542710371, -0.382508021279277,  -0.304190029913364, 0, -0.460580387503147), X14 = c(-0.296409056413787, -0.0725205178117472, -0.0738930607139561, -0.524830127688946, 0)), row.names = c("X1", "X11", "X12", "X13", "X14"), class = "data.frame")


Any help on how resolve this problem so I have a symmetrical transformed distance matrix would be greatly appreciated.

Just start with a matrix object and stay there.

library(compositions)
#> Welcome to compositions, a package for compositional data analysis.
#> Find an intro with "? compositions"
#>
#> Attaching package: 'compositions'
#> The following objects are masked from 'package:stats':
#>
#>     anova, cor, cov, dist, var
#> The following object is masked from 'package:graphics':
#>
#>     segments
#> The following objects are masked from 'package:base':
#>
#>     %*%, norm, scale, scale.default
d <- structure(list(X1 = c(0, 0.2177609998, 0.2133361674, 0.1549136105, 0.1400395799), X11 = c(0.2177609998, 0, 0.07805820645, 0.1418994689, 0.1934668819), X12 = c(0.2133361674, 0.07805820645, 0, 0.1475390242, 0.1857477705), X13 = c(0.1549136105, 0.1418994689, 0.1475390242, 0, 0.1046740994), X14 = c(0.1400395799, 0.1934668819, 0.1857477705, 0.1046740994, 0)), row.names = c("X1", "X11", "X12", "X13", "X14"), class = "data.frame")

m <- as.matrix(d)
isSymmetric(m)
#> [1] TRUE
clr(m)
#>              X1         X11         X12          X13        X14
#> X1   0.00000000  0.20063313  0.18010417 -0.139897392 -0.2408399
#> X11  0.39312860  0.00000000 -0.63281474 -0.035150684  0.2748368
#> X12  0.37816758 -0.62724680  0.00000000  0.009391124  0.2396881
#> X13  0.13213489  0.04438612  0.08335999  0.000000000 -0.2598810
#> X14 -0.07864311  0.24453814  0.20382147 -0.369716499  0.0000000
#> attr(,"class")
#> [1] "rmult"


Created on 2023-06-13 with reprex v2.0.2

Hi! Thanks for the help. But, that solution is still not symmetrical (qiime2 requires a symmetrical distance matrix for further analysis). For example, in your transformed matrix the X1-X11 values are 0.393 on one side on the diagonal, and 0.200 on the other. I am looking for my transformed matrix to be symmetrical like my input matrix (X1-X11 values are 0.217 on both sides of the diagonal) while still using a centred log ratio.

My bad. I thought I had done a isSymmetric(), but right, the rmult isn't. Using {coda.base} , which uses an explicit transformation matrix, I got a better of idea of what's going on however.

coda.base::clr() produces a square matrix of specified size

# Compute the transformation matrix to express a composition
#  using the linearly dependant centered log-ratio coordinates.
(B <- clr_basis(5))
#>    clr1 clr2 clr3 clr4 clr5
#> c1  0.8 -0.2 -0.2 -0.2 -0.2
#> c2 -0.2  0.8 -0.2 -0.2 -0.2
#> c3 -0.2 -0.2  0.8 -0.2 -0.2
#> c4 -0.2 -0.2 -0.2  0.8 -0.2
#> c5 -0.2 -0.2 -0.2 -0.2  0.8


When applied to m by ordinary multiplication the result is, indeed, symmetric, but when applied by matrix multiplication, %*%, the result is not symmetric because although the two matrices are conformable they are not commutable. A\times B\ne B\times A and, while it is possible for two non-commutable matrices to be symmetric, it is apparently rare.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.