R/wtd_variance.R

Defines functions weighted_variance

weighted_variance <- function(x, w) {
  if (is.null(w)) w <- rep(1, length(x))

  x[is.na(w)] <- NA
  w[is.na(x)] <- NA

  w <- stats::na.omit(w)
  x <- stats::na.omit(x)

  xbar <- sum(w * x) / sum(w)
  sum(w * ((x - xbar)^2)) / (sum(w) - 1)
}

Try the sjstats package in your browser

Any scripts or data that you put into this service are public.

sjstats documentation built on May 29, 2024, 12:09 p.m.