weighted.stats: Weighted Mean, Variance and Standard Deviation

View source: R/weighted.stats.R

weighted.statsR Documentation

Weighted Mean, Variance and Standard Deviation

Description

Calculates the weighted mean and the variance or standard deviation for a given weighted mean.

Usage

weighted.stats(x, w, na.rm = FALSE, stats = "mean")

Arguments

x

numerical. A vector of values to compute the weighted statistics.

w

numerical. A vector of weights with the same length as x.

na.rm

logical. Should NA values be removed? Default to FALSE.

stats

character. Which weighted statistic should be returned: "mean", "var" or "sd"?

Details

Weigths should vary between 0 and 1. If they don't, they are normalized to sum one. Weights equal to 0 means that the corresponding values in x will be omitted from the from the calculation.

Author(s)

Renato A. F. de Lima

Examples


x <- c(1,3,4,8)
w <- c(0.2,0.5,0.7,0.9)
mean(x); weighted.stats(x, w)
sd(x); weighted.stats(x, w, stats = "sd")


LimaRAF/TreeCo documentation built on Sept. 25, 2024, 12:41 p.m.