Skip to contents

Classical geometric mean

Usage

geomean(x, na.rm = TRUE, naValue = NA, offset = 0, ...)

Arguments

x

numeric vector containing only positive values

na.rm

logical indicating whether to ignore NA values, default TRUE. When TRUE, NA values are removed upfront, before log transformation. Note that negative values are maintained by default, by applying log transformation on the absolute value, then multiplying by the sign.

offset

numeric value added to input x prior to log transformation, intended only when values between 0 and 1 should be retained. Note that the offset makes the result slightly different than classical geometric mean.

...

additional parameters are ignored.

Value

numeric value representing the geometric mean of input values

Details

This function calculates the classical geometric mean.

The classical geometric mean is defined as the exponentiated mean of log-transformed values. Said another way, it is the nth root of the product of n numeric values. This formula is analogous to geometric distance.

The original formula does not permit negative values, however the formula used here applies log to the absolute value, then multiplies that by the sign. For example x(-4, 1/4) would be applied log2(abs(c(-4, 1/4))) * sign(c(-4, 1/4)) and thus both values would become -2. This adaptation is for convenience, assuming that a fraction 1/4 and negative value -4 represent the same underlying value, usually a fold change. A fold change may be represented either as -4`` or as 1/4“, although technically the 1/4 is not a fold change but a ratio.

Whens supplying non-negative values, specifically those where a value '0' should be considered a legitimate measured result, and therefore should be incorporated into the geomean, set argument 'offset' to some suitable offset, most common is offset=1 to mimic log2(1 + x) transformation.

Examples

x <- c(2, 4);
geomean(x);
#> [1] 2.828427

x <- c(-2, 2, 4);
geomean(x);
#> [1] 1.587401

x <- c(0, 4000, 200000);
geomean(x, offset=1);
#> [1] 927.3967
geomean(-x, offset=1);
#> [1] -927.3967