Title: Weighted Descriptive Statistics
Version: 0.1.1
Description: Weighted versions of common descriptive statistics (variance, standard deviation, covariance, correlation, quantiles).
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.3.3
Imports: glue, rlang
Suggests: pkgdown, testthat (≥ 3.0.0)
Config/testthat/edition: 3
LinkingTo: cpp11
NeedsCompilation: yes
Packaged: 2026-03-07 21:51:04 UTC; jgaeb
Author: Johann D. Gaebler [aut, cre]
Maintainer: Johann D. Gaebler <me@jgaeb.com>
Repository: CRAN
Date/Publication: 2026-03-12 08:20:02 UTC

wstats: Weighted Descriptive Statistics

Description

Weighted versions of common descriptive statistics (variance, standard deviation, covariance, correlation, quantiles).

Author(s)

Maintainer: Johann D. Gaebler me@jgaeb.com


Weighted correlation

Description

Weighted correlation coefficient, computed as weighted_cov(x, y, w) / (weighted_sd(x, w) * weighted_sd(y, w)).

Usage

weighted_cor(x, y, w, na.rm = FALSE)

Arguments

x

A numeric vector of observations.

y

A numeric vector of observations (same length as x).

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, observations with any NA in x, y, or w are removed before computation.

Value

A single numeric value in [-1, 1].


Weighted covariance

Description

Computes the population (importance-weight) weighted covariance sum(w_hat * (x - mu_x) * (y - mu_y)).

Usage

weighted_cov(x, y, w, na.rm = FALSE)

Arguments

x

A numeric vector of observations.

y

A numeric vector of observations (same length as x).

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, observations with any NA in x, y, or w are removed before computation.

Value

A single numeric value.


Weighted excess kurtosis

Description

Computes the population weighted excess kurtosis: sum(w_hat * ((x - mu) / sigma)^4) - 3.

Usage

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

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, paired NAs in x and w are removed before computation. If FALSE (default) and any NA is present, NA is returned.

Value

A single numeric value.


Weighted median absolute deviation

Description

Computes the weighted MAD as the weighted median of ⁠|x - median(x, w)|⁠, scaled by constant (default 1.4826 for consistency with stats::mad() under normality).

Usage

weighted_mad(x, w, na.rm = FALSE, constant = 1.4826)

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, paired NAs in x and w are removed before computation. If FALSE (default) and any NA is present, NA is returned.

constant

Scale factor (default 1.4826).

Value

A single numeric value.


Weighted median

Description

Convenience wrapper: weighted_quantile(x, w, 0.5, na.rm).

Usage

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

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, paired NAs in x and w are removed before computation. If FALSE (default) and any NA is present, NA is returned.

Value

A single numeric value.


Weighted quantiles

Description

Computes weighted quantiles using a type-7 analog: observations are placed at the midpoints of their weight intervals in the cumulative weight distribution, rescaled to [0, 1], and quantiles are obtained by linear interpolation. For equal weights this matches quantile(x, type = 7).

Usage

weighted_quantile(x, w, probs = seq(0, 1, 0.25), na.rm = FALSE)

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

probs

A numeric vector of probabilities in [0, 1].

na.rm

Logical. If TRUE, paired NAs in x and w are removed.

Value

A numeric vector of the same length as probs.


Weighted standard deviation

Description

Square root of weighted_var().

Usage

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

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, paired NAs in x and w are removed before computation. If FALSE (default) and any NA is present, NA is returned.

Value

A single numeric value.


Weighted skewness

Description

Computes the population weighted skewness (Fisher's g1): sum(w_hat * ((x - mu) / sigma)^3).

Usage

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

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, paired NAs in x and w are removed before computation. If FALSE (default) and any NA is present, NA is returned.

Value

A single numeric value.


Weighted variance

Description

Computes the population (importance-weight) weighted variance sum(w_hat * (x - mu)^2) where w_hat = w / sum(w) and mu = weighted.mean(x, w).

Usage

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

Arguments

x

A numeric vector of observations.

w

A numeric vector of non-negative weights (need not sum to 1).

na.rm

Logical. If TRUE, paired NAs in x and w are removed before computation. If FALSE (default) and any NA is present, NA is returned.

Value

A single numeric value.