<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/simulating-local-community-dynamics-under-ecological-drift/"></div></div><div><a data-url="https://www.r-bloggers.com/simulating-local-community-dynamics-under-ecological-drift/" data-counturl="https://www.r-bloggers.com/simulating-local-community-dynamics-under-ecological-drift/" data-count="horizontal" data-text="Simulating local community dynamics under ecological drift" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://biologyforfun.wordpress.com/2016/08/14/simulating-local-community-dynamics-under-ecological-drift/"> R – biologyforfun</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In 2001 the book by Stephen Hubbell on the <a target="_blank" href="http://press.princeton.edu/titles/7105.html">neutral theory of biodiversity</a> was a major shift from classical community ecology. Before this book the niche-assembly framework was dominating the study of community dynamics. Very briefly under this framework local species composition is the result of the resource available at a particular site and species presence or absence depends on species niche (as defined in <a target="_blank" href="http://press.uchicago.edu/ucp/books/book/chicago/E/bo3638660.html">Chase 2003</a>). As a result community assembly was seen as a deterministic process and a specific set of species should emerge from a given set of resources and species composition should be stable as long as the system is not disturbed. Hubbell theory based on a dispersal-assembly framework (historically developed by <a target="_blank" href="https://books.google.de/books?hl=de&lr=&id=YBDWCgAAQBAJ&oi=fnd&pg=PR7&dq=theory+island+biogeography&ots=czmii1b2IK&sig=EA1QSu629rDRYP2tIR4BuoWpsLo">MacArthur and Wilson</a>) was a departure from this mainstream view of community dynamics by assuming that local species composition are transient with the absence of any equilibrium. Species abundance follow a random walk under ecological drift, species are continuously going locally extinct while a constant rain of immigrants from the metacommunity bring new recruits. The interested reader is kindly encouraged to read: <a target="_blank" href="http://onlinelibrary.wiley.com/doi/10.1111/j.1461-0248.2006.00996.x/full">this</a>, <a target="_blank" href="http://www.jstor.org/stable/10.1086/652373?seq=1#page_scan_tab_contents">that</a> or <a target="_blank" href="http://www.sciencedirect.com/science/article/pii/S0169534706001650">that.</a></p>
<p>Being an R geek and after reading Chapter 4 of Hubbell’s book, I decided to implement his model of local community dynamic under ecological drift. Do not expect any advanced analysis of the model behavior, Hubbell actually derive in this chapter analytical solutions for this model. This post is mostly educational to get a glimpse into the first part of Hubbell’s neutral model.</p>
<p>Before diving into the code it is helpful to verbally outline the main characteristics of the model:</p>
<ul>
<li>The model is individual-based and neutral, assuming that all individuals are equal in their probability to die or to persist.</li>
<li>The model assume a zero-sum dynamic, the total number of individual (<strong>J</strong>) is constant, imagine for example a forest. When a tree dies it allows for a new individual to grow and use the free space</li>
<li>At each time step a number of individual (<strong>D</strong>) dies and are replaced by new recruits coming either from the metacommunity (with probability <strong>m</strong>) or from the local communities (with probability 1-m)</li>
<li>The species identity of the new recruit is proportional to the species relative abundance either in the metacommunity (assumed to be fixed and having a log-normal distribution) or in the local community (which obviously varies)</li>
</ul>
<p>We are now equipped to immerse ourselves into R code:</p>
<pre>#implement zero-sum dynamic with ecological drift from Hubbel theory (Chp4)
untb&lt;-function(R=100,J=1000,D=1,m=0,Time=100,seed=as.numeric(Sys.time())){
  mat&lt;-matrix(0,ncol=R,nrow=Time+1,dimnames=
  + list(paste0(&quot;Time&quot;,0:Time),as.character(1:R)))
  #the metacommunity SAD
  metaSAD&lt;-rlnorm(n=R)
  metaSAD&lt;-metaSAD/sum(metaSAD)
  #initialize the local community
  begin&lt;-table(sample(x=1:R,size = J,replace = TRUE,prob = metaSAD))
  mat[1,attr(begin,&quot;dimnames&quot;)[[1]]]&lt;-begin
  #loop through the time and get community composition
  for(i in 2:(Time+1)){
    newC&lt;-helper_untb(vec = mat[(i-1),],R = R,D = D,m = m,metaSAD=metaSAD)
    mat[i,attr(newC,&quot;dimnames&quot;)[[1]]]&lt;-newC
  }
  return(mat)
}
#the function that compute individual death and 
#replacement by new recruits 
helper_untb&lt;-function(vec,R,D,m,metaSAD){
  ind&lt;-rep(1:R,times=vec)
  #remove D individuals from the community
  ind_new&lt;-ind[-sample(x = 1:length(ind),size = D,replace=FALSE)]
  #are the new recruit from the Local or Metacommunity?
  recruit&lt;-sample(x=c(&quot;L&quot;,&quot;M&quot;),size=D,replace=TRUE,prob=c((1-m),m))
  #sample species ID according to the relevant SAD
  ind_recruit&lt;-c(ind_new,sapply(recruit,function(isLocal) {
    if(isLocal==&quot;L&quot;)
      sample(x=ind_new,size=1)
    else
      sample(1:R,size=1,prob=metaSAD)
  }))</pre>
<p>I always like simple theories that can be implemented with few lines of code and do not require fancy computations. Let’s put the function in action and get for a closed community (m=0) of 10 species the species abundance and the rank-abundance distribution dynamics:</p>
<pre>library(reshape2) #for data manipulation I
library(plyr) #for data manipulation II
library(ggplot2) #for the nicest plot in the world
#look at population dynamics over time
comm1&lt;-untb(R = 10,J = 100,D = 10,m = 0,Time = 200)
#quick and dirty first graphs
comm1m&lt;-melt(comm1)
comm1m$Time&lt;-as.numeric(comm1m$Var1)-1
ggplot(comm1m,aes(x=Time,y=value,color=factor(Var2)))
+geom_path()+labs(y=&quot;Species abundance&quot;)
+scale_color_discrete(name=&quot;Species ID&quot;)
#let's plot the RAD curve over time
pre_rad&lt;-apply(comm1,1,function(x) sort(x,decreasing = TRUE))
pre_radm&lt;-melt(pre_rad)
pre_radm$Time&lt;-as.numeric(pre_radm$Var2)-1
ggplot(pre_radm,aes(x=Var1,y=value/100,color=Time,group=Time))
+geom_line()+labs(x=&quot;Species rank&quot;,y=&quot;Species relative abundance&quot;)</pre>
<p><img alt="untb1.1" class="wp-image-847" src="https://i2.wp.com/biologyforfun.files.wordpress.com/2016/08/untb1-1.png?resize=456%2C157&ssl=1" data-recalc-dims="1"></p>
<p>We see there an important feature of the zero-sum dynamics, with no immigration the model will always (eventually) lead to the dominance of one species. This is because with no immigration there are so-called absorbing states, species abundance are attracted to these states and once these values are reached species abundance stay constant. In this case there are only two absorbing states for species relative abundance: 0 or 1, either you go extinct or you dominate the community.</p>
<p>In the next step we open up the community while varying the death rate and we track species richness dynamic:</p>
<pre>#apply this over a range of parameters
pars&lt;-expand.grid(R=20,J=200,D=c(1,2,4,8,16,32,64),
 + m=c(0,0.01,0.05,0.1,0.5),Time=200)
comm_list&lt;-alply(pars,1,function(p) do.call(untb,as.list(p)))
#get species abundance for each parameter set
rich&lt;-llply(comm_list,function(x) apply(x,1,function(y) length(y[y!=0])))
rich&lt;-llply(rich,function(x) data.frame(Time=0:200,Rich=x))
rich&lt;-rbind.fill(rich)
rich$D&lt;-rep(pars$D,each=201)
rich$m&lt;-rep(pars$m,each=201)
ggplot(rich,aes(x=Time,y=Rich,color=factor(D)))+geom_path()
+facet_grid(.~m)</pre>
<p><img alt="untb3" class="wp-image-849" src="https://i1.wp.com/biologyforfun.files.wordpress.com/2016/08/untb3.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>As we increase the probability that new recruit come from the metacommunity, the impact of increasing the number of death per time step becomes null. This is because the metacommunity is static in this model, even if species are wiped out by important local disturbance there will always be new immigrants coming in maintaining species richness to rather constant levels.</p>
<p>There is an R package for everything these days and the neutral theory is not an exception, check out the <a target="_blank" href="https://cran.r-project.org/web/packages/untb/index.html">untb</a> package which implement the full neutral model (ie with speciation and dynamic metacommunities) but also allow to fit the model to empirical data and to get model parameter estimate.</p>
<p>Filed under: <a target="_blank" href="https://biologyforfun.wordpress.com/category/biological-stuff/">Biological Stuff</a>, <a target="_blank" href="https://biologyforfun.wordpress.com/category/r-and-stat/">R and Stat</a> Tagged: <a target="_blank" href="https://biologyforfun.wordpress.com/tag/biodiversity/">Biodiversity</a>, <a target="_blank" href="https://biologyforfun.wordpress.com/tag/neutral-theory/">Neutral theory</a>, <a target="_blank" href="https://biologyforfun.wordpress.com/tag/r/">R</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/biologyforfun.wordpress.com/727/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/delicious/biologyforfun.wordpress.com/727/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/facebook/biologyforfun.wordpress.com/727/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/twitter/biologyforfun.wordpress.com/727/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/stumble/biologyforfun.wordpress.com/727/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/digg/biologyforfun.wordpress.com/727/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/biologyforfun.wordpress.com/727/"><img alt="" src="http://feeds.wordpress.com/1.0/reddit/biologyforfun.wordpress.com/727/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/simulating-local-community-dynamics-under-ecological-drift/"></div></div><div><a data-url="https://www.r-bloggers.com/simulating-local-community-dynamics-under-ecological-drift/" data-counturl="https://www.r-bloggers.com/simulating-local-community-dynamics-under-ecological-drift/" data-count="vertical" data-text="Simulating local community dynamics under ecological drift" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://biologyforfun.wordpress.com/2016/08/14/simulating-local-community-dynamics-under-ecological-drift/"> R – biologyforfun</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/DFgJUrBKJYo"></a></div>