1
$\begingroup$

I need help to calculate the Rao-Stirling diversity index. I tried it several times but cannot achieve equal results to R packages yet (e.g. diverse).

I used the Rao-Stirling diversity index as suggested by several references $$ \Delta=\sum_{ij} p_j p_i d_{ij} $$

including the reciprocal cosine similarity for computing the disparity $$ d_{ij}=(1-'cosine') $$

When I would have the following simple matrix, please could one show me how to do it?

\begin{bmatrix} 0 & 10 & 20 \\ 10 & 0 & 30 \\ 20 & 30 & 0 \end{bmatrix}


Edit 02/01/20

delta <- function(matrix){ 
proportion.vector <- prop.vector(matrix) 
distance.matrix <- dist.sim.matrix(matrix) 
result <- matrix(rep(NA,nrow(matrix)*ncol(matrix)),nrow=nrow(matrix)) 
for (i in 1:nrow(matrix)){ 
    for (j in 1:ncol(matrix)){ 

result[i,j] <- proportion.vector[i]*proportion.vector[j]*distance.matrix[i,j] } } 

return(colSums(result))
}

Oh sorry, I missed the point. Here is the code that I have used.

## Computing Rao-Stirling Diversity
#install.packages('diverse')
library(diverse)

rows <- c(1,1,1,2,2,2,3,3,3)
cols <- c(1,2,3,1,2,3,1,2,3)
values <- c(0,10,20,10,0,30,20,30,0)
my.dataframe <- data.frame(rows,cols,values)

RAO = diversity(data=my.dataframe, type="rao-stirling", method="cosine")
$\endgroup$
1
  • $\begingroup$ The formula requires the frequencies $p_i$ as well as the similarity matrix $d_{ij}.$ How, then, do you propose to implement it when given only a "simple matrix"?? What does this input matrix represent? $\endgroup$
    – whuber
    Jan 3 '20 at 15:14
1
$\begingroup$

I just had a cursory look at the literature. Here comes some code I just wrote:

    # computes cosine similarity between vec1 and vec2
    cos.sim <- function(vec1,vec2){
    return(sum(vec1*vec2)/sqrt(sum(vec1*vec1)*sum(vec2*vec2)))
    }

    # returns D_ij matrix
    dist.sim.matrix<- function(M){
         result <- matrix(rep(NA,nrow(M)^2),nrow=nrow(M))
         for (i in 1:nrow(M)){
            for (j in 1:nrow(M)){
                result[i,j] <- cos.sim(M[,i],M[,j])
            }
         }
         return(1-result)
     }

     # returns proportion vector
     prop.vector <- function(matrix){
          result <- numeric(ncol(matrix))
          total.sum <- sum(matrix)
          for (i in 1:ncol(matrix)){
             result[i] <- sum(matrix[,i])/total.sum
          }
          return(result)
       }
      # returns delta
     delta <- function(matrix){
        proportion.vector <- prop.vector(matrix)
        distance.matrix <- dist.sim.matrix(matrix)
        result <- matrix(rep(NA,nrow(matrix)^2),nrow=nrow(matrix))
        for (i in 1:nrow(matrix)){
           for (j in 1:nrow(matrix)){
             result[i,j] <-            proportion.vector[i]*proportion.vector[j]*distance.matrix[i,j]
           }
       }
      return(sum(result))
     }

     your.matrix <- matrix(c(0,10,20,10,0,30,20,30,0),nrow=3,byrow=TRUE)
     # Result for your matrix
     delta(your.matrix)

I hope this is correct.

$\endgroup$
4
  • $\begingroup$ I think it is correct because it results in the identical RaoStirling that I also have been calculated. However, it still does not comply with the results of the provided software of R (diverse). The results of the package are 1 0.1832 2 0.1177 3 0.0363 When I am using the previous code and the respective colSums, I receive 1 0.0780 2 0.1271 3 0.1799 Any suggestions? $\endgroup$
    – KGM
    Dec 24 '19 at 12:20
  • $\begingroup$ Your results are unclear to me. What are these numbers? My code is supposed to output a single number: the diversity. Besides, did you specify the correct method, i.e. 'cosine'? Feel free to share your code. it would help me to help you. $\endgroup$ Dec 24 '19 at 17:13
  • $\begingroup$ Sorry for the late and previous short reply; you know holidays ;) I used the same code and tried to receive the identical output through colSums in the delta function (see the little edit). $\endgroup$
    – KGM
    Jan 2 '20 at 10:43
  • $\begingroup$ Thanks but I was asking about your use of the diverse package. It does not seem to appear in the code you provided. $\endgroup$ Jan 2 '20 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.