Skip to contents

Computes the variance of a weighted mean following the definitions given by Kirchner (2006).

Usage

weighted_sem(x, w, na.rm = FALSE)

Arguments

x

variable to compute the SEM for

w

weights

na.rm

should NAs be removed

Value

weighted standard error of the mean

Details

James Kirchner describes two different cases when the weighted variance is computed. The code here implements Case I where "one wants to give more weight to some points than to others, because they are considered to be more important" and "the weights differ but the uncertainties associated with the individual xi are assumed to be the same" (Kirchner, 2006, p. 1). The formula used is: SEM_w = (_i = 1^N (w_i x_i^2)-x^2)_i = 1^N w_i^21-_i = 1^N w_i^2 The expected error is within 5% of the bootstrapped SEM (at larger sample sizes).

References

Examples

set.seed(1)
n_obs <- 200
w <- runif(n_obs)
w <- w / sum(w)
x <- rnorm(n_obs, sd = 5)
weighted_sem(x, w)
#> [1] 0.3812216