weightedVar | R Documentation |
Computes a weighted variance / standard deviation of a numeric vector or across rows or columns of a matrix.
weightedVar(x, w = NULL, idxs = NULL, na.rm = FALSE, center = NULL,
...)
weightedSd(...)
rowWeightedVars(x, w = NULL, rows = NULL, cols = NULL, na.rm = FALSE,
..., useNames = TRUE)
colWeightedVars(x, w = NULL, rows = NULL, cols = NULL, na.rm = FALSE,
..., useNames = TRUE)
rowWeightedSds(x, w = NULL, rows = NULL, cols = NULL, na.rm = FALSE,
..., useNames = TRUE)
colWeightedSds(x, w = NULL, rows = NULL, cols = NULL, na.rm = FALSE,
..., useNames = TRUE)
x |
|
w |
a vector of weights the same length as |
idxs |
A |
na.rm |
If |
center |
Optional |
... |
Not used. |
rows |
A |
cols |
A |
useNames |
If |
The estimator used here is the same as the one used by the "unbiased"
estimator of the Hmisc package. More specifically,
weightedVar(x, w = w) == Hmisc::wtd.var(x, weights = w)
,
Returns a numeric
scalar.
This function handles missing values consistently with
weightedMean
().
More precisely, if na.rm = FALSE
, then any missing values in either
x
or w
will give result NA_real_
.
If na.rm = TRUE
, then all (x, w)
data points for which
x
is missing are skipped. Note that if both x
and w
are missing for a data points, then it is also skipped (by the same rule).
However, if only w
is missing, then the final results will always
be NA_real_
regardless of na.rm
.
Henrik Bengtsson
For the non-weighted variance, see var
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.