首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R中Jeffries-Matusita距离法的谱可分性

R中Jeffries-Matusita距离法的谱可分性
EN

Stack Overflow用户
提问于 2014-07-15 15:36:03
回答 1查看 3.4K关注 0票数 3

在R中,我用j-m (jeffries matusita)距离法分析数据的可分性,主要目的是计算变量之间的j-m距离,即大于2的变量之间的距离。假设我有以下关于反射率的数据,主要任务是显示四个果树在选定波长处的可分性。

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-15 16:57:39

因此,您需要一个接受任意距离函数的距离方法,并且需要定义JM距离。后者可在this post中使用。对于前者,我们在包dist(...)中使用proxy函数,它允许指定任意函数来计算成对距离。

代码语言:javascript
运行
复制
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(...)函数。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24762383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档