mahal_dist function

Calculate the pairwise Mahalanobis distances between observations

Calculate the pairwise Mahalanobis distances between observations

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.

mahal_dist(x, Cov, ...)

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.

Returns

An object of class "dist".

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

# 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)

Author(s)

Michael Collyer