Learn R Programming

fastmatrix (version 0.5-7721)

cov.weighted: Weighted covariance matrices

Description

Returns a list containing estimates of the weighted mean and covariance matrix of the data.

Usage

cov.weighted(x, weights = rep(1, nrow(x)))

Value

A list containing the following named components:

mean

an estimate for the center (mean) of the data.

cov

the estimated (weighted) covariance matrix.

Arguments

x

a matrix or data frame. As usual, rows are observations and columns are variables.

weights

a non-negative and non-zero vector of weights for each observation. Its length must equal the number of rows of x.

Details

The covariance matrix is divided by the number of observations, which arise for instance, when we use the class of elliptical contoured distributions. Thus, $$W_n = \sum\limits_{i=1}^n w_i, \qquad \overline{\bold{x}}_n = \frac{1}{W_n} \sum\limits_{i=1}^n w_i\bold{x}_i \qquad \bold{S}_n = \frac{1}{n} \sum\limits_{i=1}^n w_i (\bold{x}_i - \overline{\bold{x}}_n)(\bold{x}_i - \overline{\bold{x}}_n)^T.$$ This differs from the behaviour of function cov.wt.

References

Clarke, M.R.B. (1971). Algorithm AS 41: Updating the sample mean and dispersion matrix. Applied Statistics 20, 206-209.

See Also

cov.wt, cov and var.

Examples

Run this code
x <- cbind(1:10, c(1:3, 8:5, 8:10))
z0 <- cov.weighted(x) # all weights are 1
D2 <- Mahalanobis(x, center = z0$mean, cov = z0$cov)
p <- ncol(x)
wts <- (p + 1) / (1 + D2) # nice weights!
z1 <- cov.weighted(x, weights = wts)
z1

Run the code above in your browser using DataLab