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, tol.inv = 1e-7)

_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.

 tol.inv: tolerance to be used for computing the inverse (if 'inverted'
          is false), see 'solve'.

_A_u_t_h_o_r(_s):

     Friedrich Leisch

_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, rowMeans(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')

