mahalanobis(x, center, cov, inverted = FALSE, ...)
x | vector or matrix of data. Rows represent observations and columns represent variables. Missing values (NAs) are allowed. |
center | a numeric vector, center should not be longer than the number of column of x, usually is the mean of the distribution. Missing values (NAs) are allowed. |
cov | matrix giving the covariance matrix for the distribution. This must be square and have the same number of columns as x. This may alternatively be a QR decomposition of the covariance matrix, or the inverse of the covariance matrix (see inverted). Missing values (NAs) are not allowed. |
inverted | logical flag to specify if cov has been inverted. If FALSE, cov will be inverted before it is used. |
... | other arguments to pass in or from other functions. |
y <- cbind(c(2,3,4,1,7,6), c(2,3,5)) vy <- var(y) mahalanobis(c(0,0), 1:2, cov=vy) # [1] 2.428571x <- matrix(rnorm(3*13), ncol=3) mx <- colMeans(x) vx <- var(x) m1 <- mahalanobis(x, mx, vx)
qrv <- qr(vx) m2 <- mahalanobis(x, mx, qrv)
vinv <- solve(qrv) m3 <- mahalanobis(x, mx, vinv, inverted=TRUE) # m1, m2 and m3 are the same