使用Jeffries-Matusita距离法在R中的光谱可分离性

时间:2014-07-15 15:36:03

标签: r distance

我写的是用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)

1 个答案:

答案 0 :(得分:8)

所以你需要一个接受任意距离函数的距离方法,你需要一个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(...)功能。