Learn R Programming

simPop (version 0.2.15)

utils: Weighted mean, variance, covariance matrix and correlation matrix

Description

Compute mean, variance, covariance matrix and correlation matrix, taking into account sample weights.

Usage

meanWt(x, ...)

## S3 method for class 'default': meanWt(x, weights, na.rm = TRUE, \ldots)

## S3 method for class 'dataObj': meanWt(x, vars, na.rm = TRUE, \ldots)

varWt(x, ...)

## S3 method for class 'default': varWt(x, weights, na.rm = TRUE, \ldots)

## S3 method for class 'dataObj': varWt(x, vars, na.rm = TRUE, \ldots)

covWt(x, ...)

## S3 method for class 'default': covWt(x, y, weights, \dots)

## S3 method for class 'matrix': covWt(x, weights, \dots)

## S3 method for class 'data.frame': covWt(x, weights, \dots)

## S3 method for class 'dataObj': covWt(x, vars, \dots)

corWt(x, ...)

## S3 method for class 'default': corWt(x, y, weights, \dots)

## S3 method for class 'matrix': corWt(x, weights, \dots)

## S3 method for class 'data.frame': corWt(x, weights, \dots)

## S3 method for class 'dataObj': corWt(x, vars, \dots)

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
y
a numeric vector. If missing, this defaults to x.
vars
a character vector of variable names that should be used for the calculation.
weights
an optional numeric vector containing sample weights.
na.rm
a logical indicating whether any NA or NaN values should be removed from x before computation. Note that the default is TRUE.
...
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.

Details

meanWt is 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.

varWt calls var(x, na.rm=na.rm) if weights is missing.

covWt and corWt always remove missing values pairwise and call cov and cor, respectively, if weights is missing.

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

See Also

mean, weighted.mean, var, cov, cor

Examples

Run this code
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"))

Run the code above in your browser using DataLab