Compute mean, variance, covariance matrix and correlation matrix, taking into account sample weights.
meanWt
: a simple wrapper that calls mean(x, na.rm=na.rm)
if
weights
is missing and weighted.mean(x, w=weights,
na.rm=na.rm)
otherwise. Implemented methods for this generic are:
meanWt.default(x, weights, na.rm=TRUE, ...)
meanWt.dataObj(x, vars, na.rm=TRUE, ...)
varWt
: calls var(x, na.rm=na.rm)
if weights
is missing.
Implemented methods for this generic are:
varWt.default(x, weights, na.rm=TRUE, ...)
varWt.dataObj(x, vars, na.rm=TRUE, ...)
covWt
and covWt
: always remove missing values pairwise and call
cov
and cor
, respectively, if weights
is missing.
Implemented methods for these generics are:
covWt.default(x, y, weights, ...)
covWt.matrix(x, weights, ...)
covWt.data.frame(x, weights, ...)
covWt.dataObj(x, vars, ...)
corWt.default(x, y, weights, ...)
corWt.matrix(x, weights, ...)
corWt.data.frame(x, weights, ...)
corWt.dataObj(x, vars, ...)
The additional parameters are now described:
y: a numeric vector. If missing, this defaults to x
.
vars: a character vector of variable names that should be used for the calculation.
na.rm: a logical indicating whether any NA
or NaN
values
should be removed from x
before computation. Note that the default
is TRUE
.
weights: an optional numeric vector containing sample weights.
1 2 3 4 5 6 7 
x 
for 
... 
for the generic functions 
For meanWt
, the (weighted) mean.
For varWt
, the (weighted) variance.
For covWt
, the (weighted) covariance matrix or, for the default
method, the (weighted) covariance.
For corWt
, the (weighted) correlation matrix or, for the default
method, the (weighted) correlation coefficient.
meanWt
, varWt
, covWt
and corWt
all make use of
slot weights
of the input object if the dataObj
method is
used.
Stefan Kraft and Andreas Alfons
mean
, weighted.mean
,
var
, cov
,
cor
1 2 3 4 5 6 7 8 9 10  data(eusilcS)
meanWt(eusilcS$netIncome, weights=eusilcS$rb050)
sqrt(varWt(eusilcS$netIncome, weights=eusilcS$rb050))
# dataObjmethods
inp < specifyInput(data=eusilcS, hhid="db030", hhsize="hsize", strata="db040", weight="db090")
meanWt(inp, vars="netIncome")
sqrt(varWt(inp, vars="netIncome"))
corWt(inp, vars=c("age", "netIncome"))
covWt(inp, vars=c("age", "netIncome"))

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.
Please suggest features or report bugs with the GitHub issue tracker.
All documentation is copyright its authors; we didn't write any of that.