Calculate the weighted average that maximizes the maximum likelihood estimator

average(x, w = rep(1, length(x)), type = "mean", na.rm = FALSE)

Arguments

x

A numeric vector with the values to be averaged.

w

A vector of weights of the same length as x.

type

A character string, either: 'mean' or 'L2' (the default), such that a weighted mean is computed using weightedMean; or 'median' or 'L1', such that a weighted median is computed using weightedMedian.

na.rm

Should NA values be ignored? Default is na.rm = FALSE.

Value

A numeric value, representing the weighted mean or median

Examples


# Create dummy values
x = runif(10,1,100)
w = runif(10,1,10)

# Compute weighted mean
average(x, w, type = 'mean')
#> [1] 65.24238

# Compute weighted median
average(x, w, type = 'median')
#> [1] 57.9018