Trophic position / height in food webs (following paths in webs)

As part of developing a demo for the package I'm working on, I need to quantify the ecological food web as described below. I checked vegan, bipartisan and sleep, but I see nothing that does what I need, although I can be wrong - these are large packages. So I'm wondering if this idea is already in the package, or if someone has a smart way to calculate the results. It looks like it should be a package.

The food web can be described by a matrix of interactions between A: F species, as shown in the code and diagram. In words, we can say that "A eats B, which eats E", etc. (It is very difficult to see in the matrix, trivial in the diagram).

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)) 

I want to calculate the trophic position and trophic height of each species. The trophic position is defined as the total number of species in the food chain below a certain species + 1. In the diagram, the trophic position A is 6, and for D it is 3. On the other hand, the trophic height is average about the position of the species in each individual chain in which it involved. Species B is connected to 4 different chains (tracks); its height is the average of the considered positions at the time: (3 + 3 + 3 + 2) / 4 = 2.75.

It is computable to calculate, you need to read the matrix L, and then work through the various paths implied by the matrix to calculate the necessary values.

If this is not too stupid, does anyone know a package that will do this, or look at a way to track paths and compute at different lengths / parameters? It โ€œfeelsโ€ as if there should be some kind of recursive / applied approach that should work, but I don't want to invent material.

Thanks at Advance

a simple food web

+6
source share
2 answers

Here is one way to calculate the trophic heights that you are after.

Let's say that the matrix L encodes a DAG with FROM references of views in each row to a view in each column. L_(i,j)=1 indicates that spp_i is eating spp_j.

Then L*L expresses the number of two-step "trophic pathways" connecting each pair of species, L*L*L contains the number of three-step paths, etc. Between them, a set of โ€œmatrix degreesโ€ records all paths of any length connecting pairs of nodes.

From your description, the trophic height of the view is one plus the average path length of all the paths that connect it to one of the "leaf nodes" at the ends of the graph.

 ## 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)) 

This function (which uses the operator in the expm package) should do what you are looking for. This can be done with more detailed comments, but I will add that later if I have time.

 library(expm) ## provides "%^%", a matrix power operator calcHeight <- function(MAT) { ## Find 'leaf nodes' (ie 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) ABCD 3.75 2.75 2.00 2.00 
+2
source

This is a classic graph theory material. You have a directed graph where animals are nodes, and โ€œA eats Bโ€ is the edge. Your matrix is โ€‹โ€‹an adjacency matrix encoding edges (but you probably want half of the matrix to be 0s, so you don't have A eats B and B eats A. Similarly, zeros on the diagonal if you don't have cannibalistic behavior). This is a directed acyclic graph linked in another answer.

Graph theory packages are listed in graphical models. View task:

http://ftp.heanet.ie/mirrors/cran.r-project.org/web/views/gR.html

and I think igraph will do what you want. First create a graph object from your matrix (using a modified matrix with only the upper triangle):

 > LG=graph.adjacency(L) > LG Vertices: 6 Edges: 7 Directed: TRUE Edges: [0] 'A' -> 'B' [1] 'B' -> 'C' [2] 'B' -> 'D' [3] 'B' -> 'E' [4] 'C' -> 'E' [5] 'D' -> 'E' [6] 'D' -> 'F' 

Then you can use community.size to get the trophic position:

 > neighborhood.size(LG,Inf,mode="out") [1] 6 5 2 3 1 1 

which should be in order A, B, C, D, E, F. Note the use of Inf to stop the neighborhood restriction and use the mode = "exit" to follow the graphs in the direction of the meal.

All this talk about food chains makes me hungry. Hope you get started.

Barry

+3
source

Source: https://habr.com/ru/post/898788/


All Articles