Calculates weighted variance, see stats::cov.wt()
or
https://en.wikipedia.org/wiki/Sample_mean_and_covariance#Weighted_samples
for details.
weighted_var(x, w = NULL, method = c("unbiased", "ML"), na.rm = FALSE, ...)
Numeric vector.
Optional vector of non-negative case weights.
Specifies how the result is scaled. If "unbiased", the denomiator
is reduced by 1, see stats::cov.wt()
for details.
Should missing values in x
be removed? Default is FALSE
.
Further arguments passed to stats::cov.wt()
.
A length-one numeric vector.
weighted_var(1:10)
#> [1] 9.166667
weighted_var(1:10, w = NULL)
#> [1] 9.166667
weighted_var(1:10, w = rep(1, 10))
#> [1] 9.166667
weighted_var(1:10, w = 1:10)
#> [1] 6.875
weighted_var(1:10, w = 1:10, method = "ML")
#> [1] 6