Compute the centered log ratio (CLR) transform of a count matrix.
Details
The CLR of a vector x
of counts in D
categories is defined as
clr(x) = log(x) - mean(log(x))
. For details see van den Boogaart and Tolosana-Delgado (2013).
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)
# centered log ratio
clr(counts)
#> cat_1 cat_2 cat_3 cat_4
#> sample_1 -0.8671869 -0.2423589 0.3158128 0.7937330
#> sample_2 -0.8124421 -0.2015330 0.2382568 0.7757184
#> sample_3 -0.8696391 -0.1320401 0.4362337 0.5654455
#> sample_4 -0.9686262 -0.2236539 0.4015821 0.7906980
#> sample_5 -0.9667979 -0.2218256 0.3815515 0.8070720