[英]Mahalanobis distance of each pair of observations
I am trying to compute the Mahalanobis distance between each observations of a dataset dat
, where each row is an observation and each column is a variable. 我正在尝试计算数据集
dat
每个观测值之间的Mahalanobis距离,其中每一行是一个观测值,每一列是一个变量。 Such distance is defined as: 该距离定义为:
I wrote a function that does it, but I feel like it is slow. 我编写了一个函数来执行此操作,但是感觉很慢。 Is there any better way to compute this in R ?
有没有更好的方法可以在R中对此进行计算?
To generate some data to test the function: 生成一些数据来测试功能:
generateData <- function(nObs, nVar){
library(MASS)
mvrnorm(n=nObs, rep(0,nVar), diag(nVar))
}
This is the function I have written so far. 这是我到目前为止编写的函数。 They both work and for my data (800 obs and 90 variables), it takes approximatively 30 and 33 seconds for the
method = "forLoop"
and method = "apply"
, respectively. 对于我的数据(800 obs和90个变量),它们都起作用,
method = "forLoop"
和method = "apply"
分别花费大约30和33秒。
mhbd_calc2 <- function(dat, method) { #Method is either "forLoop" or "apply"
dat <- as.matrix(na.omit(dat))
nObs <- nrow(dat)
mhbd <- matrix(nrow=nObs,ncol = nObs)
cv_mat_inv = solve(var(dat))
distMH = function(x){ #Mahalanobis distance function
diff = dat[x[1],]-dat[x[2],]
diff %*% cv_mat_inv %*% diff
}
if(method=="forLoop")
{
for (i in 1:nObs){
for(j in 1:i){
mhbd[i,j] <- distMH(c(i,j))
}
}
}
if(method=="apply")
{
mhbd[lower.tri(mhbd)] = apply(combn(nrow(dat),2),2, distMH)
}
result = sqrt(mhbd)
colnames(result)=rownames(dat)
rownames(result)=rownames(dat)
return(as.dist(result))
}
NB: I tried using outer()
but it was even slower (60seconds) 注意:我尝试使用external
outer()
但速度甚至更慢(60秒)
You need some mathematical knowledge. 您需要一些数学知识。
dist
to compute Euclidean distance on transformed observations. dist
计算变换后的观测值的欧几里得距离。 dist.maha <- function (dat) {
X <- as.matrix(na.omit(dat)) ## ensure a valid matrix
V <- cov(X) ## empirical covariance; positive definite
L <- t(chol(V)) ## lower triangular factor
stdX <- t(forwardsolve(L, t(X))) ## standardization
dist(stdX) ## use `dist`
}
Example 例
set.seed(0)
x <- matrix(rnorm(6 * 3), 6, 3)
dist.maha(x)
# 1 2 3 4 5
#2 2.362109
#3 1.725084 1.495655
#4 2.959946 2.715641 2.690788
#5 3.044610 1.218184 1.531026 2.717390
#6 2.740958 1.694767 2.877993 2.978265 2.794879
The result agrees with your mhbd_calc2
. 结果与您的
mhbd_calc2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.