Last data update: 2014.03.03

R: Mahalanobis distances for grouped data
pairwise.mahalanobisR Documentation

Mahalanobis distances for grouped data

Description

Returns a square matrix of Mahalanobis distances by doing a pairwise comparison of group means using the correlation between variables.

Usage

pairwise.mahalanobis(x, grouping = NULL, cov = NULL, inverted = FALSE, digits = 5, ...)

Arguments

x

vector or matrix of data with N observations and D variables. If grouping is not specified, the first column is used for grouping observations.

grouping

vector of characters or values designating group classification for observations.

cov

Covariance matrix (DxD) of the distribution

inverted

logical. If TRUE, cov is the inverse of the covariance matrix.

digits

number of decimals to keep for the means, cov and distance values

...

passed to mahalanobis for computing the inverse of the covariance matrix (if inverted is false).

Details

To determine the distance between group i and group j, the difference of group means for each variable are compared. For a (NxD) data matrix with m groups, a matrix of mxD means and a correlation matrix of DxD values are calculated. pairwise.mahalanobis calculates the mahalanobis distance for all possible group combinations and results in a mxm square distance matrix with m choose 2 distinct pairwise measures.

Value

means

(mxD) matrix of group means for each variable

cov

(DxD) covariance matrix of centered and scaled data, so it's actually the correlation matrix

distance

(mxm) matrix of squared mahalanobis distances

Author(s)

Lisa McFerrin

See Also

mahalanobis

Examples


data(bHLH288)
grouping = t(bHLH288[,1])
bHLH_Seq = as.vector(bHLH288[,2])
bHLH_pah = FactorTransform(bHLH_Seq, alignment=TRUE)

Mahala1 = pairwise.mahalanobis(bHLH_pah, grouping, digits = 3)
D = sqrt(Mahala1$distance)
D

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(HDMD)
Loading required package: psych
Loading required package: MASS
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/HDMD/pairwise.mahalanobis.Rd_%03d_medium.png", width=480, height=480)
> ### Name: pairwise.mahalanobis
> ### Title: Mahalanobis distances for grouped data
> ### Aliases: pairwise.mahalanobis
> 
> ### ** Examples
> 
> 
> data(bHLH288)
Warning message:
In data(bHLH288) : data set 'bHLH288' not found
> grouping = t(bHLH288[,1])
> bHLH_Seq = as.vector(bHLH288[,2])
> bHLH_pah = FactorTransform(bHLH_Seq, alignment=TRUE)
> 
> Mahala1 = pairwise.mahalanobis(bHLH_pah, grouping, digits = 3)
> D = sqrt(Mahala1$distance)
> D
         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 0.000000 4.142149 5.077082 5.619281 4.818219
[2,] 4.142149 0.000000 4.300668 5.662378 4.004002
[3,] 5.077082 4.300668 0.000000 6.356415 4.781217
[4,] 5.619281 5.662378 6.356415 0.000000 6.929369
[5,] 4.818219 4.004002 4.781217 6.929369 0.000000
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>