forked from RangeShifter/RangeshiftR-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial_1.html
1087 lines (960 loc) · 42.9 KB
/
tutorial_1.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 1: Range expansion, long-distance dispersal and environmental stochasticity</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 1: Range expansion, long-distance
dispersal and environmental stochasticity</h1>
</div>
<script>
addClassKlippyTo("pre.r, pre.markdown");
addKlippy('right', 'top', 'auto', '1', 'Copy code', 'Copied!');
</script>
<p>This is an example of how <code>RangeShiftR</code> can be used at
national scale for modelling species range dynamics <span
class="citation">(Bocedi et al. 2014)</span>. We model a hypothetical
grassland species distributed initially in the South-West of England,
and assume that from the start of the simulation the species is free to
expand its range. This could be the case for alien species that
naturally start to expand after having gone through an establishment
phase, an alien or native species that have been released from natural
enemies or competitors, or a species for which a previously prohibiting
climate has become suitable.</p>
<p>We assume that we have data about the current species distribution
and use it as a starting point. The objective is to investigate how
different assumptions about the dispersal ability of the species and
about temporal environmental stochasticity can affect the modelled range
expansion. We start with the basic setting of a single dispersal kernel
and no environmental stochasticity.</p>
<div id="simulating-range-expansions" class="section level1" number="1">
<h1><span class="header-section-number">1</span> Simulating range
expansions</h1>
<div id="create-an-rs-directory" class="section level2" number="1.1">
<h2><span class="header-section-number">1.1</span> Create an RS
directory</h2>
<p>The directory in which we run <code>RangeShiftR</code> needs to have
a certain folder structure. It should contain three sub-folders named
‘Inputs’, ‘Outputs’ and ‘Output_Maps’. We can create them from R:</p>
<pre class="r"><code>library(RangeShiftR)
library(terra)
library(RColorBrewer)
library(viridis)
library(grid)
library(gridExtra)
# relative path from working directory:
dirpath = "Tutorial_01/"
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 1 into the ‘Inputs’
folder. The files can be downloaded <a
href="files/Tutorial1_Inputs.zip">here</a>.</p>
</div>
<div id="landscape-parameters" class="section level2" number="1.2">
<h2><span class="header-section-number">1.2</span> Landscape
parameters</h2>
<p>We use a land-cover map of Great Britain at 1km resolution. Six
dominant aggregated habitat types were derived from LandCover Map 2007
<span class="citation">(Morton et al. 2011)</span>. The map,
<em>UKmap_1km.txt</em>, is an ASCII raster in the standard text format,
where each cell holds the code of its dominant habitat type. The habitat
codes have to be given as sequential integer numbers, starting from
one:</p>
<ul>
<li>1 = woodland (broadleaved and conifer)</li>
<li>2 = arable</li>
<li>3 = improved grassland</li>
<li>4 = semi-natural grassland (acid, neutral and calcareous
grassland)</li>
<li>5 = heath and bog</li>
<li>6 = other (urban, water & coastal habitats)</li>
</ul>
<p>Furthermore, we are provided with a map that defines the (initial)
species distribution, named <em>Species_Distribution_10km.txt</em>. It
is given at a coarser resolution of 10km.</p>
<p>Let’s plot the landscape map and overlay it with the initial species
distribution:</p>
<pre class="r"><code>UKmap <- terra::rast(paste0(dirpath, "Inputs/UKmap_1km.txt"))
SpDist <- terra::rast(paste0(dirpath, "Inputs/Species_Distribution_10km.txt"))
values(SpDist)[values(SpDist) < 1] <- NA
# plot land cover map and highlight cells with initial species distribution - option 1:
plot(UKmap, col=brewer.pal(n = 6, name = "Spectral"), axes=F)
plot(as.polygons(SpDist, dissolve=F), add=T)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-3-1.png" width="672" /></p>
<p>For prettier mapping with a legend specifying the different land
cover categories and a custom colour palette, we need a little
workaround:</p>
<pre class="r"><code># plot land cover map and highlight cells with initial species distribution - option 2 with categorical legend:
UKmap.f <- as.factor(UKmap)
# add the land cover classes to the raster attribute table (RAT)
rat <- levels(UKmap.f)[[1]][-2]
rat[["landcover"]] <- c("woodland", "arable", "improved grassland", "semi-natural grassland", "heath and bog", "other")
levels(UKmap.f) <- rat
custom.pal <- c("#1A9850", "#91CF60", "#D9EF8B", "#FEE08B", "#D8B365", "#777777")
plot(UKmap.f, col=custom.pal, axes=F)
plot(as.polygons(SpDist, dissolve=F), border='red', col=NA, add=T)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-4-1.png" width="672" /></p>
<p>In order to use the habitat and distribution maps in
<code>RangeShiftR</code>, we need to set up a landscape module. It takes
the path to the map files along with some other parameters, like their
resolutions (given in meters) and the number of habitats. Additionally,
we have to specify the demographic density dependence for our target
species in each habitat type, which is given in individuals per hectare
and provided to the argument <code>K_or_DensDep</code>. Care should be
taken, since this value is used differently in the population models for
overlapping and non-overlapping generations. In this example, we use a
simple model with non-overlapping generations (see below), so that the
given values are used as limiting carrying capacities <em>K</em> of each
habitat type. We assume that the species can reproduce only in
semi-natural grassland, which has the code <em>4</em>. Therefore, we set
the carrying capacity for this habitat equal to <em>5 inds/ha</em> and
all others to <em>0</em>, by defining a vector named <em>carrycap</em>
that contains these numbers in the order of increasing habitat codes and
that we give to the argument <code>K_or_DensDep</code> of the imported
landscape module:</p>
<pre class="r"><code># carrying capacitíes and landscape parameter object
carrycap <- c(0, 0, 0, 5, 0, 0)
land <- ImportedLandscape(LandscapeFile = "UKmap_1km.txt",
Resolution = 1000,
Nhabitats = 6,
K_or_DensDep = carrycap,
SpDistFile = "Species_Distribution_10km.txt",
SpDistResolution = 10000)</code></pre>
</div>
<div id="species-parameters" class="section level2" number="1.3">
<h2><span class="header-section-number">1.3</span> Species
parameters</h2>
<p>Next, we specify the species parameters for the demography and
dispersal modules. In this first example, we choose the simplest model
options for both: the population dynamics are described by a female-only
model with non-overlapping generations. This has one compulsory
argument, the maximum growth rate <em>Rmax</em>, that we set to
<em>1.5</em>.</p>
<pre class="r"><code>demo <- Demography(Rmax = 1.5)</code></pre>
<p>The dispersal module comprises three sub-modules representing the
three phases of dispersal: Emigration, Transport and Settlement. We
assume a constant emigration probability of <em>0.1</em>. The transfer
phase is modelled with a dispersal kernel, whose mean distance is set to
<em>2,000m</em>. For the Settlement sub-module, we use the default
options, which assume that an individual will die if it arrives in an
unsuitable cell and settle if it’s suitable.</p>
<pre class="r"><code>disp <- Dispersal(Emigration = Emigration(EmigProb = 0.1),
Transfer = DispersalKernel(Distances = 2000),
Settlement = Settlement() )
# alternative notation:
# disp <- Dispersal() + Emigration(EmigProb = 0.1) + DispersalKernel(Distances = 2000) + Settlement()</code></pre>
</div>
<div id="initialisation-parameters" class="section level2" number="1.4">
<h2><span class="header-section-number">1.4</span> Initialisation
parameters</h2>
<p>In order to control the initial distribution of individuals in the
landscape at year <em>0</em>, we set initialisation rules. We want to
initialise all habitat cells that are located inside the <em>10km</em> x
<em>10km</em> presence cells indicated by the loaded species
distribution map; and we want each of those cells to be initialised at
its respective carrying capacity. These options are encoded in numeric
arguments; for a list of possible settings see the documentation:</p>
<pre class="r"><code>?Initialise
init <- Initialise(InitType = 1, # = initialisation from a loaded species distribution map
SpType = 0, # = all suitable cells within all distribution presence cells
InitDens = 0) # = at carrying capacity</code></pre>
</div>
<div id="simulation-parameters" class="section level2" number="1.5">
<h2><span class="header-section-number">1.5</span> Simulation
parameters</h2>
<p>Lastly, we set some basic simulation parameters, i.e. the simulation
number, number of replicates and number of years to be simulated.
Furthermore, we specify what file output will be generated. In this
example, we choose to output the population, occupancy and range data
for every 5 years.</p>
<pre class="r"><code>sim_0 <- Simulation(Simulation = 0,
Replicates = 20,
Years = 100,
OutIntPop = 5,
OutIntOcc = 5,
OutIntRange = 5)</code></pre>
</div>
<div id="parameter-master" class="section level2" number="1.6">
<h2><span class="header-section-number">1.6</span> Parameter master</h2>
<p>All settings we have made so far are contained in their respective
module objects. They have to be combined to a parameter master object,
which validates all parameter settings and is needed to run the
simulation.</p>
<pre class="r"><code>s <- RSsim(land = land, demog = demo, dispersal = disp, simul = sim_0, init = init)
# alternative notation:
# s <- RSsim() + land + demo + disp + sim_0 + init</code></pre>
<p>Before we run the simulation, let’s get an overview of the settings
we have made by simply typing:</p>
<pre class="r"><code>s</code></pre>
<pre><code>## Batch # 1
##
## Simulation # 0
## -----------------
## Replicates = 20
## Years = 100
## Absorbing = FALSE
## File Outputs:
## Range, every 5 years
## Occupancy, every 5 years
## Populations, every 5 years, starting year 0
##
## Landscape imported from file:
## UKmap_1km.txt
## with 6 unique integer habitat code(s)
## K or 1/b : 0 0 0 5 0 0 [inds per ha].
## Resolution : 1000
## Initial Species Distribution imported from file:
## Species_Distribution_10km.txt
## Resolution : 10000
##
## Demography:
## Unstructured population:
## Rmax : 1.5
## bc : 1
## Reproduction Type : 0 (female only)
##
## Dispersal:
## Emigration:
## Emigration probabilities:
## [,1]
## [1,] 0.1
##
## Transfer:
## Dispersal Kernel
## Dispersal kernel traits:
## [,1]
## [1,] 2000
## Constant mortality prob = 0
##
## Settlement:
## Settlement conditions:
## [,1]
## [1,] 0
## FindMate = FALSE
##
## Initialisation:
## InitType = 1 : Initialisation from loaded species distribution map
## all presence cells/patches.
## InitDens = 0 : At K_or_DensDep</code></pre>
<p>At any stage, you can check for parameter validity of any module or
the master.</p>
<pre class="r"><code>validateRSparams(s)</code></pre>
<pre><code>## [1] TRUE</code></pre>
<p>If it returns <code>TRUE</code>, the parameters are valid. Also,
before actually running the simulation, the <code>RSsim()</code>
function (see next section) will always check the validity of all
settings and give error messages in case something doesn’t match up.</p>
</div>
<div id="run-the-simulation" class="section level2" number="1.7">
<h2><span class="header-section-number">1.7</span> Run the
simulation</h2>
<p>Once all parameters are set and a parameter master object has been
defined, we can run the simulations in the specified RS directory.</p>
<pre class="r"><code>RunRS(s, dirpath)</code></pre>
</div>
<div id="plot-results" class="section level2" number="1.8">
<h2><span class="header-section-number">1.8</span> Plot results</h2>
<p>For convenience and for compliance with the RangeShifter Windows GUI,
the <code>RangeShiftR</code> package contains a few plotting functions
to inspect the population time series of the replicate simulations.</p>
<pre class="r"><code># read 'range' output into a data frame
range_df <- readRange(s, dirpath)
# plot trajectories of all individual runs and overlay with mean:
par(mfrow=c(1,2))
plotAbundance(range_df)
plotOccupancy(range_df)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-14-1.png" width="672" /></p>
<pre class="r"><code># or plot mean and standard deviation:
par(mfrow=c(1,2))
plotAbundance(range_df, rep=F, sd=T)
plotOccupancy(range_df, rep=F, sd=T)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-14-2.png" width="672" /></p>
<p>The population output file contains the local abundances for all
populated cells in the recorded years for all replicates. We can convert
this data frame into raster maps and plot the spatial abundance
distribution.</p>
<pre class="r"><code># read population output file into a data frame
pop_df <- readPop(s, dirpath)
# Not all years have the same number of cells, since only cells that had ever established a population are recorded.
# For later stacking, we need a common extent. This is a quick & dirty solution:
ext <- terra::ext(c(min(pop_df$x)-500,max(pop_df$x)+500,min(pop_df$y)-500,max(pop_df$y)+500))
# Make stack of different raster layers for each year and for only one repetition (Rep==0):
pop_wide_rep0 <- reshape(subset(pop_df,Rep==0)[,c('Year','x','y','NInd')], timevar='Year', v.names=c('NInd'), idvar=c('x','y'), direction='wide')
r_years_rep0 <- terra::rast(pop_wide_rep0, type="xyz")
# Overlay with UK mask
r_years_rep0 <- terra::extend(r_years_rep0, UKmap)
values(r_years_rep0)[is.na(values(r_years_rep0))] <- 0
r_years_rep0 <- terra::mask(r_years_rep0, UKmap)
# Map abundance
plot(r_years_rep0[['NInd.90']], col=c('grey',rev(inferno(150))), axes=F)
plot(as.polygons(SpDist, dissolve=F), border='red', col=NA, add=T)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-15-1.png" width="672" /></p>
<p>We can make similar maps showing the average abundance over all
replicate runs. For convenience, we define a small function for this,
which we can re-use later. When calling this function, we can choose
whether all replicates should be averaged (the default) or whether a
specific replicate should be extracted. Also, we can optionally overlay
a mask, e.g. the UK map.</p>
<pre class="r"><code>stack_pop <- function(pop_df, ext, rep=NULL, mask=NULL){
# This function takes the population data frame output from RangeShiftR and turns it into a raster stack of abundance maps.
# If the ID of the Replicate ("rep") is not provided, it will return the mean abundance over all replicates.
if (!is.null(rep)){
pop_wide <- reshape(subset(pop_df,Rep==rep)[,c('Year','x','y','NInd')], timevar='Year', v.names=c('NInd'), idvar=c('x','y'), direction='wide')
r_years <- terra::rast(pop_wide, type="xyz")
if (!is.null(mask)){
r_years <- terra::extend(r_years, mask)
values(r_years)[is.na(values(r_years))] <- 0
r_years <- terra::mask(r_years, mask)
}
} else {
pop_wide <- lapply(unique(pop_df$Year),FUN=function(year){reshape(subset(pop_df,Year==year)[,c('Rep','x','y','NInd')], timevar='Rep', v.names=c('NInd'), idvar=c('x','y'), direction='wide')})
# for(i in 1:length(pop_wide)){
# if(i==1)r_years <- mean(terra::extend(terra::rast(pop_wide[i][[1]], type="xyz"),ext))
# else r_years <- c(r_years,mean(terra::extend(terra::rast(pop_wide[i][[1]], type="xyz"),ext)))
# }
r_years <- terra::rast(sapply(pop_wide, FUN=function(i){mean(terra::extend(terra::rast(i, type="xyz"),ext))}))
names(r_years) <- paste0('mean.NInd.',unique(pop_df$Year))
names(r_years) <- paste0('mean.NInd.',unique(pop_df$Year))
if (!is.null(mask)){
r_years <- terra::extend(r_years, mask)
values(r_years)[is.na(values(r_years))] <- 0
r_years <- terra::mask(r_years, mask)
}
}
return(r_years)
}</code></pre>
<p>Extracting the maps is much easier now:</p>
<pre class="r"><code># Extract maps of single replicate run:
r_years_rep0 <- stack_pop(pop_df, ext, rep=0, mask=UKmap)
names(r_years_rep0)</code></pre>
<pre><code>## [1] "NInd.0" "NInd.5" "NInd.10" "NInd.15" "NInd.20" "NInd.25"
## [7] "NInd.30" "NInd.35" "NInd.40" "NInd.45" "NInd.50" "NInd.55"
## [13] "NInd.60" "NInd.65" "NInd.70" "NInd.75" "NInd.80" "NInd.85"
## [19] "NInd.90" "NInd.95" "NInd.100"</code></pre>
<pre class="r"><code># Extract maps with averaged abundances over all replicates:
r_years <- stack_pop(pop_df, ext, mask=UKmap)
names(r_years)</code></pre>
<pre><code>## [1] "mean.NInd.0" "mean.NInd.5" "mean.NInd.10" "mean.NInd.15"
## [5] "mean.NInd.20" "mean.NInd.25" "mean.NInd.30" "mean.NInd.35"
## [9] "mean.NInd.40" "mean.NInd.45" "mean.NInd.50" "mean.NInd.55"
## [13] "mean.NInd.60" "mean.NInd.65" "mean.NInd.70" "mean.NInd.75"
## [17] "mean.NInd.80" "mean.NInd.85" "mean.NInd.90" "mean.NInd.95"
## [21] "mean.NInd.100"</code></pre>
<pre class="r"><code># Map abundance of single replicate run in year 90:
plot(r_years_rep0[['NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-17-1.png" width="672" /></p>
<pre class="r"><code># Map mean abundance in year 90:
plot(r_years[['mean.NInd.90']], margin=F,axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-17-2.png" width="672" /></p>
</div>
</div>
<div id="change-simulations" class="section level1" number="2">
<h1><span class="header-section-number">2</span> Change simulations</h1>
<p>We can easily change certain aspects of the simulations by changing
the various modules. Here, we explore the different simulations as shown
in Fig. 2 of <span class="citation">Bocedi et al. (2014)</span>.
Firstly, we add long-distance dispersal. Secondly, we add environmental
stochasticity without and with temporal autocorrelation.</p>
<div id="add-rare-long-distance-dispersal" class="section level2"
number="2.1">
<h2><span class="header-section-number">2.1</span> Add rare
long-distance dispersal</h2>
<p>To explore the effect of rare long-distance dispersal events on the
range expansion, we only need to change the dispersal module. More
specifically, we define a new transfer sub-module with the option
<em>DoubleKernel</em> enabled, to use the double negative exponential
kernel (Mixed kernel). This means that in addition to the standard
dispersal kernel, an individual has a certain probability of dispersing
with a second - a long-distance - dispersal kernel.</p>
<pre class="r"><code># in the dispersal kernel sub-module, the distance parameters are now given as
# Distances = c(MeanDistance-I, MeanDistance-II, probability of dispersing with Kernel-I)
trans_long <- DispersalKernel(DoubleKernel = T, Distances = matrix(c(2000, 10000, 0.99), ncol = 3))
disp_long <- Dispersal(Emigration = Emigration(EmigProb = 0.1),
Transfer = trans_long,
Settlement = Settlement() )</code></pre>
<p>Moreover we change the simulation number to <em>1</em> in the
corresponding parameter object, in order to avoid overwriting the
previous output.</p>
<pre class="r"><code>sim_1 <- Simulation(Simulation = 1,
Replicates = 20,
Years = 100,
OutIntPop = 5,
OutIntOcc = 5,
OutIntRange = 5)</code></pre>
<p>Finally, the new modules are added to the old parameter master to
define a new one, <em>s_long</em>.</p>
<pre class="r"><code>s_long <- s + disp_long + sim_1</code></pre>
<p>With this, run the new simulation:</p>
<pre class="r"><code>RunRS(s_long, dirpath)</code></pre>
<p>We can now compare the result plots. The number of occupied cells as
well as the total abundance is much higher in this scenario.</p>
<pre class="r"><code># Get range results
range_df_long <- readRange(s_long, dirpath)
# Plot total abundance and number of occupied cells:
par(mfrow=c(1,2))
plotAbundance(range_df_long)
plotOccupancy(range_df_long)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-22-1.png" width="672" /></p>
<pre class="r"><code># Map mean abundance:
pop_df_long <- readPop(s_long, dirpath)
ext_long <- terra::ext(c(min(pop_df_long$x)-500,max(pop_df_long$x)+500,min(pop_df_long$y)-500,max(pop_df_long$y)+500))
r_years_long <- stack_pop(pop_df_long, ext_long, mask=UKmap)
plot(r_years_long[['mean.NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-23-1.png" width="672" /></p>
<p>When comparing the maps of mean abundance between the first and
second scenario, these look quite similar. One reason might be that
successful long-distance dispersal is highly stochastic and, thus, high
occupancy only occurs for a few but not for all runs. So, let’s try and
map only the replicate run with the highest occupancy.</p>
<pre class="r"><code># Which replicate had highest occupancy?
head(subset(range_df_long,Year==95)[order(subset(range_df_long,Year==95)$NOccupCells,decreasing=T),])</code></pre>
<pre><code>## Rep Year RepSeason NInds NOccupCells Occup.Suit min_X max_X min_Y
## 20 0 95 0 1117897 3181 0.1338520 211000 569000 45000
## 104 4 95 0 935708 2936 0.1235430 211000 569000 45000
## 83 3 95 0 820045 2633 0.1107930 211000 569000 58000
## 356 16 95 0 783999 2490 0.1047760 211000 569000 45000
## 125 5 95 0 729725 2129 0.0895855 211000 569000 37000
## 272 12 95 0 648651 2089 0.0879024 211000 569000 45000
## max_Y
## 20 296000
## 104 292000
## 83 284000
## 356 282000
## 125 262000
## 272 263000</code></pre>
<pre class="r"><code>rep_x = subset(range_df_long,Year==95)$Rep[which.max(subset(range_df_long,Year==95)$NOccupCells)]
r_years_long_1 <- stack_pop(pop_df_long, ext_long, rep=rep_x, mask=UKmap)
names(r_years_long_1)</code></pre>
<pre><code>## [1] "NInd.0" "NInd.5" "NInd.10" "NInd.15" "NInd.20" "NInd.25"
## [7] "NInd.30" "NInd.35" "NInd.40" "NInd.45" "NInd.50" "NInd.55"
## [13] "NInd.60" "NInd.65" "NInd.70" "NInd.75" "NInd.80" "NInd.85"
## [19] "NInd.90" "NInd.95" "NInd.100"</code></pre>
<pre class="r"><code>plot(r_years_long_1[['NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-24-1.png" width="672" /></p>
<p>We can see that there is a chance for the population to spread to the
north and establish in a broader range. However, only some replicates
show this.</p>
</div>
<div id="add-temporal-environmental-stochasticity"
class="section level2" number="2.2">
<h2><span class="header-section-number">2.2</span> Add temporal
environmental stochasticity</h2>
<p>Lastly, we incorporate temporal environmental stochasticity, which is
recognized to be fundamental for both ecological and evolutionary
processes and is expected to increase in frequency and become more
auto-correlated with climate change.</p>
<p>The options for the environmental stochasticity are set in the
Simulation parameters object. For this example we use <em>global</em>
(referring to the spatial extent) stochasticity in <em>growth rate</em>.
We explore two types of stochasticity: temporally uncorrelated (white
noise) and positively correlated (red noise), by setting the
autocorrelation coefficient <em>ac</em>:</p>
<pre class="r"><code> # for white noise
sim_2 <- Simulation(Simulation = 2,
Replicates = 20,
Years = 100,
EnvStoch = 1, # global environmental stochasticity
EnvStochType = 0, # in growth rate:
std = 0.25, # magnitude of stochastic fluctuations
ac = 0.0, # temporal autocorrelation
minR = 0.5, # minimum growth rate
maxR = 2.5, # maximum growth rate
OutIntPop = 5,
OutIntOcc = 5,
OutIntRange = 5)
s_envstoch_white <- s_long + sim_2</code></pre>
<pre class="r"><code> # for red noise
sim_3 <- Simulation(Simulation = 3,
Replicates = 20,
Years = 100,
EnvStoch = 1, # global environmental stochasticity
EnvStochType = 0, # in growth rate:
std = 0.25, # magnitude of stochastic fluctuations
ac = 0.7, # temporal autocorrelation
minR = 0.5, # minimum growth rate
maxR = 2.5, # maximum growth rate
OutIntPop = 5,
OutIntOcc = 5,
OutIntRange = 5)
s_envstoch_red <- s_long + sim_3</code></pre>
<pre class="r"><code>RunRS(s_envstoch_white, dirpath)
RunRS(s_envstoch_red, dirpath)</code></pre>
<p>Comparing the results of all four simulation runs:</p>
<pre class="r"><code># Get results:
range_df_envstoch_white <- readRange(s_envstoch_white, dirpath)
range_df_envstoch_red <- readRange(s_envstoch_red, dirpath)
par(mfrow=c(2,2),mar=c(3,3,1,1)+.1, tcl=-.1, mgp=c(1.6,.3,0))
# Get common y-range of all 4 simulations
ylim <- c(min(min(range_df$NInds),min(range_df_long$NInds), min(range_df_envstoch_white$NInds), min(range_df_envstoch_red$NInds)), max(max(range_df$NInds),max(range_df_long$NInds), max(range_df_envstoch_white$NInds), max(range_df_envstoch_red$NInds)))
plotAbundance(range_df, ylim = ylim)
plotAbundance(range_df_long, ylim = ylim)
plotAbundance(range_df_envstoch_white, ylim = ylim)
plotAbundance(range_df_envstoch_red, ylim = ylim)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-28-1.png" width="672" /></p>
<pre class="r"><code>par(mfrow=c(2,2),mar=c(3,3,1,1)+.1, tcl=-.1, mgp=c(1.6,.3,0))
ylim <- c(min(min(range_df$NOccupCells),min(range_df_long$NOccupCells), min(range_df_envstoch_white$NOccupCells), min(range_df_envstoch_red$NOccupCells)), max(max(range_df$NOccupCells),max(range_df_long$NOccupCells), max(range_df_envstoch_white$NOccupCells), max(range_df_envstoch_red$NOccupCells)))
plotOccupancy(range_df, ylim = ylim)
plotOccupancy(range_df_long, ylim = ylim)
plotOccupancy(range_df_envstoch_white, ylim = ylim)
plotOccupancy(range_df_envstoch_red, ylim = ylim)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-29-1.png" width="672" /></p>
<pre class="r"><code># Map mean abundance scenario 3
pop_df_envstoch_white <- readPop(s_envstoch_white, dirpath)
ext_envstoch_white <- terra::ext(c(min(pop_df_envstoch_white$x)-500,max(pop_df_envstoch_white$x)+500,min(pop_df_envstoch_white$y)-500,max(pop_df_envstoch_white$y)+500))
r_years_envstoch_white <- stack_pop(pop_df_envstoch_white, ext_envstoch_white, mask=UKmap)
plot(r_years_envstoch_white[['mean.NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-30-1.png" width="672" /></p>
<pre class="r"><code># Which replicate had highest occupancy?
rep_x = subset(range_df_envstoch_white,Year==95)$Rep[which.max(subset(range_df_envstoch_white,Year==95)$NOccupCells)]
r_years_envstoch_white_1 <- stack_pop(pop_df_envstoch_white, ext_envstoch_white, rep=rep_x, mask=UKmap)
plot(r_years_envstoch_white_1[['NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-30-2.png" width="672" /></p>
<pre class="r"><code># Map mean abundance scenario 4
pop_df_envstoch_red <- readPop(s_envstoch_red, dirpath)
ext_envstoch_red <- terra::ext(c(min(pop_df_envstoch_red$x)-500,max(pop_df_envstoch_red$x)+500,min(pop_df_envstoch_red$y)-500,max(pop_df_envstoch_red$y)+500))
r_years_envstoch_red <- stack_pop(pop_df_envstoch_red, ext_envstoch_red, mask=UKmap)
plot(r_years_envstoch_red[['mean.NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-30-3.png" width="672" /></p>
<pre class="r"><code># Which replicate had highest occupancy?
rep_x = subset(range_df_envstoch_red,Year==95)$Rep[which.max(subset(range_df_envstoch_red,Year==95)$NOccupCells)]
r_years_envstoch_red_1 <- stack_pop(pop_df_envstoch_red, ext_envstoch_red, rep=rep_x, mask=UKmap)
plot(r_years_envstoch_red_1[['NInd.90']], margin=F, axes=FALSE, col=c('grey',rev(inferno(150))))
plot(as.polygons(SpDist, dissolve=F), col=NA, border='red', add=TRUE)</code></pre>
<p><img src="tutorial_1_files/figure-html/unnamed-chunk-30-4.png" width="672" /></p>
</div>
</div>
<div id="references" class="section level1 unnumbered">
<h1 class="unnumbered">References</h1>
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Bocedi2014" class="csl-entry">
Bocedi, G., S. C. F. Palmer, G. Pe’er, R. K. Heikkinen, Y. G. Matsinos,
K. Watts, and J. M. J. Travis. 2014. <span>“RangeShifter: A Platform for
Modelling Spatial Eco-Evolutionary Dynamics and Species’ Responses to
Environmental Changes.”</span> <em>Methods in Ecology and Evolution</em>
5 (4): 388–96. <a
href="https://doi.org/10.1111/2041-210X.12162">https://doi.org/10.1111/2041-210X.12162</a>.
</div>
<div id="ref-Morton2011" class="csl-entry">
Morton, D., C. Rowland, C. Wood, L. Meek, C. Marston, G. Smith, R.
Wadsworth, and I.. Simpson. 2011. <span>“Final Report for LCM2007 - the
New UK Land Cover Map.”</span> Countryside Survey Technical Report No
11/07. NERC/Centre for Ecology & Hydrology.
</div>
</div>
</div>
<!DOCTYPE html>