weightedVar: Weighted variance and weighted standard deviation
Description
Computes a weighted variance / standard deviation of a numeric
vector or across rows or columns of a matrix.Usage
weightedVar(x, w=NULL, na.rm=FALSE, center=NULL, ...)
colWeightedVars(x, w=NULL, na.rm=FALSE, ...)
rowWeightedVars(x, w=NULL, na.rm=FALSE, ...) weightedSd(...)
colWeightedSds(x, w=NULL, na.rm=FALSE, ...)
rowWeightedSds(x, w=NULL, na.rm=FALSE, ...)
Arguments
x
a numeric vector containing the values whose
weighted variance is to be computed. w
a vector of weights the same length as x giving the weights
to use for each element of x. Negative weights are treated
as zero weights. Default value is equal weight to all values.
na.rm
a logical value indicating whether NA values in
x should be stripped before the computation proceeds,
or not. If NA, no che center
Optional numeric scalar specifying the center
location of the data. If NULL, it is estimated from data. Missing values
Missing values are dropped at the very beginning, if argument
na.rm is TRUE, otherwise not.Weighted variance
The weights used by the weighted variance (and standard deviation)
estimator should be consider so called frequency weights such
that weightedVar(c(2,4,5), w=c(2,1,3)) == var(c(2, 2, 4, 5, 5, 5)).
Note that this means that the estimate is not invariant
to a scale factor on the weights, e.g. passing normalized weights
will not give the same estimate as non-normalized weights.See Also
For the non-weighted variance, see var.