R/dmahal.R

Defines functions dmahal

Documented in dmahal

dmahal<- function(datos, S){
########## Mahalanobis distance between pais of objects ##################
# Input:
# datos: data matrix
# S: var-cov matrix
# Output:
# d: distance matrix
###########################################################################
    n<- dim(datos)[1]
    p <- dim(datos)[2]
    s1 <- dim(S)[1]
    s2 <- dim(S)[2]
    if (s1 != s2){stop("S must be squared matrix")}
    if(p != s1){stop("data and covariance matrix must have coherent dimesions")}
    d<- matrix(0, n,n)
    I <- diag(rep(1,p)) #identity matrix 
    Sinv <- solve(S, I)
    for (i in 1:n){
         for (j in 1:i){
            a<- datos[i,]-datos[j,]
            a<-matrix(a, ncol=1)
            d[i,j] <- sqrt(t(a)%*%Sinv%*%a)
            d[j,i] <- d[i,j]
         }
     }

d <- as.dist(d)
return(d)
}

Try the ICGE package in your browser

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

ICGE documentation built on Oct. 17, 2022, 5:10 p.m.