Compute the inverse centered log ratio (CLR) transform of a count matrix.
References
Van den Boogaart, K. Gerald, and Raimon Tolosana-Delgado. Analyzing compositional data with R. Vol. 122. Berlin: Springer, 2013.
Examples
# set probability of each category
prob <- c(0.1, 0.2, 0.3, 0.5)
# number of total counts
countsTotal <- 300
# number of samples
n_samples <- 5
# simulate info for each sample
info <- data.frame(Age = rgamma(n_samples, 50, 1))
rownames(info) <- paste0("sample_", 1:n_samples)
# simulate counts from multinomial
counts <- t(rmultinom(n_samples, size = countsTotal, prob = prob))
colnames(counts) <- paste0("cat_", 1:length(prob))
rownames(counts) <- paste0("sample_", 1:n_samples)
# Fractions
counts / rowSums(counts)
#> cat_1 cat_2 cat_3 cat_4
#> sample_1 0.08666667 0.2100000 0.2833333 0.4200000
#> sample_2 0.06000000 0.1966667 0.3133333 0.4300000
#> sample_3 0.09000000 0.1700000 0.2800000 0.4600000
#> sample_4 0.11333333 0.2000000 0.2500000 0.4366667
#> sample_5 0.14000000 0.1633333 0.2566667 0.4400000
# centered log ratio, with zero pseudocount
clr(counts, 0)
#> cat_1 cat_2 cat_3 cat_4
#> sample_1 -0.9119446 -0.02690638 0.2726101 0.6662408
#> sample_2 -1.2023823 -0.01521665 0.4505407 0.7670583
#> sample_3 -0.8505964 -0.21460761 0.2843836 0.7808204
#> sample_4 -0.6769871 -0.10900307 0.1141405 0.6718497
#> sample_5 -0.4763547 -0.32220402 0.1297811 0.6687776
# recover fractions from CLR transformed values
clrInv(clr(counts, 0))
#> cat_1 cat_2 cat_3 cat_4
#> sample_1 0.08666667 0.2100000 0.2833333 0.4200000
#> sample_2 0.06000000 0.1966667 0.3133333 0.4300000
#> sample_3 0.09000000 0.1700000 0.2800000 0.4600000
#> sample_4 0.11333333 0.2000000 0.2500000 0.4366667
#> sample_5 0.14000000 0.1633333 0.2566667 0.4400000