EVOLUTION-MANAGER
Edit File: mahalanobis.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Mahalanobis Distance</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for mahalanobis {stats}"><tr><td>mahalanobis {stats}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Mahalanobis Distance</h2> <h3>Description</h3> <p>Returns the squared Mahalanobis distance of all rows in <code>x</code> and the vector <i>mu</i> = <code>center</code> with respect to <i>Sigma</i> = <code>cov</code>. This is (for vector <code>x</code>) defined as </p> <p style="text-align: center;"><i>D^2 = (x - μ)' Σ^-1 (x - μ)</i></p> <h3>Usage</h3> <pre> mahalanobis(x, center, cov, inverted = FALSE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>vector or matrix of data with, say, <i>p</i> columns.</p> </td></tr> <tr valign="top"><td><code>center</code></td> <td> <p>mean vector of the distribution or second data vector of length <i>p</i> or recyclable to that length. If set to <code><a href="../../base/html/logical.html">FALSE</a></code>, the centering step is skipped.</p> </td></tr> <tr valign="top"><td><code>cov</code></td> <td> <p>covariance matrix (<i>p x p</i>) of the distribution.</p> </td></tr> <tr valign="top"><td><code>inverted</code></td> <td> <p>logical. If <code>TRUE</code>, <code>cov</code> is supposed to contain the <em>inverse</em> of the covariance matrix.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>passed to <code><a href="../../base/html/solve.html">solve</a></code> for computing the inverse of the covariance matrix (if <code>inverted</code> is false).</p> </td></tr> </table> <h3>See Also</h3> <p><code><a href="cor.html">cov</a></code>, <code><a href="cor.html">var</a></code></p> <h3>Examples</h3> <pre> 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') </pre> <hr /><div style="text-align: center;">[Package <em>stats</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>