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.
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
$$\bar{x}_{w}=\frac{\Sigma_{i=1}^{k}x_{i}w_{i}}{\Sigma_{i=1}^{k}w_{i}}$$
where x is a numeric vector and w is a vector of weights.
The weighted variance is computed as
$$var_{w}(x)=\frac{\Sigma_{i=1}^{k}\left(x_{i}-\bar{x}_{w}\right)^{2}w_{i}}{\Sigma_{i=1}^{k}w_{i}}$$
and the unbiased weighted variance is estimated by multiplying \(var_{w}(x)\) by \(\frac{k}{k-1}\).