Calculated a weighted generalized mean.
Arguments
- x
[numeric > 0]A strictly positive numeric vector.- weights
[numeric >= 0]A positive numeric vector of weights, the same length asx. The default is to equally weight each element ofx.- order
[numeric(1)]A finite number giving the order (or exponent) of the generalized mean. The default calculates an arithmetic mean.- na.rm
[logical(1)]Should missing values be removed? By default, missing values are not removed.
Details
The generalized mean is also called the power mean, Hölder mean, or \(l_p\) mean; see Bullen (2003, p. 175) for details.
Both x and weights are usually strictly positive
(and finite), especially for the purpose of making a price index. This is not
enforced, but the results may not make sense if the generalized mean is not
defined. There are two exceptions to this.
The convention by Hardy et al. (1952, p. 13) is used in cases where
xhas zeros: the generalized mean is 0 whenever the weights are strictly positive andorder < 0. The analogous convention holds whenever at least one element ofxisInf: the generalized mean isInfwhenever the weights are strictly positive andorder > 0.Some authors let the weights be non-negative and sum to 1. If there are zero weights then the corresponding element of
xhas no impact on the result wheneverxis strictly positive. Unlikeweighted.mean(), however, zero weights are not strong zeros, so infinite values inxwill propagate.
The weights are scaled to sum to 1 to satisfy the definition of a generalized mean.
Note
The generalized mean can be defined on the extended real line, so
that order = -Inf / Inf returns min()/max(), to agree with the
definition by Bullen (2003). This is not implemented, and the order of the
generalized mean must be finite.
References
Bullen, P. S. (2003). Handbook of Means and Their Inequalities. Springer Science+Business Media.
Hardy, G., Littlewood, J. E., and Polya, G. (1952). Inequalities (2nd edition). Cambridge University Press.
See also
Other math functions:
emean(),
nested_gmean(),
scale_weights(),
transmute_weights(),
transmute_weights2(),
update_weights()
Examples
x <- 1:3
w <- c(0.25, 0.25, 0.5)
# Arithmetic mean.
gmean(x, w)
#> [1] 2.25
# Geometric mean.
gmean(x, w, order = 0)
#> [1] 2.059767
# The Lehmer mean is a generalized mean with specific weights.
gmean(x, w * x)
#> [1] 2.555556
