深度整合物种丰度的整合功能

Integrate functions for depth integrated species abundance

嘿,

我正在尝试计算整个深度范围(例如,从 10 m 到 90 m)内每个 class 的生物数量。为此,我在特定深度(例如 10、30 和 90 米)获得丰度,并使用积分函数计算: 每对深度之间的平均丰度乘以两对深度的差异。这些值在整个深度水柱上求和以获得水柱上的总丰度。 看一个例子(只是更大数据集的一小部分,有几个位置和年份,更多 class 和深度):

View(df)
      Class      Depth   organismQuantity
1   Ciliates        10  1608.89
2   Ciliates        30  2125.09
3   Ciliates        90  1184.92
4   Dinophyceae     10  0.00
5   Dinoflagellates 30  28719.60
6   Dinoflagellates 90  4445.26



integrate = function(x) {
  averages = (x$organismQuantity[1:length(x)-1] + x$organismQuantity[2:length(x)]) / 2
  sum(averages * diff(x$Depth))
}
result = ddply(df, .(Class), integrate)
print(result)

但是我得到了这些结果和带有 NA 值的行的警告消息:

     Class       V1
1        Ciliates 136640.1
2 Dinoflagellates       NA
3     Dinophyceae      0.0

Warning messages:
1: In averages * diff(x$Depth) :
  longer object length is not a multiple of shorter object length

我不明白为什么甲藻获得 NA 值...在我的完整数据集中其他几个 class 也是一样的(对于某些 class 丰度,积分方程适用于其他人我收到了警告信息)。 感谢您的帮助!! 干杯, 露西

这里有一种使用包 caTools 中的函数 trapz 的方法,适用于该问题。

#
# library(caTools)
# Author(s)
# Jarek Tuszynski
#
# Original, adapted
trapz <- function(DF, x, y){
  x <- DF[[x]]
  y <- DF[[y]]
  idx <- seq_along(x)[-1]
  as.double( (x[idx] - x[idx-1]) %*% (y[idx] + y[idx-1]) ) / 2
}

library(plyr)

ddply(df, .(Class), trapz, x = "Depth", y = "organismQuantity")
#            Class       V1
#1        Ciliates 136640.1
#2 Dinoflagellates 994945.8
#3     Dinophyceae       NA

数据

df <- read.table(text = "
      Class      Depth   organismQuantity
1   Ciliates        10  1608.89
2   Ciliates        30  2125.09
3   Ciliates        90  1184.92
4   Dinophyceae     10  0.00
5   Dinoflagellates 30  28719.60
6   Dinoflagellates 90  4445.26
", header = TRUE)