首页 > 解决方案 > 运行香农和辛普森:素食包

问题描述

我对使用素食包计算生物多样性指数感兴趣。辛普森指数有效,但香农参数没有结果。我希望有人知道解决方案

    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?

标签: rvegan

解决方案


您的数据需要采用宽格式。此外,计数必须是总数或平均值(不是同一地块的重复计数)。

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

要检查计算是否正确,请注意香农计算执行为 -1 x 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

推荐阅读