Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

likely error in CLR formula in decostand docs #621

Open
handibles opened this issue Feb 21, 2024 · 5 comments
Open

likely error in CLR formula in decostand docs #621

handibles opened this issue Feb 21, 2024 · 5 comments

Comments

@handibles
Copy link

Dear Devs,

Thanks as always for the work. I was using decostand as a quick look up for the CLR formula, but think it's wrong in the vegan docs.

While the code for CLR (.calc_clr) is fine - mean of logs :

means <- rowMeans(clog)

The docs indicate that the formula here is $log(x) - log(u)$, where $x$ are the beasties and $u$ is the beastie mean, i.e. log of means.

Sadly,
> mean(log(1:10)) == log(mean(1:10))
> [1] FALSE

Might save the next veganner from a mishap. All the best,
CH

@jarioksa
Copy link
Contributor

@antagomir Have you looked at this?

@antagomir
Copy link
Contributor

Woops, yes. This we should be able to solve asap. I will have a look today/tomorrow and open a PR. The broader rCLR improvements might take more time.

@antagomir
Copy link
Contributor

antagomir commented Apr 24, 2024

I think the issue confused arithmetic and geometric mean, and the documentation is correct.

The documentation states that

clr = log(x/g(x)) = log x - log (g(x)), where g(x) denotes the geometric mean.

This is how CLR transformation is formally defined.

Log of geometric mean can be written as:
log (g(x)) = log ((x1 * ... * xn)^(1/n)) = (1/n) * (log(x1) + ... + log(xn)) = mean(log(x))

Thus, log(g(x)) = mean(log(x)), where g(x) is geometric mean.

This is also seen with:

gm_mean = function(a){prod(a)^(1/length(a))}; log(gm_mean(1:10))
[1] 1.510441

mean(log(1:10))
[1] 1.510441

I am not sure whether the documentation could/should be improved since it already states that g(x) is the geometric mean, and this is how CLR definition is written in most sources afaik.

As far as I can see the documentation is correct and this issue could be closed unless there are further suggestions on how to improve.

@handibles
Copy link
Author

handibles commented Apr 29, 2024

Thanks @antagomir. I've yet to find a solid mnemonic for the order in the CLR transform (hence the OP), but if I've got this correctly:

Definition of the CLR (as above):
clr = log(x/g(x)) = log x - log(g(x))
i.e.,
clr = log(x/g(x)) = log x - mean(log(x))

Definition in the decostand documentation (latex'd formula):
$clr = log(x) - log(u)$
, where u is the arithmetic mean :

Is that not incorrect?...

@antagomir
Copy link
Contributor

Thanks @handibles - yes - that seems incorrect.

Looking at the current master branch of the man pages in github:vegandevs/vegan, lines 94-109 (clr documentation) refers to the geometric mean. So at least that manpage seems to be correct.

Where did you find that incorrect formula exactly - could you point me to the exact source? I will correct asap if we have a mistake anywhere. But right now I am not able to trace this..!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants