基于物种特征值模拟随机二分网络 - 在R中

时间:2012-08-27 22:46:58

标签: r phylogeny

我想在R中创建二分网络。例如,如果你有两种物种的数据框架(只能跨物种互动,不能在物种内互动),每个物种都有一个特征值(例如捕食者的嘴巴大小允许谁吃掉哪种猎物种类,我们如何根据物种的特征模拟网络(也就是说,两个物种只有在它们的特征重叠时才能相互作用)?

更新:这是我想要做的最小例子。 1)创建系统发育树; 2)模拟系统发育的特征; 3)基于物种特征值创建网络。

# packages
install.packages(c("ape","phytools"))
library(ape); library(phytools)

# Make phylogenetic trees
tree_predator <- rcoal(10)
tree_prey <- rcoal(10)

# Simulate traits on each tree
trait_predator <- fastBM(tree_predator)
trait_prey <- fastBM(tree_prey)

# Create network of predator and prey
## This is the part I can't do yet. I want to create bipartite networks, where 
## predator and prey interact based on certain crriteria. For example, predator
## species A and prey species B only interact if their body size ratio is
## greater than X.

1 个答案:

答案 0 :(得分:2)

答案的格式真的取决于你接下来要做什么,但这是一次尝试:

set.seed(101)
npred <- nprey <- 10
tree_predator <- rcoal(npred)
tree_prey <- rcoal(nprey)

## Simulate traits on each tree
trait_predator <- fastBM(tree_predator)
trait_prey <- fastBM(tree_prey)

(我使用set.seed(101)进行再现,所以这些是我的特质结果......

> trait_predator
         t1          t9          t4          t8          t5          t2 
-2.30933392 -3.17387148 -0.01447305 -0.01293273 -0.25483749  1.87279355 
         t6         t10          t3          t7 
 0.70646610  0.79508740  0.05293099  0.00774235 
> trait_prey
         t10           t7           t9           t6           t8           t1 
 0.849256948 -0.790261142  0.305520218 -0.182596793 -0.033589511 -0.001545289 
          t4           t5           t3           t2 
-0.312790794  0.475377720 -0.222128629 -0.095045954 

...)

你生成的值是在一个无限的空间,所以采取它们的比率是没有意义的;我们假设它们是大小的对数,因此exp(x-y)将是捕食者/猎物的大小比例。任意地,我假设截止值是1.5 ......

使用outer比较所有捕食者与捕食者的特征:这会创建一个二元(0/1)矩阵。

bmatrix <- outer(trait_predator,trait_prey,
      function(x,y) as.numeric(exp(x-y)>1.5))

可视化结果的一种方法......

library(Matrix)
image(Matrix(bmatrix),xlab="Prey",ylab="Predator",sub="")

enter image description here

你可以看到例如捕食者#6(在上面的输出中标记为t2)真的很大(对数大小= 1.87),所以它吃了所有猎物......

使用igraph(我的一些方法有点hacky)

library(igraph)

edges <- which(bmatrix==1,arr.ind=TRUE)  ## extract vertex numbers
## distinguish prey (columns) from pred (rows)
edges[,2] <- npred+edges[,2]             

gg <- graph.bipartite(rep(1:0,c(npred,nprey)),
             c(t(edges))) 
## c(t(edges)) collapses the two-column matrix to a vector in row order ...
## now plot ...
plot(gg,vertex.color=rep(c("cyan","pink"),c(npred,nprey)),
     edge.arrow.mode=">")

这匹配上面的矩阵格式 - 捕食者1和2(=顶点1和2)吃掉没有人,猎物2(=顶点12)被许多不同的捕食者吃掉......这种表现更漂亮但不是必须更清楚(例如捕食者7和8都吃猎物2(顶点12),但它们的箭头重合)。如果您想要应用图论方法,那么以igraph形式使用它可能会很好(并且有大量的布局选项可用于绘制图形)。

enter image description here