R/mahalanobis.dist.R

Defines functions mahalanobis.dist

Documented in mahalanobis.dist

mahalanobis.dist <- function(data.x, data.y=NULL, vc=NULL){

    xx <- as.matrix(data.x)
    if(is.null(data.y)) yy <- as.matrix(data.x)
    else yy <- as.matrix(data.y)

    if(is.null(vc)){
        if(is.null(data.y)) vc <- var(xx)
        else vc <- var(rbind(xx,yy))
    }

    ny <- nrow(yy)
    md <- matrix(0,nrow(xx), ny)
    for(i in 1:ny){
        md[,i] <- mahalanobis(xx, yy[i,], cov=vc)
    }
    if(is.null(data.y)) dimnames(md) <- list(rownames(data.x), rownames(data.x))
    else dimnames(md) <- list(rownames(data.x), rownames(data.y))
    sqrt(md)
}

Try the StatMatch package in your browser

Any scripts or data that you put into this service are public.

StatMatch documentation built on May 29, 2024, 2:15 a.m.