# mahalanobis

##### Mahalanobis Distance

Returns the squared Mahalanobis distance of all rows in `x`

and the
vector \(\mu\) = `center`

with respect to
\(\Sigma\) = `cov`

.
This is (for vector `x`

) defined as
$$D^2 = (x - \mu)' \Sigma^{-1} (x - \mu)$$

- Keywords
- multivariate

##### Usage

`mahalanobis(x, center, cov, inverted = FALSE, ...)`

##### Arguments

- x
vector or matrix of data with, say, \(p\) columns.

- center
mean vector of the distribution or second data vector of length \(p\) or recyclable to that length. If set to

`FALSE`

, the centering step is skipped.- cov
covariance matrix (\(p \times p\)) of the distribution.

- inverted
logical. If

`TRUE`

,`cov`

is supposed to contain the*inverse*of the covariance matrix.- ...
passed to

`solve`

for computing the inverse of the covariance matrix (if`inverted`

is false).

##### See Also

##### Examples

`library(stats)`

```
# NOT RUN {
require(graphics)
ma <- cbind(1:6, 1:3)
(S <- var(ma))
mahalanobis(c(0, 0), 1:2, S)
x <- matrix(rnorm(100*3), ncol = 3)
stopifnot(mahalanobis(x, 0, diag(ncol(x))) == rowSums(x*x))
##- Here, D^2 = usual squared Euclidean distances
Sx <- cov(x)
D2 <- mahalanobis(x, colMeans(x), Sx)
plot(density(D2, bw = 0.5),
main="Squared Mahalanobis distances, n=100, p=3") ; rug(D2)
qqplot(qchisq(ppoints(100), df = 3), D2,
main = expression("Q-Q plot of Mahalanobis" * ~D^2 *
" vs. quantiles of" * ~ chi[3]^2))
abline(0, 1, col = 'gray')
# }
```

*Documentation reproduced from package stats, version 3.6.1, License: Part of R 3.6.1*

### Community examples

Looks like there are no examples yet.