Spectral separability using Jeffries-Matusita distance method in R
distance, r
Solution
So you need a distance method that accepts an arbitrary distance function, and you need a definition of JM distance. The latter is available in this post. For the former we use the `dist(...)` function in package `proxy`, which allows specifying an arbitrary function to calculate pairwise distances.
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
Note that once you load the `proxy` library you've masked the default `dist(...)` function.
Problem
I am writing to analyse separability on my data using j-m (jeffries matusita) distance method in R. The main goal is to calculate j-m distance between my variables which are more that two. Assuming i have the following data on reflectance, the main task is showing separability between the four fruit trees at the chosen wavelengths. ``` 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) ```