Learn R Programming

psychmeta (version 2.6.4)

wt_dist: Weighted descriptive statistics for a vector of numbers

Description

Compute the weighted mean and variance of a vector of numeric values. If no weights are supplied, defaults to computing the unweighted mean and the unweighted maximum-likelihood variance.

Usage

wt_dist(
  x,
  wt = rep(1, length(x)),
  unbiased = TRUE,
  df_type = c("count", "sum_wts")
)

wt_mean(x, wt = rep(1, length(x)))

wt_var( x, wt = rep(1, length(x)), unbiased = TRUE, df_type = c("count", "sum_wts") )

Value

A weighted mean and variance if weights are supplied or an unweighted mean and variance if weights are not supplied.

Arguments

x

Vector of values to be analyzed.

wt

Weights associated with the values in x.

unbiased

Logical scalar determining whether variance should be unbiased (TRUE) or maximum-likelihood (FALSE).

df_type

Character scalar determining whether the degrees of freedom for unbiased estimates should be based on numbers of cases ("count"; default) or sums of weights ("sum_wts").

Details

The weighted mean is computed as x_w=_i=1^kx_iw_i_i=1^kw_isum(x * wt) / sum(wt) where x is a numeric vector and w is a vector of weights.

The weighted variance is computed as var_w(x)=_i=1^k(x_i-x_w)^2w_i_i=1^kw_ivar(x) = sum((x - sum(x * wt) / sum(wt))^2 * wt) / sum(wt) and the unbiased weighted variance is estimated by multiplying var_w(x)var(x) by kk-1k/(k-1).

Examples

Run this code
wt_dist(x = c(.1, .3, .5), wt = c(100, 200, 300))
wt_mean(x = c(.1, .3, .5), wt = c(100, 200, 300))
wt_var(x = c(.1, .3, .5), wt = c(100, 200, 300))

Run the code above in your browser using DataLab