Mahalanobis | R Documentation |
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})
Mahalanobis(x, center, cov, inverted = FALSE)
x |
vector or matrix of data. As usual, rows are observations and columns are variables. |
center |
mean vector of the distribution. |
cov |
covariance matrix ( |
inverted |
logical. If |
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.
cov
, mahalanobis
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
## Not run: D2 <- Mahalanobis(x, center = xbar, S)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.