r - 运行香农和辛普森:素食包
问题描述
我对使用素食包计算生物多样性指数感兴趣。辛普森指数有效,但香农参数没有结果。我希望有人知道解决方案
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?
解决方案
您的数据需要采用宽格式。此外,计数必须是总数或平均值(不是同一地块的重复计数)。
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
推荐阅读
- swift - 如何确保新的视图控制器对用户可见?
- laravel - Laravel Echo 未订阅推送频道
- google-apps-script - 在不同的电子表格之间移动一行 - Google 电子表格
- ruby-on-rails - 如何使用 SQLite 在 Rails 6 中添加引用列迁移
- node.js - 节点 js => SyntaxError: Unexpected identifier
- python-3.x - 安装包时出现python依赖错误
- node.js - 在 Azure 和 Repl.it 上使用 Hapi 部署 Nodejs API 的问题
- javascript - 侧滚动顶部的标题
- angular - 使用带有 Angular 应用程序的 UserPoolId 和 ClientId 密钥访问现有的 AWS Cognito UserPool(我不是其所有者)
- android - 哪个 SingleLiveData 可以处理屏幕旋转和观察前发出的事件