下面的代码可让您生成100个逼真的食物网。食物网不是由列矩阵关联组成的,而是由代表“植物”,“草食动物”和“敌人”的三个列构成的。对于模拟的每个生态系统,都构建了一个独特的三养网。生态系统是通过在两个消费者水平上从对数正态丰度分布和pareto分布的饮食广度中随机选择个体来构建的。注意:即使没有消费者,交互网络的每一行总会有基础植物。
我们需要迫使社区拥有饮食广度的帕累托分布(饮食专长),以使其包括广泛的通才。目前,按照撰写本文的方式,广泛的通才的尾巴消失了。您会注意到,“广泛的通才”在某些分布中最多只能达到15种植物。这应该更大,因为在这些社区中有100多个植物。
以下是社区的代码:
library(doParallel)
library(vegan)
library(VGAM)
dFlooredParetoI <- function(x,alpha,xmin=NA,xmax=NA) {
## Set xmax or infer it from x as max(x)?
if(is.na(xmax)) {
xmax <- max(x)
}
## Set xmin or infer it from x as min(x)?
if(is.na(xmin)) {
xmin <- min(x)
}
return( (VGAM::ppareto(x+1,shape=alpha) - VGAM::ppareto(x,shape=alpha))/
(VGAM::ppareto(xmax+1,shape=alpha) - VGAM::ppareto(xmin,shape=alpha)) )
}
rFlooredParetoI <- function(n,alpha,xmin=1,maxval) {
if(maxval!=round(maxval)) { warning("maxval must be an integer!")}
ps <- dFlooredParetoI(xmin:maxval, alpha)
counts <- rmultinom(1,n,ps)
## this next line gives us counts[1] 1s, counts[2] 2s, ...
## then shuffles those and returns the vector.
return(sample(rep(which(counts>0), counts[which(counts>0)])))
}
cl<-makeCluster(4)
registerDoParallel(cl)
## ABUNDANCE, SPECIES, AND DIET BREADTH (1=PLANT, 2=HERBIVORE, 3=PSIT),
RAMPED UP EACH PART OF THE LOOP
## (THIS IS LIKE A 1000 M^2 PLOT SIZE, SO SCALE UP IF YOU LIKE A LARGER
SCALE)
## SPECIES1: 4:160
## SPECIES2: RAMPED UP FROM SPECIES 1
## SPECIES3: RAMPED UP FROM SPECIES 2
## ABUNDANCES: SHOULD STAY THE SAME AS THE RANGES BELOW
## DIETS: variable, with higher values for richer communities
#1,2,3 REFERS TO TROPHIC LEVEL, AND THE VALUES OF THE SEQ REPRESENT THE
RANGE OF THE ALPHA PARAMETER
x <- 2
repeat {
species1<- (2*x):(2*x+x)
abundance1<-250:600
species2<-(2*x+2*x):(3*x+2*x)
abundance2<-250:600
diet2<-seq((1+x/150),(2+x/40),by=0.1)
#diet2<-seq((1+x/20),(2+x/20),by=0.1)
species3<-(2*x+3*x):(3*x+3*x)
abundance3<-100:400
diet3<-seq((1+x/20),(2+x/20),by=0.1)
## ALSO, BUMP UP THE NUMBER OF ECOSYSTEMS TO WHATEVER YOU'D LIKE. HERE
I HAVE 10 COMMUNITIES
necosystems<-10
## parameters:
for (t in 1:necosystems){
## formulation: food web setup/interaction list
## The following lines are where individual variable levels are selected
## randomly
t1species<-sample(species1,1,replace=T)
t1abundance<-sample(abundance1,1,replace=T)
t2species<-sample(species2,1,replace=T)
t2abundance<-sample(abundance2,1,replace=T)
t2diet<-sample(diet2,1,replace=T)
t2alpha<-t2diet
t3species<-sample(species3,1,replace=T)
t3abundance<-sample(abundance3,1,replace=T)
t3diet<-sample(diet3,1,replace=T)
t3alpha<-t3diet
## The following three sections create log-normal densities of species
## the three trophic groups. The second and third groups have additional column
## for diet breadth based the pareto distributions.
## create lognormal deviations from mean:
t1deviates<-rlnorm(t1species,meanlog=0,sdlog=1)
## convert those deviations to individual densities based on overall abundance
## + 0.5 rounds out zeros
t1counts<-round(t1deviates*t1abundance/sum(t1deviates)+0.5)
## create matix and ther respective abundace per species
trophic1<-matrix(0,nrow=t1species,ncol=2)
trophic1[,1]<-1:t1species ## populate matrix with species numbers
trophic1[,2]<-t1counts ## populate matrix with their counts
## matrix created the same as above but with additional column for diet breadths
t2deviates<-rlnorm(t2species,meanlog=0,sdlog=1)
t2counts<-round(t2deviates*t2abundance/sum(t2deviates)+0.5)
alpha<-t2alpha; k<-exp(1)
## individual diet breadths created for each unique species
#t2dietbreadth<-VGAM::dparetoII(1:t2species,scale=alpha,shape=k)
t2dietbreadth<-rFlooredParetoI(t2species,alpha=alpha, maxval=t1species)
#t2dietbreadth
#hist(t2dietbreadth)
#t2dietbreadth<-round((t1species*t2dietbreadth)+0.5)
trophic2<-matrix(0,nrow=t2species,ncol=3)
trophic2[,1]<-1:t2species
trophic2[,2]<-t2counts
trophic2[,3]<-t2dietbreadth
## matrix created the same as above but with additional column for diet breadths
t3deviates<-rlnorm(t3species,meanlog=0,sdlog=1)
t3counts<-round(t3deviates*t3abundance/sum(t3deviates)+0.5)
alpha<-t3alpha; k<-exp(1)
## individual diet breadths created for each unique species
#t3dietbreadth<-VGAM::dparetoII(1:t3species,scale=alpha,shape=k)
t3dietbreadth<-rFlooredParetoI(t3species,alpha=alpha, maxval=t2species)
#t3dietbreadth<-round((t2species*t3dietbreadth)+0.5)
trophic3<-matrix(0,nrow=t3species,ncol=3)
trophic3[,1]<-1:t3species
trophic3[,2]<-t3counts
trophic3[,3]<-t3dietbreadth
plants<-rep(trophic1[,1],trophic1[,2])
interactions<-matrix(0,nrow=length(plants),ncol=7,byrow=F)
interactions[,1]<-plants
herbivores<-matrix(0,nrow=1,ncol=2)
enemies<-matrix(0,nrow=1,ncol=2)
i<-1
length<-nrow(trophic2)
while (i <= length){
food<-sample(trophic1[,1],trophic2[i,3],replace=T)
data<-matrix(c(rep(trophic2[i,1],trophic2[i,2]),rep(food,each=1,len=trophic2[i,2])),ncol=2,byrow=F)
herbivores<-rbind(herbivores,data)
i<-i+1
}
rownames(herbivores)<-1:nrow(herbivores)
i<-1
length<-nrow(trophic3)
while (i <= length){
food<-sample(trophic2[,1],trophic3[i,3],replace=T)
data<-matrix(c(rep(trophic3[i,1],trophic3[i,2]),rep(food,each=1,len=trophic3[i,2])),ncol=2,byrow=F)
enemies<-rbind(enemies,data)
i<-i+1
}
rownames(enemies)<-1:nrow(enemies)
i<-1
length<-nrow(interactions)
while (i <= length){
plant<-interactions[i,1]
if (any(herbivores[,2]==plant)){
row<-sample(rownames(herbivores[herbivores[,2]==plant,,drop=F]),1,replace=F) ####!!!!!!XXXXXXX
interactions[i,2]<-herbivores[row,1]
herbivores<-herbivores[!rownames(herbivores) %in% row,,drop=F]
}
herbivore<-interactions[i,2]
if (any(enemies[,2]==herbivore)){
row<-sample(rownames(enemies[enemies[,2]==herbivore,,drop=F]),1,replace=F) #####
interactions[i,3]<-enemies[row,1]
enemies<-enemies[!rownames(enemies) %in% row,,drop=F]
}
i<-i+1
}
interactions[,4]<-paste(interactions[,1],interactions[,2],sep="_")
interactions[,5]<-paste(interactions[,2],interactions[,3],sep="_")
interactions[,6]<-paste(interactions[,1],interactions[,2],interactions[,3],sep="_")
interactions[,4]<-gsub("(\\d+)(_0)(_0)", "0" , interactions[,4], perl=T )
interactions[,4]<-gsub("(_0)", "" , interactions[,4], perl=T )
interactions[,5]<-gsub("(\\d+)(_0)(_0)", "0" , interactions[,5], perl=T )
interactions[,5]<-gsub("(_0)", "" , interactions[,5], perl=T )
interactions[,6]<-gsub("(\\d+)(_0)(_0)", "0" , interactions[,6], perl=T )
interactions[,6]<-gsub("(_0)", "" , interactions[,6], perl=T )
interactions[,7]<-t*x
interactions<-data.frame(interactions)
names(interactions)<-c("plant","herbivore","enemy","int.PH","int.HE","int.PHE","ecosystem")
nsurveys<-nrow(interactions) #or #nsurveys = 500
survey<-sample(1:nrow(interactions),nsurveys,replace=F) ## or #survey<-sample(1:nrow(interactions),nsurveys,replace=T)
subsamples<-interactions[survey,]
outfile<-"Ecosystem.txt"
if (file.exists(outfile)){write.table(interactions,file = outfile,
append = T,quote = F,sep = " ",
row.names = F,col.names=F)
} else {
write.table(interactions,file = outfile,
append = T,quote = F,sep = " ",
row.names = F,col.names=T)
}
} ## end t loop ##
x = x+2
if (x > 90){
break
}
}
stopImplicitCluster()
答案 0 :(得分:1)
丰度太低,起始植物的丰度也太低。在代码的开头附近进行这些更改,您将获得大部分具有与植物物种丰富度相关的alpha的良好pareto分布:
x <- 10
repeat {
species1<- (2*x):(2*x+x)
abundance1<-2500:6000
species2<-(2*x+2*x):(3*x+2*x)
abundance2<-1000:4000
diet2<-seq((x/100),(1+x/100),by=0.1)
species3<-(2*x+3*x):(3*x+3*x)
abundance3<-500:1400
diet3<-seq((x/200),(1+x/200),by=0.1)
necosystems<-10