-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtutorial_3.html
1548 lines (1396 loc) · 67.7 KB
/
tutorial_3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Tutorial 3: Dynamic landscapes and movement paths</title>
<script src="site_libs/header-attrs-2.23/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/jqueryui-1.13.2/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<script src="site_libs/clipboard-1.7.1/clipboard.min.js"></script>
<link href="site_libs/primer-tooltips-1.4.0/build.css" rel="stylesheet" />
<link href="site_libs/klippy-0.0.0.9500/css/klippy.min.css" rel="stylesheet" />
<script src="site_libs/klippy-0.0.0.9500/js/klippy.min.js"></script>
<link href="site_libs/font-awesome-6.4.0/css/all.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.4.0/css/v4-shims.min.css" rel="stylesheet" />
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
/* for pandoc --citeproc since 2.11 */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark the anchor link active (and if it's in a dropdown, also mark that active)
var dropdown = menuAnchor.closest('li.dropdown');
if (window.bootstrap) { // Bootstrap 4+
menuAnchor.addClass('active');
dropdown.find('> .dropdown-toggle').addClass('active');
} else { // Bootstrap 3
menuAnchor.parent().addClass('active');
dropdown.addClass('active');
}
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">RangeShiftR tutorials</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">About RangeShiftR</a>
</li>
<li>
<a href="installing.html">Installing RangeShiftR</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
RangeShiftR tutorials
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="overview_0.html">0. General package introduction</a>
</li>
<li>
<a href="tutorial_1.html">1. Range expansion</a>
</li>
<li>
<a href="tutorial_2.html">2. Patch connectivity</a>
</li>
<li>
<a href="tutorial_3.html">3. Dynamic landscapes & SMS paths</a>
</li>
<li>
<a href="tutorial_4.html">4. Evolution of dispersal</a>
</li>
<li>
<a href="appendix_tutorial_3.html">Appendix A3. Create dynamic landscapes</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="mailto:[email protected]">
<span class="fa fa-envelope"></span>
</a>
</li>
<li>
<a href="https://rangeshifter.github.io">
<span class="fa fa-home"></span>
</a>
</li>
<li>
<a href="https://github.com/RangeShifter">
<span class="fa fa-github"></span>
</a>
</li>
<li>
<a href="https://twitter.com/ZurellLab">
<span class="fa fa-twitter"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Tutorial 3: Dynamic landscapes and movement
paths</h1>
</div>
<script>
addClassKlippyTo("pre.r, pre.markdown");
addKlippy('right', 'top', 'auto', '1', 'Copy code', 'Copied!');
</script>
<p>In this tutorial, we show how to simulate population response to
dynamic changes in the landscape. Additionally, we explore possibilities
to analyse individual path data from the Stochastic Movement Simulator
(SMS) dispersal model and compare different SMS specifications.</p>
<p>To this end, two settings of landscape dynamics are covered:</p>
<ol style="list-style-type: decimal">
<li>Road scenario: a patch-matrix landscape is getting fragmented by the
construction of a road</li>
<li>Land abandonment scenario: a cell-based landscape with continuous
shifts in the proportional cover of different land use types</li>
</ol>
<div id="getting-started" class="section level1" number="1">
<h1><span class="header-section-number">1</span> Getting started:</h1>
<div id="create-a-rs-directory" class="section level2" number="1.1">
<h2><span class="header-section-number">1.1</span> Create a RS
directory</h2>
<p>First of all, load <code>RangeShiftR</code> and other required
packages and set the relative path from your current working directory
to the RS directory.</p>
<pre class="r"><code># load packages
library(RangeShiftR)
library(tidyr)
library(ggplot2)
library(terra)
library(gridExtra)
library(RColorBrewer)
# relative path from current working directory:
dirpath = "Tutorial_03/"</code></pre>
<p>As already shown in the previous tutorials, we need the three
sub-folders ‘Inputs’, ‘Outputs’ and ‘Output_Maps’, which can be created
from within R if they don’t exist already:</p>
<pre class="r"><code>dir.create(paste0(dirpath,"Inputs"), showWarnings = TRUE)
dir.create(paste0(dirpath,"Outputs"), showWarnings = TRUE)
dir.create(paste0(dirpath,"Output_Maps"), showWarnings = TRUE)</code></pre>
<p>Copy the input files provided for exercise 3 into the ‘Inputs’
folder. The files can be downloaded <a
href="files/Tutorial3_Inputs.zip">here</a>. All dynamic landscapes from
Tutorial 3 were created using the package <code>NLMR</code> <span
class="citation">(Sciaini et al. 2018)</span> as shown in the <a
href="appendix_tutorial_3.html">appendix</a>.</p>
</div>
</div>
<div id="road-scenario" class="section level1" number="2">
<h1><span class="header-section-number">2</span> Road scenario</h1>
<p>In this scenario, we simulate a patch-matrix landscape that is
getting fragmented by the construction of a road. With this example we
show some possible ways to analyse the optional SMS paths output.</p>
<div id="artificial-maps" class="section level2" number="2.1">
<h2><span class="header-section-number">2.1</span> Artificial Maps</h2>
<p>We created a series of four maps (<em>map_01.asc</em> to
<em>map_04.asc</em>) that represent different time steps in which a
landscape mosaic of woodland and grassland patches in an agricultural
matrix gets increasingly disturbed by woodland clearing and the
construction of a road. For the code used to create these habitat maps
and their corresponding patch maps, see the <a
href="appendix_tutorial_3.html">appendix</a>. However, this code has not
been updated to terra yet and still depends on raster.</p>
<p>We choose land type <em>2</em> to be the matrix and the others
(<em>1</em>,<em>3</em>,<em>4</em>) to be different forms of habitat. The
road is denoted by its own code <em>5</em> and will be assigned higher
dispersal costs. The land types are:</p>
<ol style="list-style-type: decimal">
<li>semi-natural grassland</li>
<li>arable</li>
<li>woodland</li>
<li>improved grassland</li>
<li>road</li>
</ol>
<p>Load all habitat maps into R and plot them together:</p>
<pre class="r"><code>habitat_maps <- terra::rast(sapply(1:4, FUN=function(n){terra::rast(paste0(dirpath,"Inputs/map_0",n,".asc"))}))
mycol_terrain <- c("#D0C096","#E2E2E2","#027C1E","#97A753","dark red")
plot(habitat_maps,
col=mycol_terrain,
breaks = 0:5+.5, type="continuous", axes=F)</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-4-1.png" width="672" /></p>
<p>The patches are defined by cohesive regions of habitat, confined by
the matrix. Thus, a patch can be made up of up to three different
habitat types. As we have already seen in <a
href="tutorial_02.html">Tutorial 2</a>, we also need to explicitly
define the patch IDs. The patch files should have the same extent and
resolution as the habitat map, and each cell contains a unique patch ID
that indicates to which patch it belongs. As the patches do not change
between habitat maps 3 and 4, we only need three different patch
files.</p>
<pre class="r"><code>patches <- terra::rast(sapply(1:3, FUN=function(n){terra::rast(paste0(dirpath,"Inputs/patches_0",n,".asc"))}))
# Plot the patches in different colours:
plot(patches, breaks=c(0:230), legend=F,
col = c('black',rep(brewer.pal(n = 12, name = "Paired"),5)), axes = F, type ="continuous")</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-5-1.png" width="672" /></p>
<p>As the last input map, we look at the initial distribution of our
model species. According to this map, a few large patches will be
initialised at the beginning of the simulation time:</p>
<pre class="r"><code>plot(terra::rast(paste0(dirpath,"Inputs/init_dist.asc")), type="continuous")</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-7-1.png" width="672" /></p>
</div>
<div id="rangeshiftr-setup" class="section level2" number="2.2">
<h2><span class="header-section-number">2.2</span> RangeShiftR
setup</h2>
<p>We aim to simulate a population at equilibrium with the environment
before road construction starts. Landscape clearing and road
construction then increasingly disrupt individual movement in the
landscape. In order to set up the simulation, we need to specify the
species’ demography parameters, the landscape parameters and the
dispersal parameters.</p>
<div id="demographic-parameters" class="section level3" number="2.2.1">
<h3><span class="header-section-number">2.2.1</span> Demographic
parameters</h3>
<p>We choose to model a long-lived, stage-structured population with
four stages and a simple form of mate limitation
(<code>ReproductionType = 1</code>). The transition matrix is set up so
that new-borns (stage 0) always develop to juveniles (stage 1), meaning
that new-born mortality is accounted for within the fecundity. Juveniles
(stage 1) do not reproduce and develop to sub-adult (stage 2) quickly,
with a rate of ca. <em>6/7</em>. Sub-adult (stage 2) have a little
slower development than juveniles. Only the adults (stage 3) reproduce
and have a high survival rate.</p>
<pre class="r"><code>( TraMa <- matrix( c(0,1,0,0, 0,.10,.60,0, 0,0,0.2,0.45, 5,0,0,.85), ncol = 4) )</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 0 0.0 0.00 5.00
## [2,] 1 0.1 0.00 0.00
## [3,] 0 0.6 0.20 0.00
## [4,] 0 0.0 0.45 0.85</code></pre>
<p>Furthermore, we assume density-dependence in fecundity and survival,
where the density dependence in survival is lower than in fecundity
(controlled by <code>SurvDensCoeff</code>). Finally, we assume that
individuals in patches that get destroyed will not die instantly but
will be able to disperse to new patches (controlled by the parameter
<code>PostDestructn</code>).</p>
<pre class="r"><code>demog <- Demography(ReproductionType = 1,
StageStruct = StageStructure(Stages = 4,
TransMatrix = TraMa,
FecDensDep = T,
SurvDensDep = T,
SurvDensCoeff = 0.4,
PostDestructn = T))</code></pre>
</div>
<div id="set-landscape-and-explore-equilibrium-population-size"
class="section level3" number="2.2.2">
<h3><span class="header-section-number">2.2.2</span> Set landscape and
explore equilibrium population size</h3>
<p>Next, we need to define the landscape object (using
<code>ImportedLandscape()</code>). You may remember from the previous
tutorials that the strength of density dependence (<em>1/b</em>) is also
specified in the landscape object via the argument
<code>K_or_DensDep</code>. (Note that <code>K_or_DensDep</code> holds
the carrying capacities <em>K</em> in case of a non-structured
population and <em>1/b</em> in case of a stage-structured population,
like in this example.)</p>
<p>Specifically, for each land cover type, we need to set <em>1/b</em>,
the strength of density dependence, which will affect the equilibrium
population size. To this end, we give a vector to
<code>K_or_DensDep</code> that holds the values of <em>1/b</em> for each
land type. Since we’d like to simulate a woodland species we set the
lowest density dependence <em>b</em> (i.e. highest <em>1/b</em>) for
forest (land type <em>3</em>). To characterise the matrix and roads,
<em>1/b</em> is set to <em>0</em>, denoting non-habitat.</p>
<p>As <em>b</em> affects the equilibrium population size, it will also
affect the maximum abundance that we could observe in a patch and stage.
To explore the potential effect of <em>b</em> on equilibrium population
sizes before running the simulation, we implemented the function
<code>getLocalisedEquilPop()</code>. This allows a better understanding
of reasonable values of <em>1/b</em> for the different land types. In
fact, the function <code>getLocalisedEquilPop()</code> runs a quick
simulation of a closed and localised population (i.e. without dispersal
and in a single idealised patch) for a given vector of potential
<em>1/b</em> values (argument <code>DensDep_values</code>) and based on
our above defined <code>Demography()</code> module (argument
<code>demog</code>).</p>
<p>The <code>getLocalisedEquilPop()</code> function uses absolute values
of individuals in the local population (since there is not spatial
extent), while what we need are relative values of <em>1/b</em> as
individuals per hectare. We choose to simulate a hypothetical patch of
our landscape, and want to determine the value of <em>1/b</em> that is
needed to observe e.g. <em>100</em> individuals (let’s assume this
corresponds to our empirical observation how many individuals were
maximally observed in woodland patches). Thus, we aim to find the
<em>1/b</em> parameter that would yield a maximum local population
abundance of <em>100</em> individuals. We can now assess the localised
equilibrium population size for different values of <em>1/b</em> and see
how the density dependence plays out. By default, the function
<code>getLocalisedEquilPop()</code> will output a barplot illustrating
the equilibrium population sizes for the different levels of
<em>1/b</em>:</p>
<pre class="r"><code>eq_pop <- getLocalisedEquilPop(demog = demog, DensDep_values = seq(50,300,50))</code></pre>
<pre><code>## [1] 50 100 150 200 250 300</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-10-1.png" width="672" /></p>
<p>Here, a <em>1/b</em> of roughly <em>150</em> would yield the desired
result approximately, in a closed and localised population. We choose
the slightly higher value, since dispersal losses are not accounted for.
We thus assume <em>1/b=150</em> for the woodland habitat and lower
<em>1/b</em> for the less suitable grassland patches.</p>
<p>Now, we can specify all parameters in the landscape object, including
the file names of the habitat and patch files, and the years in which
the different landscapes should be loaded in the simulation. Note that
we have to include the patch map 3 twice, in order to match the patch
maps with their corresponding habitat maps.</p>
<pre class="r"><code># define in which simulation year the new habitat maps and patch landscape should be loaded:
year_blocks <- c(0,80,110,140)
# explicitly provide the file names to the landscape object:
landnames <- c("map_01.asc","map_02.asc","map_03.asc","map_04.asc")
land <- ImportedLandscape(LandscapeFile = landnames,
PatchFile = c("patches_01.asc","patches_02.asc","patches_03.asc","patches_03.asc"),
DynamicLandYears = year_blocks,
Nhabitats = 5,
Resolution = 10,
K_or_DensDep = c(125,0,150,75,0),
SpDistFile = "init_dist.asc",
SpDistResolution = 10)</code></pre>
</div>
<div id="dispersal-parameters" class="section level3" number="2.2.3">
<h3><span class="header-section-number">2.2.3</span> Dispersal
parameters</h3>
<p>The next step is to set up the dispersal module. We set a density-
and stage-dependent emigration probability, such that only the juvenile
and sub-adult stages emigrate. As a transfer method we choose the SMS
movement process with a dispersal bias, which gives an individual the
preference to move away from its natal patch. The strength of this bias
decays with the number of steps taken (controlled by the decay rate
<code>AlphaDB</code> and the inflection point <code>BetaDB</code>). In
<code>SMS()</code>, the dispersal resistance of each land type is set by
the argument <code>Costs</code>, and the constant step mortality is
defined by <code>StepMort</code>. The density-dependent emigration
probability is defined by the maximum emigration probability and the
parameters <span class="math inline">\(\alpha\)</span> and <span
class="math inline">\(\beta\)</span>, all of which are provided as
matrix in the argument <code>EmigProb</code>.</p>
<p>The settlement rules include a maximum and minimum number of steps
per dispersal event. Furthermore, we use the option to set a maximum
number of steps per year (<code>MaxStepsYear</code>). If an individual
reaches this limit during dispersal, it halts and waits for the next
year to continue its dispersal event, while being subjected to the
yearly survival probability of its stage. If, in contrast the individual
hits the overall limit on the number of steps, <code>MaxSteps</code>, it
dies.</p>
<p>Additionally, we set the mating requirement (<code>FindMate</code>)
for settlement. This enables an individual to settle in a given patch
only if there is an individual of the opposite sex present.</p>
<pre class="r"><code># density-dependent emigration
disp <- Dispersal(Emigration = Emigration(StageDep = T,
DensDep = T,
EmigProb = cbind(0:3,c(0.55,0.45,0,0), c(5,5,0,0), c(1,1,0,0))),
Transfer = SMS(DP = 1.8, MemSize = 4,
GoalType = 2, GoalBias = 2.5, AlphaDB = .4, BetaDB = 10,
Costs = c(3,5,1,2,30),
StepMort = 0.01),
Settlement = Settlement(MaxSteps = 80, MinSteps = 15, MaxStepsYear = 20,
FindMate = T)
)</code></pre>
<p>We can have a look at the dispersal bias and emigration probability
we parameterised using the generic function
<code>plotProbs()</code>.</p>
<pre class="r"><code>par(mfrow=c(1,2))
plotProbs(disp@Transfer)
plotProbs(disp@Emigration)</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-13-1.png" width="672" /></p>
</div>
<div id="initialisation" class="section level3" number="2.2.4">
<h3><span class="header-section-number">2.2.4</span> Initialisation</h3>
<p>Now we turn to the initial conditions of our simulation. We
initialise every patch with the equilibrium density and proportional
density of stages that we simulated above with the matrix model (with
<code>1/b=150</code>).</p>
<pre class="r"><code># run again for convenience to get local equilibrium population size
# at target value of 1/b:
eq_pop <- getLocalisedEquilPop(demog = demog, DensDep_values = 150, plot=F)
# calculate proportion of all stages excluding the new-born juvenile (stage 0) population,
# which can't be initialised:
prop_stgs <- eq_pop[-1]/sum(eq_pop[-1])
# we initialise at roughly half the 1/b of the most suitable patch
init <- Initialise(InitType = 1, # from loaded species distribution map (see 'land' module)
SpType = 0, # all presence cells (the default)
InitDens = 2, # initial density: user-defined
IndsHaCell = 75, # initial density in inds/ha
PropStages = c(0,prop_stgs), # initial stage distribution
InitAge = 2) # initial age distribution: quasi-equilibrium (the default).</code></pre>
</div>
<div id="simulation" class="section level3" number="2.2.5">
<h3><span class="header-section-number">2.2.5</span> Simulation</h3>
<p>Lastly, we set the number of simulated years and replicates as well
as the types and temporal intervals of produced output.</p>
<p>We request the population output for every 10 years and the range
output for every 5 years.</p>
<p>Since we simulate SMS dispersal, we can enable the paths output to
get the individual dispersal trajectories and their step-wise status by
setting <code>OutIntPaths</code>> 0. Since this output type can
produce large amounts of data when there are many dispersal events
taking place, it can be advantageous to start with large intervals or
few replicates. The results for each replicate will be stored in a
separate file.</p>
<p>Finally, we put together the parameter master with all the settings
we have made in this section and a set seed.</p>
<pre class="r"><code>simul <- Simulation(Simulation = 1,
Years = 200,
Replicates = 20,
OutIntPop = 10,
OutIntRange = 5,
OutIntPaths = 10,
OutIntInd = 10)
s <- RSsim(batchnum = 12, seed = 987, land = land, demog = demog, dispersal = disp, init = init, simul = simul)</code></pre>
</div>
<div id="run-rs" class="section level3" number="2.2.6">
<h3><span class="header-section-number">2.2.6</span> Run RS</h3>
<p>Let’s run the <code>RangeShiftR</code> simulation:</p>
<pre class="r"><code>RunRS(s, dirpath = dirpath)</code></pre>
</div>
</div>
<div id="results" class="section level2" number="2.3">
<h2><span class="header-section-number">2.3</span> Results</h2>
<div id="abundance-and-occupied-patches-over-time"
class="section level3" number="2.3.1">
<h3><span class="header-section-number">2.3.1</span> Abundance and
occupied patches over time</h3>
<p>To get a first impression of our simulation results, we look at the
time series for total abundance and the number of occupied patches.
These functions use the range output; as we set its output interval to 5
years, we get a time series with a temporal resolution of 5 years.</p>
<pre class="r"><code>par(mfrow=c(1,2))
plotAbundance(s, dirpath)
plotOccupancy(s, dirpath)</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-17-1.png" width="672" /></p>
<p>After initialisation, the abundance first drops and then quickly
grows to values around <em>2000</em> individuals. The mean occupancy
plateaus at about <em>20</em> patches, but with a large variance.</p>
</div>
<div id="occupancy-probability-and-time-to-colonisation"
class="section level3" number="2.3.2">
<h3><span class="header-section-number">2.3.2</span> Occupancy
Probability and Time to colonisation</h3>
<p>We use the function <code>ColonisationStats()</code> to get the
occupancy probabilities at given years as well as the time to
colonisation for all patches. With the option <code>maps=T</code>, this
function also returns maps to visualise these quantities.</p>
<p><code>ColonisationStats()</code> uses the population output; as we
set its output interval to 10 years, we can only request the results for
those years. If we don’t specify a year, the last recorded year is used
by default. (In the second scenario below, you can find an example on
how to get the occupancy probabilities for given years.)</p>
<p>The function returns a list with the numeric results and the
maps:</p>
<pre class="r"><code>col <- ColonisationStats(s, dirpath, maps=T)</code></pre>
<pre><code>## Warning: [rast] the first raster was empty and was ignored
## Warning: [rast] the first raster was empty and was ignored</code></pre>
<pre class="r"><code>names(col)</code></pre>
<pre><code>## [1] "occ_prob" "col_time" "map_occ_prob" "map_col_time"</code></pre>
<p>Let’s plot the mean occupancy probability in the last recorded year,
year <em>200</em>, over all replicates:</p>
<pre class="r"><code>plot(col$map_occ_prob)</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-19-1.png" width="672" /></p>
<p>We find that larger patches tend to have a higher probability of
being occupied in the last year of a given replicate of our
simulation.</p>
<p>Now, let’s also plot the mean time to colonisation for all
patches:</p>
<pre class="r"><code>plot(col$map_col_time)</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-20-1.png" width="672" /></p>
<p>The lighter the color, the earlier a patch was colonised during the
simulation. The initial patches appear in light gray and the matrix in
white. We find that larger patches tend to get colonised earlier than
smaller ones.</p>
</div>
<div id="local-population-densities-prior-to-landscape-changes"
class="section level3" number="2.3.3">
<h3><span class="header-section-number">2.3.3</span> Local population
densities prior to landscape changes</h3>
<p>To get a spatial view of the population, we load the population
output that contains information on patch-specific and stage-specific
abundance (recorded every 10 years) and map the local population density
onto the four different patch maps. We use the last recorded time step
before the landscape changes occur.</p>
<p>Since the population output lists the absolute abundances for each
patch, larger patches tend to have larger population sizes than smaller
ones. To account for this, we divide the abundances by the number of
cells in its respective patch to get a population density.</p>
<pre class="r"><code># last recorded time step before the landscape changes:
map_years <- c(70,100,130,190)
# We have already read in the patches earlier:
patches <- patches[[c(1:3,3)]]
# initialise raster stacks for population density
map_pop <- patches
names(map_pop) <- paste('Year',map_years)
values(map_pop)[values(map_pop)==0] <- NA
values(map_pop)[values(map_pop) >0] <- 0
# populate stack with values read from Population output (takes a little while to run...)
pop_df <- readPop(s, dirpath)
for(i in 1:length(map_years)){
# mean NInd per Patch over all replicates
Pop_mean_over_reps <- aggregate(NInd~PatchID, data = subset(pop_df,Year==map_years[i],select=c('Rep','PatchID','NInd')), FUN = mean)
# exclude matrix patch
Pop_mean_over_reps <- Pop_mean_over_reps[Pop_mean_over_reps$PatchID>0,]
# get patch sizes
cell_counts <- data.frame(table(values(patches[[i]])))
Pop_mean_over_reps <- merge(Pop_mean_over_reps, cell_counts, by.x = 'PatchID', by.y = 'Var1')
# assign density values to patch map
for (l in 1:nrow(Pop_mean_over_reps) ) {
values(map_pop[[i]])[values(patches[[i]])==Pop_mean_over_reps[l,'PatchID']] <- (Pop_mean_over_reps[l,'NInd']/Pop_mean_over_reps[l,'Freq'])
}
}
# plot density
plot(map_pop, range=c(0,max(values(map_pop), na.rm=T)),type="continuous")</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-21-1.png" width="672" /></p>
<p>In the present example, we find that larger patches tend to have
higher population densities. We encourage you to play around with
different modelling assumptions, for example the density dependence in
survival and emigration probability and explore the effects on local
population densities.</p>
</div>
<div id="dispersal-outcomes" class="section level3" number="2.3.4">
<h3><span class="header-section-number">2.3.4</span> Dispersal
outcomes</h3>
<p>Now we analyse the SMS paths output. There is a separate output file
for each replicate. Let’s look at the paths of the first replicate (note
that counting starts at zero):</p>
<pre class="r"><code># read output file
steps <- read.table(paste0(dirpath,"Outputs/Batch12_Sim1_Land1_Rep1_MovePaths.txt"), header = T)
head(steps)</code></pre>
<pre><code>## Year IndID Step x y Status
## 1 0 4 0 100 123 0
## 2 0 4 1 100 124 1
## 3 0 10 0 92 118 0
## 4 0 10 1 92 119 1
## 5 0 17 0 93 123 0
## 6 0 17 1 93 122 1</code></pre>
<p>The SMS paths files list for each output year all currently
dispersing individuals with all steps taken by them during this year.
From this file we can easily extract how many steps individuals took
during dispersal.</p>
<pre class="r"><code># we ask how often a step was logged per individual
hist(c(table(steps$IndID)),xlab='Number of steps',main='Steps per dispersing individual')</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-23-1.png" width="672" /></p>
<p>We see that many individuals disperse more than 20 steps, meaning
that they did not settle within the first year. Quite a few of them even
reach the overall maximum number of steps <code>MaxSteps</code>, which
is set to <em>80</em> in this example.</p>
<p>To reconstruct the individual paths, we need the status information
of each step from the last column of the SMS path files. This variable
codes for 9 different possible states:</p>
<ol start="0" style="list-style-type: decimal">
<li>in natal patch</li>
<li>still dispersing</li>
<li>awaiting settlement in possible suitable patch</li>
<li>waiting between dispersal events</li>
<li>completed settlement</li>
<li>completed settlement in a suitable neighbouring cell</li>
<li>died during transfer by failing to find a suitable patch (includes
exceeding maximum number of steps or crossing absorbing boundary)</li>
<li>died during transfer by constant, step-dependent, habitat-dependent
or distance-dependent mortality</li>
<li>died: failed to survive annual (demographic) mortality (only
available in Individual output)</li>
<li>died: exceeded maximum age (only available in Indidividual
output)</li>
</ol>
<p>The first 7 states are directly related to dispersal and thus are
included in the SMS paths output. The last two states (8 and 9) are
related to annual mortality and age mortality and are therefore only
available in the Individual output.</p>
<p>Let’s look at a specific trajectory.</p>
<pre class="r"><code># find all individuals with a 10-step path:
Inds <- names(which(table(steps$IndID) == 10))
steps_10 <- subset(steps, IndID %in% Inds)
# look at first individual with a starting path
(steps_i <- subset(steps_10, IndID == subset(steps_10, Step == 0)[1,"IndID"]))</code></pre>
<pre><code>## Year IndID Step x y Status
## 165 0 867 0 25 82 0
## 166 0 867 1 24 82 1
## 532 0 867 2 23 82 1
## 994 0 867 3 22 82 1
## 1213 0 867 4 21 82 1
## 1377 0 867 5 20 82 1
## 1543 0 867 6 19 82 1
## 1959 0 867 7 19 81 1
## 2012 0 867 8 19 80 1
## 2377 0 867 8 19 80 7</code></pre>
<p>This list of steps begins with the starting cell in the natal patch
(status 0), followed by 8 SMS steps that record the intermediate cells,
and ends with the dispersal outcome; in this case the individual died
during dispersal from the set step-mortality (status 7). Note that
<code>x</code> and <code>y</code> refer to the cell count in each
direction, not the actual position in meters.</p>
<p>We look at another 10-step dispersal event, one that already starts
with a step number above the maximum number of steps per year
(<code>MaxStepsYear</code>):</p>
<pre class="r"><code>(steps_i <- subset(steps, IndID == subset(steps_10, Step > 20)[1,"IndID"]) )</code></pre>
<pre><code>## Year IndID Step x y Status
## 8484 20 16815 21 214 109 1
## 8693 20 16815 22 213 108 1
## 8964 20 16815 23 214 108 1
## 9133 20 16815 24 213 107 1
## 9169 20 16815 25 212 107 1
## 9442 20 16815 26 211 108 1
## 9534 20 16815 27 212 109 1
## 9690 20 16815 28 211 110 1
## 9854 20 16815 29 211 111 1
## 9935 20 16815 29 211 111 7</code></pre>
<p>This list doesn’t start with with status 0, so we know that this
event has begun already in an earlier year and is now being continued.
This happens if an individual is forced to wait until the next year to
continue dispersal because it reached the maximum yearly number of steps
(<code>MaxStepsYear</code>). This outcome will be denoted by status 3 at
the end of the previous year. During the wait time, the individual is
subject to annual survival and might die from demographic mortality
(status 8). In above case, settlement terminates again unsuccessfully
(status 7).</p>
<p>Now that we know how to interpret the data, let’s plot some
trajectories. Since a single dispersal event can be allowed to span
multiple years and because we haven’t recorded the SMS steps of every
year but only every 10th year, there might be incomplete trajectories in
the data. We plot all dispersal events that started in year <em>70</em>
(i.e. before the first landscape transition):</p>
<pre class="r"><code># find all dispersing individuals of the last 10 years of the first map
steps_map1 <- subset(steps, Year >= 70 & Year < 80)
Inds <- subset(steps_map1, Status == 0, select = IndID) # Trajectory contains start of path
# trajectory is completed within one year if it starts with status 0 and ends with status 4 or greater
Inds_outcome <- sapply(Inds$IndID, function(Ind){
steps_i <- subset(steps_map1, IndID == Ind)
outcome = steps_i$Status[nrow(steps_i)]
if(outcome %in% c(2,3) ) "wait" else {
if(outcome %in% c(4,5) ) "settle" else {
if(outcome>4) "die"
}
}
})
Inds <- data.frame(IndID = Inds, Outcome = Inds_outcome)
# load raster of first map
map1 <- terra::rast(paste0(dirpath,"Inputs/",landnames[1]))
# multiply x and y counts by map resolution
steps_map1[,c(4,5)] <- steps_map1[,c(4,5)]*res(map1)
# We use ggplot to plot the movement paths
pathplot_1 <- ggplot()+
geom_tile(data = as.data.frame(map1, xy = TRUE), aes(x = x, y = y,fill = factor(map_01))) +
geom_path(data = steps_map1[steps_map1$IndID %in% Inds[Inds$Outcome == "wait",1],], aes(x = x, y = y, group = factor(IndID)), col = "mediumorchid", alpha = .7, lwd=2) +
geom_path(data = steps_map1[steps_map1$IndID %in% Inds[Inds$Outcome == "settle",1],], aes(x = x, y = y, group = factor(IndID)), col = "blue2", alpha = .7, lwd=2) +
geom_path(data = steps_map1[steps_map1$IndID %in% Inds[Inds$Outcome == "die",1],], aes(x = x, y = y, group = factor(IndID)), col = "red", alpha = .7, lwd=2) +
scale_fill_manual(breaks = 1:5, values = mycol_terrain) +
guides(fill = "none", color = "none") +
theme_void()
pathplot_1</code></pre>
<p><img src="tutorial_3_files/figure-html/unnamed-chunk-26-1.png" width="672" /></p>
<p>The blue paths indicate successful dispersal events, the purple paths
indicate individual dispersal events that paused due to the maximum
numbers of steps per year and will continue in the following year (which
we have not logged), and the red paths indicate unsuccessful dispersal
events where the individuals died during dispersal.</p>
<p>Let’s compare to the movement paths after road construction.</p>
<pre class="r"><code># find all dispersing individuals during last 10 years of road scenario
steps_map4 <- subset(steps, Year >= 190)
Inds <- subset(steps_map4, Status == 0, select = IndID) # Trajectory contains start of path
# trajectory is completed within one year if it starts with status 0 and ends with status 4 or greater
Inds_outcome <- sapply(Inds$IndID, function(Ind){
steps_i <- subset(steps_map4, IndID == Ind)
outcome = steps_i$Status[nrow(steps_i)]
if(outcome %in% c(2,3) ) "wait" else {