Thursday, January 6, 2011

Ecological networks from abundance distributions

Another grad student and I tried recently to make a contribution to our understanding of the relationship between ecological network structure (e.g., nestedness) and community structure (e.g., evenness)...

...Alas, I had no luck making new insights. However, I am providing the code used for this failed attempt in hopes that someone may find it useful. This is very basic code. It was roughly based off of the paper by Bluthgen et al. 2008 Ecology (here). In my code the number of interactions is set to 600, and there are 30 plant species, and 10 animal species. This assumes they share the same abundance distributions and sigma values.

UPDATE: I changed the below code a bit to just output the metrics links per species, interaction evenness and H2.

#######################################################
library(bipartite)
 
# Set of mean and sd combinations of log-normal distribution
# In my case, these values were those empirically estimated 
# from many bipartite networks from the Interaction Web Database
mu <- c(0.5,2.9,5.3)
sig <- c(0.75,1.6,2.45)
 
# Function to make a set of matrices based on some combination of mu and sigma values
make.matrices <- function(a,b,nmats){
 plants <- round(rlnorm(n=30, meanlog=mu[a], sdlog=sig[b]))
 animals <- round(rlnorm(n=10, meanlog=mu[a], sdlog=sig[b]))
 plants <- plants*(600/sum(plants))
 animals <- animals*(600/sum(animals))
 r2dtable(nmats,animals,plants)
}
 
# Output matrices
matrices11 <- make.matrices(1,1,100)
 
# Etc. for the remaining combinations of mu and sigma
#matrices12 <- make.matrices(1,2,100)
#matrices13 <- make.matrices(1,3,100)
#matrices21 <- make.matrices(2,1,100)
# etc.....
 
# Calculate some network metrics-e.g., for one combination of mu and sigma
linkspersp11 <- numeric(100)
inteven11 <- numeric(100)
h211 <- numeric(100)
 
for(i in 1:length(matrices11)){
  m <- matrix(unlist(matrices11[i]),ncol=30,byrow=F)
  metrics <- t(networklevel(m,index=c("links per species","H2","interaction evenness")))
  linkspersp11[i] <- metrics[1]
 inteven11[i] <- metrics[2]
 h211[i] <- metrics[3]
}
 
linkspersp11
h211
inteven11
Created by Pretty R at inside-R.org

5 comments:

  1. Scott,

    Thanks for sharing. The code seems functional "on paper", but sadly it yields in nonsensical results. For instance, I often get matrices with mostly empty rows and columns (I can live with it and simply remove them), or even negative interaction values (this part is slightly more problematic).

    I'm trying to understand exactly why, but perhaps the simulation that can give "realistic" webs has to be more complicated than that…

    ReplyDelete
  2. Hi Timothee,

    Are you talking about the output of the function 'make.matrices'? I don't get that when I just ran it again. Can you show me? Is it only in certain cases, or all the time?

    There is a bug sometimes in which the function make.matrices in which I get:
    > matrices21 <- make.matrices(2,1,100)
    Error in r2dtable(nmats, animals, plants) :
    arguments 'r' and 'c' must have the same sums

    I'm not sure why this is happening, but I just run 'matrices21', or whatever one gave me that error message, again. It usually works the second time.

    You are right, this is very simple code. I imagine that of Bluthgen et al. is much more elegant.

    -S

    ReplyDelete
  3. Hi Scott,

    Nevermind the negative values problem, that was a bad parameter on my side (not enough interactions, or so I think).

    I managed to rewrite your code so that the problem you describe disappears. r2dtable is quite capricious, so I had to use error catching with the 'try' function to get it to work.

    t

    ReplyDelete
  4. Hi, Can you share the code solution?

    -S

    ReplyDelete
  5. I will try to make it really reliable (for the moment, some bugs still go undetected), then I'll put it online (no problem)

    t

    ReplyDelete