harmonic_mean: Harmonic Mean

harmonic_meanR Documentation

Harmonic Mean

Description

The harmonic mean is the reciprocal of the arithmetic mean of the reciprocals of the input values. Common uses include computing the mean of ratios, for instance the average P/E ratio in a portfolio. Also it is the correct mean for averaging speeds weighted for distance.

The harmonic mean is calculated using the following formula:

H = \frac{n}{\sum_{i=1}^{n} \frac{1}{x_i}}

Usage

harmonic_mean(x, weights = NULL, na.rm = c(TRUE, FALSE))

Arguments

x

A vector of values to take the harmonic mean for

weights

Optional vector of weights, one per value in x.

na.rm

Remove NAs (default = TRUE). With weights, cases with NA in x or weights are dropped together so the two stay aligned. If FALSE, NA input returns NA.

Value

  • Harmonic mean of x

References

https://en.wikipedia.org/wiki/Harmonic_mean

See Also

  • geometric_mean(), aggregate()

Other Miscellaneous Stats Functions: FishersMethod(), SE_from_p(), geometric_mean(), oddsratio(), reliability(), umx, umxCov2cor(), umxHetCor(), umxParan(), umxWeightedAIC(), umx_apply(), umx_cor(), umx_means(), umx_r_test(), umx_round(), umx_scale(), umx_var()

Examples

# Harmonic means are suitable for ratios
tmp = c(33/1, 23/1)
harmonic_mean(tmp)

geometric_mean(tmp)
mean(tmp)

# Example with weights
harmonic_mean(c(33/1, 23/1), weights= c(.2, .8))

# If Jack travels outbound at 1 mph, and returns at 10 miles an hour, what is his average speed?
harmonic_mean(c(1,10)) # 1.81 mph

# Missing data are dropped by default (na.rm = TRUE)
harmonic_mean(c(33, 23, NA)) # 27.1
harmonic_mean(c(33, 23, NA), na.rm = FALSE) # NA
# With weights, incomplete cases are dropped together, keeping x and weights aligned
harmonic_mean(c(33, NA, 23), weights = c(.2, .3, .5)) # 25.18
harmonic_mean(c(33, 23), weights = c(.2, NA)) # 33
## Not run: 
# weights must match x in length; non-positive input is an error
harmonic_mean(c(33, 23), weights = c(.2, .3, .5))
harmonic_mean(c(33, -23))

## End(Not run)


umx documentation built on Oct. 11, 2026, 5:07 p.m.