Bry*_*son 6 r social-networking
作为开发我正在研究的软件包演示的一部分,我需要量化一个经典的生态食品网,如下所述.我已经检查了素食主义者,二分法和sna,但没有看到任何能满足我需要的东西,尽管我可能错了 - 那些都是大包装.因此,我想知道这个想法是否已经在包中,或者是否有人有一个聪明的方法来计算结果.好像它应该是一个包.
食物网可以通过物种A:F之间的相互作用矩阵来描述,如代码和图中所示.换句话说,人们可以说"A吃B吃E"等(很难在矩阵中看到,图中是微不足道的).
species <- LETTERS[1:6]
links <- c(0, 1, 0, 0, 0, 0,
1, 0, 1, 1, 1, 0,
0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 1,
0, 1, 1, 1, 0, 0,
0, 0, 0, 1, 0, 0)
L <- matrix(links, nrow = 6, byrow = TRUE,
dimnames = list(species, species))
Run Code Online (Sandbox Code Playgroud)
我想计算每个物种的营养位置和营养高度.营养位置定义为特定物种+ 1下食物链中物种的总数.在图中,A的营养位置为6,D为3,另一方面,营养高度为平均值.物种在其参与的每个独立链中的位置.物种B连接到4个不同的链(路径); 它的高度是在时间上考虑的位置的平均值:(3 + 3 + 3 + 2)/ 4 = 2.75.
在计算上,需要读取矩阵L,然后通过矩阵隐含的不同路径来计算所需的值.
如果这不是太迟钝,有没有人知道这样做的包,或者看到一种方法来跟踪路径并计算各种长度/选项?它"感觉"必须有一些应该有效的递归/应用方法,但我不想重新发明东西.
提前致谢

这是计算您所追求的营养高度的一种方法。
假设矩阵L编码一个DAG,其中包含从每行中的物种到每列中的物种的链接。
L_(i,j)=1表示 spp_i 吃掉 spp_j。
然后L*L表示连接每对物种的两步“营养路径”的数量,L*L*L包含三步路径的数量,依此类推。在它们之间,“矩阵幂”集记录了连接节点对的所有路径,无论长度如何。
根据您的描述,一个物种的营养高度是一加上将其连接到图表尖端的“叶节点”之一的所有路径的平均路径长度。
## Here I've edited the matrix L to make it a DAG
species <- LETTERS[1:6]
links <-
c(0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0)
L <- matrix(links, nrow = 6, byrow = TRUE,
dimnames = list(FROM=species, TO=species))
Run Code Online (Sandbox Code Playgroud)
该函数(使用expm包中的运算符)应该可以满足您的要求。它可以做一些更详细的评论,但如果我有时间,我会在稍后添加。
library(expm) ## provides "%^%", a matrix power operator
calcHeight <- function(MAT) {
## Find 'leaf nodes' (i.e. species that are only eaten,
## and don't eat any others)
leaves <- which(rowSums(L)==0)
## Find the maximum possible chain length (if this is a DAG)
maxHeight <- nrow(MAT) - length(leaves) - 1
## Then use it to determine which matrix powers we'll need to calculate.
index <- seq_len(maxHeight)
paths <- lapply(index, FUN=function(steps) MAT %^% steps)
pathSteps <- lapply(index, FUN=function(steps) (1 + steps) * paths[[steps]])
## Trophic height is expressed relative to leaf nodes
paths <- Reduce("+", paths)[-leaves, leaves]
pathSteps <- Reduce("+", pathSteps)[-leaves, leaves]
rowSums(pathSteps)/rowSums(paths)
}
calcHeight(L)
A B C D
3.75 2.75 2.00 2.00
Run Code Online (Sandbox Code Playgroud)