jamGeomean: Modified geometric mean for positive and negative values

jamGeomeanR Documentation

Modified geometric mean for positive and negative values

Description

Modified geometric mean for positive and negative values

Usage

jamGeomean(x, na.rm = TRUE, ...)

Arguments

x

numeric vector which may contain positive and negative values.

na.rm

logical indicating whether to ignore NA values.

...

additional parameters are ignored.

Details

This function calculates a geometric mean using a formula which tolerates positive and negative values. It also tolerates zeros without resulting in zero. The intent is to provide a mean summary value which closely models the classical geometric mean, while retaining information present in vectors that contain either zeros or negative values.

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 formula does not allow negative values, however, and if any value is zero the result is also zero.

There are several proposed alternatives to address negative numbers, or zeros. This function makes the following adjustments:

  • Add 1 to absolute value of the input, so the numeric sign is not flipped during log transformation: i <- log2(1+ abs(x))

  • Multiply that vector by the sign(x) to retain the original positive and negative directionality: j <- i * sign(x)

  • Take the mean: k <- mean(j)

  • Exponentiate the absolute value: m <- 2^(abs(k))

  • Multiply by the sign: n <- m * sign(k)

  • Subtract 1: o <- n - 1;

The properties of the calculation:

  • Symmetry around zero, for example jamGeomean(x) = -jamGeomean(-x)

  • Results are slightly different than classical geometric mean values, as a result of adding 1 prior to log transformation. The difference is larger with increasing range(x) and is most noticeable when one input value in x is zero.

Value

numeric value representing the modified geometric mean of input values.

See Also

Other jam numeric functions: geomean()

Examples

x <- c(1, 10, 40);
jamGeomean(x);
# compare to classical geometric mean
geomean(x);

# Positive and negative values should offset
x <- c(-20, 20);
jamGeomean(x);

x <- c(-20,10,40);
jamGeomean(x);


jmw86069/jambio documentation built on Nov. 29, 2022, 11:08 a.m.