mahalanobis              package:stats              R Documentation

_M_a_h_a_l_a_n_o_b_i_s _D_i_s_t_a_n_c_e

_D_e_s_c_r_i_p_t_i_o_n:

     Returns the 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)

_U_s_a_g_e:

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

_A_r_g_u_m_e_n_t_s:

       x: vector or matrix of data with, say, p columns.

  center: mean vector of the distribution or second data vector of
          length p.

     cov: covariance matrix (p x 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).

_S_e_e _A_l_s_o:

     'cov', 'var'

_E_x_a_m_p_l_e_s:

     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 Euclidean distances

     Sx <- cov(x)
     D2 <- mahalanobis(x, colMeans(x), Sx)
     plot(density(D2, bw=.5), main="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')

