Learn R Programming

StatMatch (version 1.2.0)

mahalanobis.dist: Computes the Mahalanobis Distance

Description

This function computes the Mahalanobis distance among units in a dataset or among observations in two distinct datasets.

Usage

mahalanobis.dist(data.x, data.y=NULL, vc=NULL)

Arguments

data.x
A matrix or a data frame containing variables that should be used in the computation of the distance. Only continuous variables are allowed. Missing values (NA) are not allowed.

When only data.x is supplied, the distances betw

data.y
A numeric matrix or data frame with the same variables, of the same type, as those in data.x (only continuous variables are allowed). Dissimilarities between rows of data.x and rows of data.y will be computed. If n
vc
Covariance matrix that should be used in distance computation. If it is not supplied (vc=NULL) it is estimated from the input data. In particular, when vc=NULL and only data.x is supplied then the covariance matrix

Value

  • A matrix object with distances among rows of data.x and those of data.y.

Details

This function computes the Mahalanobis distance:

$$d(i,j)=\sqrt{(x_i - x_j)^T S^{-1} (x_i - x_j)}$$

When vc=NULL the covariance matrix S is estimated from the available data (see argument vc for details) otherwise the one supplied via the argument vc is used.

References

Mahalanobis, P C (1936) On the generalised distance in statistics. Proceedings of the National Institute of Sciences of India 2, pp. 49-55.

See Also

mahalanobis

Examples

Run this code
md1 <- mahalanobis.dist(iris[1:6,1:4])
md2 <- mahalanobis.dist(data.x=iris[1:6,1:4], data.y=iris[51:60, 1:4])

vv <- var(iris[,1:4])
md1a <- mahalanobis.dist(data.x=iris[1:6,1:4], vc=vv)
md2a <- mahalanobis.dist(data.x=iris[1:6,1:4], data.y=iris[51:60, 1:4], vc=vv)

Run the code above in your browser using DataLab