Learn R Programming

fastmatrix (version 0.5-7721)

Mahalanobis: Mahalanobis distance

Description

Returns the squared Mahalanobis distance of all rows in \(\bold{x}\) and the vector \(\bold{\mu}\) = center with respect to \(\bold{\Sigma}\) = cov. This is (for vector \(\bold{x}\)) defined as $$D^2 = (\bold{x} - \bold{\mu})^T \bold{\Sigma}^{-1} (\bold{x} - \bold{\mu})$$

Usage

Mahalanobis(x, center, cov, inverted = FALSE)

Arguments

x

vector or matrix of data. As usual, rows are observations and columns are variables.

center

mean vector of the distribution.

cov

covariance matrix (\(p \times p\)) of the distribution, must be positive definite.

inverted

logical. If TRUE, cov is supposed to contain the inverse of the covariance matrix.

Details

Unlike function mahalanobis, the covariance matrix is factorized using the Cholesky decomposition, which allows to assess if cov is positive definite. Unsuccessful results from the underlying LAPACK code will result in an error message.

See Also

Examples

Run this code
x <- cbind(1:6, 1:3)
xbar <- colMeans(x)
S <- matrix(c(1,4,4,1), ncol = 2) # is negative definite
D2 <- mahalanobis(x, center = xbar, S)
all(D2 >= 0) # several distances are negative

## next command produces the following error:
## Covariance matrix is possibly not positive-definite
if (FALSE) D2 <- Mahalanobis(x, center = xbar, S)

Run the code above in your browser using DataLab