我写信是为了使用 R 中的 j-m (jeffries matusita) 距离方法分析我的数据的可分性。主要目标是计算两个以上的变量之间的 j-m 距离。
假设我有以下关于反射率的数据,主要任务是显示选定波长下四棵果树之间的可分离性。
orange <- c(37, 27, 45, 30, 57, 48, 34, 50, 20, 53, 33, 25, 51),
lemon <- c(12, 17, 20, 32, 16, 30, 30, 37, 25, 42, 13, 56, 13),
pear <- c(41, 19, 15, 12, 15, 55, 33, 37, 40, 40, 43, 46, 54),
apple <- c(38, 39, 12, 60, 34, 47, 13, 24, 30, 19, 57, 54, 55)
Wavelength <- c(354, 576, 842, 853, 918, 948, 1142, 1221, 1253, 1322, 1545, 1684, 2407)
最佳答案
所以你需要一个能够接受任意距离函数的距离方法,并且你需要一个JM距离的定义。后者可在 this post 中获得.对于前者,我们使用 dist(...)
包中的函数 proxy
,它允许指定任意函数来计算成对距离。
jm.dist <- function ( Vector.1 , Vector.2 ) {
# this function adapted from:
# https://stats.stackexchange.com/questions/78849/measure-for-separability
Matrix.1 <- as.matrix (Vector.1)
Matrix.2 <- as.matrix (Vector.2)
mean.Matrix.1 <- mean ( Matrix.1 )
mean.Matrix.2 <- mean ( Matrix.2 )
mean.difference <- mean.Matrix.1 - mean.Matrix.2
cv.Matrix.1 <- cov ( Matrix.1 )
cv.Matrix.2 <- cov ( Matrix.2 )
p <- ( cv.Matrix.1 + cv.Matrix.2 ) / 2
# calculate the Bhattacharryya index
bh.distance <- 0.125 *t ( mean.difference ) * p^ ( -1 ) * mean.difference +
0.5 * log (det ( p ) / sqrt (det ( cv.Matrix.1 ) * det ( cv.Matrix.2 )))
# calculate Jeffries-Matusita
# following formula is bound between 0 and 2.0
jm.distance <- 2 * ( 1 - exp ( -bh.distance ) )
# also found in the bibliography:
# jm.distance <- 1000 * sqrt ( 2 * ( 1 - exp ( -bh.distance ) ) )
# the latter formula is bound between 0 and 1414.0
return(jm.distance)
}
df <- data.frame(orange,lemon,pear,apple)
library(proxy)
dist(df,method=jm.dist,by_rows=FALSE)
# orange lemon pear
# lemon 0.24530946
# pear 0.04906073 0.09034789
# apple 0.05878462 0.14807198 0.01435419
请注意,一旦您加载了
proxy
您屏蔽了默认的库 dist(...)
功能。
关于r - 在 R 中使用 Jeffries-Matusita 距离方法的光谱可分离性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24762383/