R/weighted_covariance.R

Defines functions weighted.cov

Documented in weighted.cov

#' Weighted bivariate covariance
#' 
#' @description Computes the weighted covariance coefficient of two numeric vectors.
#' @param x (numeric vector) Variable of interest x for computing the covariance
#' @param y (numeric vector) Variable of interest y for computing the covariance
#' @param w (numeric vector) Vector with the weights
#' @param na.rm (bool) if TRUE: Excludes missing values before computing the covariance
#'
#' @return Scalar containing the covariance
#' @export
#'
#' @examples
#' data(df_talis18) 
#' 
#' weighted.cov(x = df_talis18$t3stake, y = df_talis18$t3team, w = df_talis18$tchwgt)
weighted.cov <- function(x, y, w, na.rm = TRUE) {
  # Goal: Get weighted covariance
  # ------ INPUTS ------.
  # x: (vector) variable from where to get correlation
  # y: (vector) variable from where to get correlation
  # w : (vector) vector of weights
  
  # Use non NAs in both x and y
  use <- !is.na(x) & !is.na(y)
  x <- x[use]
  y <- y[use]
  w <- w[use]
  
  # Covariance
  w.mean <- weighted.mean(x, w, na.rm = na.rm)
  cov.w <- sum(w * (x - w.mean) * (y - w.mean), na.rm = na.rm)/sum(w, na.rm = na.rm)
  return(cov.w)
}

Try the Rrepest package in your browser

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

Rrepest documentation built on April 4, 2025, 2:07 a.m.