A simple model to illustrate metapopulation (sink source) dynamics

This code produces a very simple spatial simulation with a tcltk interface to illustrate how connectivity of a landscape affects the viability of populations. It has three parameters

1) The probability of any single sub-population becoming extinct during a time step.

2) The probability of recolonization from any connected sub-population (note that this can be lower than the extinction probability and the whole population can be viable if there are several connections between each sub population)

3) The distance beyond which sub-populations are not connected.

test.gif

library(spdep)
library(tcltk)

#

run<-function(ext=0.1,colin=0.2,t=100,dist=50){
set.seed(1)
res<<-numeric(t)
coords<<-data.frame(x=runif(100,0,200),y=runif(100,0,200))
pop<<-sample(c(0,1),100,replace=T,prob=c(0.2,0.98) )
nbs <<- dnearneigh(as.matrix(coords),0,dist)
plot(nbs, coords,bg=pop+1,col=”green”,cex=1.5,pch=21)
for(i in 1:t){
res[i]<<-sum(pop)
ncontacts<-unlist(lapply(nbs,function(x)sum(pop[x])))
colonization<<-(runif(100,0,1)<colin)*ncontacts>0
extinction<<-runif(100,0,1)>ext
pop<<-pop*extinction+colonization
pop[pop>1]<<-1
points(coords, bg=pop+1,col=”green”,cex=1.5,pch=21)
if(sum(pop)<1)
Sys.sleep(0.1)
gc()
}}

##############################################
#############################################
###################################################
RUN<-function(){
ext1<-as.numeric(tclvalue(“ext”))
colin1<-as.numeric(tclvalue(“colin”))
dist1<-as.numeric(tclvalue(“dist”))
t1<-as.numeric(tclvalue(“t”))
run(ext=ext1,colin=colin1,dist=dist1,t=t1)}

plot2<-function(){
plot(res,type=”l”,xlab=”Tiempo”,ylab=”Ocupancy”)
}
#####################################################

###################################################
#INTERFACE

tt <- tktoplevel()
but <- tkbutton(tt, text=”Run”,command=RUN)
but5 <- tkbutton(tt, text=”plot”,command=plot2)

d1<-tklabel(tt,text=”Probabilidad de extinción”)
d2<-tklabel(tt,text=”Probabilidad de recolonización”)
d3<-tklabel(tt,text=”Distancia”)
d4<-tklabel(tt,text=”Tiempo”)

s1 <- tkscale(tt,from=0, to=1, variable=”ext”,
showvalue=TRUE, resolution=0.1, orient=”horiz”)
s2 <- tkscale(tt,from=0, to=1, variable=”colin”,
showvalue=TRUE, resolution=0.1, orient=”horiz”)
s3 <- tkscale(tt,from=10, to=100, variable=”dist”,
showvalue=TRUE, resolution=5, orient=”horiz”)
s4 <- tkscale(tt,from=100, to=1000, variable=”t”,
showvalue=TRUE, resolution=100, orient=”horiz”)

tkgrid(d1,s1)
tkgrid(d2,s2)
tkgrid(d3,s3)
tkgrid(d4,s4)
tkgrid(but)
tkgrid(but5)
###########################################

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s