trimmed.weighted.mean: Weighted trimmed mean

View source: R/helpers.R

trimmed.weighted.meanR Documentation

Weighted trimmed mean

Description

Compute a weighted trimmed mean, i.e. a mean that assigns non-negative weights to the observations and (2) discards an equal share of total weight from each tail of the distribution before averaging.

Usage

trimmed.weighted.mean(x, trim = 0, w = NULL, na.rm = FALSE, ...)

Arguments

x

Numeric vector of data values.

trim

Single number in [0,\,0.5]. Fraction of the total weight to cut from each tail.

w

Numeric vector of non-negative weights of the same length as 'x'. If 'NULL' (default), equal weights are used.

na.rm

Logical: should 'NA' values in 'x' or 'w' be removed?

...

Further arguments passed to ['weighted.mean()'] (for compatibility).

Details

For example, 'trim = 0.10' removes 10 from the right (20 Setting 'trim = 0.5' returns the weighted median.

The algorithm follows these steps:

  1. Sort the data by 'x' and accumulate the corresponding weights.

  2. Identify the lower and upper cut-points that mark the central share of the total weight.

  3. Drop observations whose cumulative weight lies entirely outside the cut-points and proportionally down-weight the two (at most) remaining outermost observations.

  4. Return the weighted mean of the retained mass. If 'trim == 0.5', only the 50

Value

A single numeric value: the trimmed weighted mean of 'x'. Returns 'NA_real_' if no non-'NA' observations remain after optional 'na.rm' handling.

See Also

['mean()'] for the unweighted trimmed mean, ['weighted.mean()'] for the untrimmed weighted mean.

Examples

set.seed(1)
z <- rt(100, df = 3)
w <- pmin(1, 1 / abs(z)^2)  # Far-away observations tails get lower weight

mean(z, trim = 0.20)  # Ordinary trimmed mean
trimmed.weighted.mean(z, trim = 0.20)  # Same

weighted.mean(z, w)   # Ordinary weighted mean (no trimming)
trimmed.weighted.mean(z, w = w)  # Same

trimmed.weighted.mean(z, trim = 0.20, w = w)  # Weighted trimmed mean
trimmed.weighted.mean(z, trim = 0.5,  w = w)  # Weighted median

smoothemplik documentation built on Aug. 22, 2025, 1:11 a.m.