1
votes

I am interested in biodiversity index calculations using vegan package. The simpsons index works but no results from Shannon argument. I was hoping somebody know the solution

    What I have tried is that I have converted data. frame into vegan
    package test data format using code below  

      Plot     <- c(1,1,2,2,3,3,3)
      species  <- c( "Aa","Aa", "Aa","Bb","Bb","Rr","Xx")
      count   <-  c(3,2,1,4,2,5,7)

      veganData  <- data.frame(Plot,species,count)
      matrify(veganData )
      diversity(veganData,"simpson")
      diversity(veganData,"shannon", base = exp(1))


          1. I get the following results, so I think it produces all
              simpsons indices   

           > diversity(veganData,"simpson")
              simpson.D simpson.I simpson.R
           1      1.00      0.00       1.0
           2      0.60      0.40       1.7
           3      0.35      0.65       2.8


           2. But when I run for Shannon index get the following 
             message 

               > diversity(veganData,"shannon")
              data frame with 0 columns and 3 rows

     I am not sure why its not working ? do we need to make any changes 
      in data formatting while switching the methods?
1

1 Answers

2
votes

Your data need to be in the wide format. Also the counts must be either in total or averages (not repeated counts for the same plot).

library(dply); library(tidyr)

df <- veganData %>% 
         group_by(Plot, species) %>% 
         summarise(count = sum(count)) %>% 
         ungroup %>% 
         spread(species, count, fill=0)

df
# # A tibble: 3 x 5
#     Plot    Aa    Bb    Rr    Xx
#    <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     1     5     0     0     0
# 2     2     1     4     0     0
# 3     3     0     2     5     7

diversity(df[,-1], "shannon")
# [1] 0.0000000 0.5004024 0.9922820

To check if the calculation is correct, note the Shannon calculation is carried out as -1 x summation of Pi*lnPi

# For plot 3:

-1*(
    (2/(2+5+7))*log((2/(2+5+7))) + #Pi*lnPi of Bb
      (5/(2+5+7))*log((5/(2+5+7))) + #Pi*lnPi of Rr 
        (7/(2+5+7))*log((7/(2+5+7))) #Pi*lnPi of Xx
    )

 # [1] 0.992282