Weighted mean, variance, covariance matrix and correlation matrix

Share:

Description

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.

Usage

1
2
3
4
5
6
7
meanWt(x, ...)

varWt(x, ...)

covWt(x, ...)

corWt(x, ...)

Arguments

x

for meanWt and varWt, a numeric vector or an object of class dataObj. For covWt and corWt, a numeric vector, matrix, data.frame or dataObj. In case of a dataObj, weights are automatically used from the S4-object itself.

...

for the generic functions covWt and corWt, additional arguments to be passed to methods. Additional arguments not included in the definition of the methods are ignored.

Value

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.

Note

meanWt, varWt, covWt and corWt all make use of slot weights of the input object if the dataObj-method is used.

Author(s)

Stefan Kraft and Andreas Alfons

See Also

mean, weighted.mean, var, cov, cor

Examples

 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))

# dataObj-methods
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"))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.