Learn R Programming

RRPP (version 2.1.2)

mahal_dist: Calculate the pairwise Mahalanobis distances between observations

Description

This function emulates the dist function but allows a covariance matrix (Cov) to be included for standardizing distances. It is assumed that the Covariance matrix makes sense with respect to the data, and that the number of variables match between data and covariance matrix.

Usage

mahal_dist(x, Cov, ...)

Value

An object of class "dist".

Arguments

x

A numeric matrix of data frame.

Cov

A covariance matrix with the same number of variables as the data.

...

Other arguments passed to dist.

Author

Michael Collyer

Details

No tests are performed on distances but could be performed with the pairwise function. Distances are only calculated if the covariance matrix is not singular.

Examples

Run this code

# Using the Pupfish data (see lm.rrpp help for more detail)

data(Pupfish)
Pupfish$Y <- ordinate(Pupfish$coords)$x[, 1:3]
fit <- lm.rrpp(Y ~ Sex * Pop, SS.type = "I", 
data = Pupfish, print.progress = FALSE, iter = 0)
means <- unique(model.matrix(fit)) %*% coef(fit)
rownames(means) <- unique(interaction(Pupfish$Sex, Pupfish$Pop))
means
S <- getResCov(fit)
dist(means)
mahal_dist(means, S)

Run the code above in your browser using DataLab