x <- 1:3
w <- c(0.25, 0.25, 0.5)
#---- Common generalized means ----
# Arithmetic mean
arithmetic_mean(x, w) # same as weighted.mean(x, w)
# Geometric mean
geometric_mean(x, w) # same as prod(x^w)
# Harmonic mean
harmonic_mean(x, w) # same as 1 / weighted.mean(1 / x, w)
# Quadratic mean / root mean square
generalized_mean(2)(x, w)
# Cubic mean
# Notice that this is larger than the other means so far because
# the generalized mean is increasing in r
generalized_mean(3)(x, w)
#---- Comparing the Pythagorean means ----
# The dispersion between the arithmetic, geometric, and harmonic
# mean usually increases as the variance of 'x' increases
x <- c(1, 3, 5)
y <- c(2, 3, 4)
var(x) > var(y)
arithmetic_mean(x) - geometric_mean(x)
arithmetic_mean(y) - geometric_mean(y)
geometric_mean(x) - harmonic_mean(x)
geometric_mean(y) - harmonic_mean(y)
# But the dispersion between these means is only bounded by the
# variance (Bullen, 2003, p. 156)
arithmetic_mean(x) - geometric_mean(x) >= 2 / 3 * var(x) / (2 * max(x))
arithmetic_mean(x) - geometric_mean(x) <= 2 / 3 * var(x) / (2 * min(x))
# Example by Lord (2002) where the dispersion decreases as the variance
# increases, counter to the claims by Fisher (1922, p. 108) and the
# CPI manual (par. 1.14)
x <- (5 + c(sqrt(5), -sqrt(5), -3)) / 4
y <- (16 + c(7 * sqrt(2), -7 * sqrt(2), 0)) / 16
var(x) > var(y)
arithmetic_mean(x) - geometric_mean(x)
arithmetic_mean(y) - geometric_mean(y)
geometric_mean(x) - harmonic_mean(x)
geometric_mean(y) - harmonic_mean(y)
# The "bias" in the arithmetic and harmonic indexes is also smaller in
# this case, counter to the claim by Fisher (1922, p. 108)
arithmetic_mean(x) * arithmetic_mean(1 / x) - 1
arithmetic_mean(y) * arithmetic_mean(1 / y) - 1
harmonic_mean(x) * harmonic_mean(1 / x) - 1
harmonic_mean(y) * harmonic_mean(1 / y) - 1
#---- Missing values ----
w[2] <- NA
arithmetic_mean(x, w, na.rm = TRUE) # drop the second observation
weighted.mean(x, w, na.rm = TRUE) # still returns NA
Run the code above in your browser using DataLab