<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simulating-animal-movements-and-habitat-use/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://allthiswasfield.blogspot.com/2018/05/simulating-animal-movements-and-habitat.html"> long time ago...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> Hi there!</p>
<p> I was training some ways to simulate animal (or other organisms) movements having into account habitat suitability. To do this, I used my previous eWalk model as the underlying process to simulate random or directional walks. This model is based on Brownian / Ornstein–Uhlenbeck process. You can find <a rel="nofollow" href="http://allthiswasfield.blogspot.com.es/2016/11/ewolk-r-function-to-simulate-two.html" target="_blank">more about eWalk model here</a>!</p>
<p> Today, I will add one more element to this movement simulations. In this case, we will have into account the habitat or environmental preferences of the simulated species, to perform a simulation like this:</p>
<div><a rel="nofollow" href="https://i0.wp.com/4.bp.blogspot.com/-l-bCOkGMWMw/Wui-894bmwI/AAAAAAAAAP4/nD5BRuZWLPAVt7t3Zh57dRr-t4CwmWMxACLcBGAs/s1600/paths.gif?ssl=1" target="_blank"><img border="0" src="https://i0.wp.com/4.bp.blogspot.com/-l-bCOkGMWMw/Wui-894bmwI/AAAAAAAAAP4/nD5BRuZWLPAVt7t3Zh57dRr-t4CwmWMxACLcBGAs/s320/paths.gif?resize=450%2C831&ssl=1" data-original-width="450" width="450" data-original-height="831" data-recalc-dims="1"></a></div>
<p> First, we will create a raster layer as a random environmental variable, for example tree cover.</p>
<pre><code>   <br> library (raster)  <br> library (dismo)  <br>   <br> tc &lt;- raster(nrows=100, ncols=100, xmn=0, xmx=100, ymn=0,ymx=100)  <br> tc[] &lt;- runif(10000, -80, 180)  <br> tc &lt;- focal(tc, w=matrix(1, 5, 5), mean)  <br> tc &lt;- focal(tc, w=matrix(1, 5, 5), mean)  <br> plot(tc)  <br><br></code></pre>
<div></div>
<div></div>
<div></div>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-Bb2edCESEEs/Wuivgcq5P6I/AAAAAAAAAO4/EjV9L9_ymSInc4gsxrXHQFBy3FVlqgiSwCLcBGAs/s1600/tc.png?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/1.bp.blogspot.com/-Bb2edCESEEs/Wuivgcq5P6I/AAAAAAAAAO4/EjV9L9_ymSInc4gsxrXHQFBy3FVlqgiSwCLcBGAs/s400/tc.png?resize=450%2C831&ssl=1" data-original-width="450" width="450" data-original-height="831" data-recalc-dims="1"></a></div>
<p> Second, we will define the species class. The species will be defined by their position (coordinates), and their optimum for the environmental variable.</p>
<pre><code>   <br> species &lt;- setClass(&quot;species&quot;, slots=c(x=&quot;numeric&quot;, y=&quot;numeric&quot;, opt=&quot;numeric&quot;))  <br><br></code></pre>
<p></p>
<div></div>
<p> Here we will define the Red deer species as a specimen in the coordinates (50,50) and an optimum of 80 for the environmental variable (tree cover). In the same way, we will define the Egyptian mongoose as a specimen in the coordinates (50,50) and an optimum of 30 for the tree cover variable.</p>
<pre><code>   <br> Red_deer &lt;- species(x= 50, y =50, opt= 90)  <br> Egyptian_mongoose &lt;- species(x= 50, y =50, opt= 30)  <br>   <br></code></pre>
<p> Now, we will load the “<i>go</i>” function (I do not have a name for it yet). It require a species (<i>sp</i>), a raster layer with any environmental variable (<i>env</i>), number of iterations (<i>n</i>), a Brownian motion parameter (that is, how random is the movement of your species), a geographical optimum (the wanted destination of your species <i>theta_x</i> and <i>theta_y</i>), and the attraction strength or “interest” of the species to get this position (<i>alpha_x</i> and <i>alpha_y</i>). The syntaxis should be something like this:</p>
<pre><code>   <br> path &lt;- go (sp, env, n, sigma, theta_x, alpha_x, theta_y, alpha_y)  <br>   <br></code></pre>
<p> Here is the function to load (I will comment the function in a future post):</p>
<pre><code>   <br> go &lt;- function (sp, env, n, sigma, theta_x, alpha_x, theta_y, alpha_y) {  <br>  track &lt;- data.frame()  <br>  track[1,1] &lt;- sp@x  <br>  track[1,2] &lt;- sp@y  <br>  for (step in 2:n) {  <br>   neig &lt;- adjacent(env,   <br>            cellFromXY(env, matrix(c(track[step-1,1],  <br>                        track[step-1,2]), 1,2)),   <br>            directions=8, pairs=FALSE )  <br>   options &lt;- data.frame()  <br>   for (i in 1:length(neig)){  <br>    options[i,1]&lt;-neig[i]  <br>    options[i,2]&lt;- sp@opt - env[neig[i]]  <br>   }  <br>   option &lt;- c(options[abs(na.omit(options$V2)) == min(abs(na.omit(options$V2))), 1 ],   <br>         options[abs(na.omit(options$V2)) == min(abs(na.omit(options$V2))), 1 ])  <br>   new_cell &lt;- sample(option,1)  <br>   new_coords &lt;- xyFromCell(env,new_cell)  <br>   lon_candidate&lt;--9999  <br>   lat_candidate&lt;--9999  <br>     <br>   while ( is.na(extract(env, matrix(c(lon_candidate,lat_candidate),1,2)))) {  <br>    lon_candidate &lt;- new_coords[1]+ (sigma * rnorm(1)) + (alpha_x * ( theta_x - new_coords[1]))  <br>    lat_candidate &lt;- new_coords[2]+ (sigma * rnorm(1)) + (alpha_y * ( theta_y - new_coords[2]))  <br>   }  <br>   track[step,1] &lt;- lon_candidate  <br>   track[step,2] &lt;- lat_candidate  <br>  }  <br>  return(track)  <br> }  <br>   <br>   <br></code></pre>
<p> Well, now we can perform a simple experiment with our two specimens. We will simulate random movement of these two species having into account their environmental optimums. The “<i>go</i>” function will return us the track or the path followed by each specimen (coordinates by each step).</p>
<pre><code>   <br> deer_simul &lt;- go (Red_deer, tc, 100, 2, 90, 0, 90, 0)  <br> mongoose_simul &lt;- go (Egyptian_mongoose, tc, 100, 2, 90, 0, 90, 0)  <br>   <br></code></pre>
<p> We can plot the paths…</p>
<pre><code>   <br> plot(tc)  <br> lines(deer_simul, lwd=1.5, col=&quot;red&quot;)  <br> points(deer_simul, cex=0.3, col=&quot;red&quot;)  <br> lines(mongoose_simul, lwd=1.5, col=&quot;blue&quot;)  <br> points(mongoose_simul, cex=0.3, col=&quot;blue&quot;)  <br> legend(&quot;topleft&quot;, legend=c(&quot;deer&quot;,&quot;mongoose&quot;), col=c(&quot;red&quot;,&quot;blue&quot;),  <br>     lty=c(1,1), lwd=c(2,2))  <br>   <br></code></pre>
<p></p>
<div><a rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-e1lO1DOhrrg/Wui2udjjlDI/AAAAAAAAAPY/BJhOCFDPT4YvPDhk66RUCWmwcB2R2PT3wCLcBGAs/s1600/paths.png?ssl=1" target="_blank"><img border="0" src="https://i0.wp.com/1.bp.blogspot.com/-e1lO1DOhrrg/Wui2udjjlDI/AAAAAAAAAPY/BJhOCFDPT4YvPDhk66RUCWmwcB2R2PT3wCLcBGAs/s400/paths.png?resize=450%2C831&ssl=1" data-original-width="450" width="450" data-original-height="831" data-recalc-dims="1"></a></div>
<p> To test if each species is actually “searching” their environmental optimum, we can extract the environmental values by step for each species and plot their density distributions.</p>
<pre><code>   <br> plot(density(extract(tc, deer_simul)),lwd=3, col=&quot;red&quot;, xlim=c(20,80),   <br>    ylim=c(0,max(c(density(extract(tc, deer_simul))$y,  <br>           density(extract(tc, mongoose_simul))$y))),  <br>    main=&quot;locations density distribution&quot;, xlab=&quot;tree cover&quot;)  <br> lines(density(extract(tc, mongoose_simul)),lwd=3, col=&quot;blue&quot;)  <br> legend(&quot;topleft&quot;, legend=c(&quot;deer&quot;,&quot;mongoose&quot;), col=c(&quot;red&quot;,&quot;blue&quot;),  <br>     lty=c(1,1), lwd=c(3,3))  <br>   <br></code></pre>
<p></p>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-uDp2lf-lA_w/Wui3gvsGFMI/AAAAAAAAAPg/J9Fy7Z9TUpYUk0SMSuACEmafnJJui_T6QCLcBGAs/s1600/Rplot.png?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/2.bp.blogspot.com/-uDp2lf-lA_w/Wui3gvsGFMI/AAAAAAAAAPg/J9Fy7Z9TUpYUk0SMSuACEmafnJJui_T6QCLcBGAs/s400/Rplot.png?resize=450%2C831&ssl=1" data-original-width="450" width="450" data-original-height="831" data-recalc-dims="1"></a></div>
<p> So, we can see that the deer is actually using patches with a higher value of tree cover than the mongoose… our simulation worked! You can use the code of <a rel="nofollow" href="http://allthiswasfield.blogspot.com.es/2017/12/p-margin-bottom-0.html" target="_blank">this post</a> to perform a GIF like the one above.</p>
<p> That’s all I have to say about this for now… In the next posts we will simulate more animal movements and migrations!</p>
<p>PD: Let me show you a nice song about the <a rel="nofollow" href="https://www.youtube.com/watch?v=pQKUgoPZEks" target="_blank">mongoose</a>…</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://allthiswasfield.blogspot.com/2018/05/simulating-animal-movements-and-habitat.html"> long time ago...</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/EdzD1qqrHrg" height="1"></a></div>