-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtutorial_4.html
More file actions
1151 lines (1046 loc) · 43.9 KB
/
tutorial_4.html
File metadata and controls
1151 lines (1046 loc) · 43.9 KB
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 4: Evolution of dispersal during range shifting</title>
<script src="site_libs/header-attrs-2.29/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.5.2/css/all.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.5.2/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;
margin-bottom: 0em;
}
.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:RangeShiftR@uni-potsdam.de">
<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 4: Evolution of dispersal during
range shifting</h1>
</div>
<script>
addClassKlippyTo("pre.r, pre.markdown");
addKlippy('right', 'top', 'auto', '1', 'Copy code', 'Copied!');
</script>
<p>In this tutorial, we will revisit the third example provided in <span
class="citation">Bocedi et al. (2014)</span>. We will use the same
parameters as the original publication and the accompanying tutorial
based on the Windows GUI.</p>
<p>With this example, we show how <code>RangeShiftR</code> can be used
for theoretical applications exploring eco-evolutionary dynamics.
Specifically, we model the evolution of dispersal strategies (emigration
probability, dispersal distance or both) across the geographic range of
a hypothetical species. The species’ range is assumed to be structured
along a linear environmental gradient, which is shifted northwards for a
period of time at a constant rate. This illustrates phenomena such as
evolution of dispersal along stationary gradients <span
class="citation">(Dytham 2009)</span>, evolutionary rescue of the
species’ range during environmental changes through evolution of
dispersal <span class="citation">(Henry, Bocedi, and Travis
2013)</span>, correlation between the evolution of two dispersal traits,
and how the latter can influence the extent and pattern of the rescue
process.</p>
<p>The simulations are run on artificial landscapes with an imposed
North-South gradient. The population is initialised in the southern part
of the landscape and dispersal traits evolve under equilibrium
conditions for <em>500</em> years. Then, the suitable habitat shifts
northwards with a fixed rate per year. In consequence, we should see a
stronger selection towards increased dispersal. After another
<em>300</em> years, the habitat remains constant again and the
individuals can adapt to equilibrium conditions once more.</p>
<p>We will run three experiments:</p>
<ol style="list-style-type: lower-alpha">
<li>only emigration probability evolves,</li>
<li>only dispersal distance evolves, and</li>
<li>both traits evolve.</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 the package and set the relative path from your
current working directory to the RS directory.</p>
<pre class="r"><code>library(RangeShiftR)
library(terra)
library(RColorBrewer)
library(viridis)
library(grid)
library(gridExtra)
# relative path from working directory:
dirpath = "Tutorial_04/"
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>This tutorial does not need any additional input files as the
simulations are set within an artificial landscape.</p>
</div>
</div>
<div id="scenario-a-evolution-of-emigration-probability"
class="section level1" number="2">
<h1><span class="header-section-number">2</span> Scenario a: evolution
of emigration probability</h1>
<div id="landscape-parameters" class="section level2" number="2.1">
<h2><span class="header-section-number">2.1</span> Landscape
parameters</h2>
<p>We use the in-built landscape generator to create artificial random
discrete landscapes of <em>50</em> columns (<em>x</em>) and <em>800</em>
rows (<em>y</em>), with <em>30%</em> of the cells being suitable habitat
and the rest being unsuitable for the species. We set a (maximum)
carrying capacity of <em>100 inds/ha</em> and leave the resolution at
its default of <em>100m</em>. A new random landscape will be created for
each replicate run.</p>
<pre class="r"><code>land <- ArtificialLandscape(propSuit = 0.3,
K_or_DensDep = 100,
dimX = 50,
dimY = 800,
continuous = FALSE)</code></pre>
</div>
<div id="simulation-parameters" class="section level2" number="2.2">
<h2><span class="header-section-number">2.2</span> Simulation
parameters</h2>
<p>Next, we specify the simulation module, which not only defines the
basic simulation and output parameters, but also holds the options to
set an <em>Environmental gradient</em>. For this tutorial, we choose a
<em>shifting</em> gradient in carrying capacity <em>K</em>.</p>
<p>The required parameters are the gradient steepness defined as
fraction of local carrying capacity <em>K(x,y)</em> per cell, the
(initial) location of the optimum (unaffecetd <em>K</em>) given as
<em>y</em>-coordinate, the shifting rate defined as cells per year (in
<em>y</em>-direction) as well as the start and end years of the gradient
shift.</p>
<p>Overall, we simulate <em>20</em> replications for <em>1300</em> years
each. As outputs we store information on the range, the individuals and
the (mean) ‘traits by rows’ in <em>50</em>-year intervals:</p>
<pre class="r"><code>sim_0 <- Simulation(Simulation = 0,
Replicates = 20,
Years = 1300,
Gradient = 1, # gradient in carrying capacity
GradSteep = 0.02,
Optimum = 100,
f = 0.0, # turn off local heterogeneity
Shifting = TRUE,
ShiftRate = 1,
ShiftStart = 500,
ShiftEnd = 800,
OutIntPop = 0,
OutIntInd = 50,
OutIntRange = 5,
OutIntTraitRow = 50)</code></pre>
<p>Note that we actively disable population output (<em>OutIntPop =
0</em>) since it is switched on by default.</p>
</div>
<div id="species-parameters" class="section level2" number="2.3">
<h2><span class="header-section-number">2.3</span> Species
parameters</h2>
<p>Next, we set up our model species. We assume a simple asexual model
and non-overlapping generations with set maximum growth rate:</p>
<pre class="r"><code>demo <- Demography(Rmax = 4.0)</code></pre>
<p>In the dispersal module, we enable inter-individual variability in
emigration probability. Therefore, we need to set the mean and standard
deviation of the distribution of this trait in the initial population,
as well as the scaling factor that relates the allele scale with the
trait scale.</p>
<p>We leave the default options for <code>Settlement()</code>, and in
<code>Transfer()</code> we only set the kernel mean to <em>200m</em>
while keeping the defaults for all other options.</p>
<pre class="r"><code>emig_a <- Emigration(IndVar = TRUE,
EmigProb = matrix(c(0.15, 0.05), ncol = 2),
TraitScaleFactor = 0.05)
disp_a <- Dispersal(Emigration = emig_a,
Transfer = DispersalKernel(Distances = 200),
Settlement = Settlement() )</code></pre>
</div>
<div id="genetic-parameters" class="section level2" number="2.4">
<h2><span class="header-section-number">2.4</span> Genetic
parameters</h2>
<p>We use a simple <code>Genetics()</code> setup to make the emigration
probability a heritable trait. It defines one chromosome with three loci
that code for this trait, as well as the mutation probability and
masnitude and the crossover probability.</p>
<pre class="r"><code>gene <- Genetics(Architecture = 0,
NLoci = 3,
ProbMutn = 0.001,
MutationSD = 1.0,
ProbCross = 0.3,
AlleleSD = 0.1)</code></pre>
</div>
<div id="initialisation-parameters" class="section level2" number="2.5">
<h2><span class="header-section-number">2.5</span> Initialisation
parameters</h2>
<p>We initialise the population in the southern part of the landscape up
to <em>y=200</em>; all suitable cells within that range are initialised
at their local carrying capacity:</p>
<pre class="r"><code>init <- Initialise(InitDens = 0,
maxY = 200)</code></pre>
</div>
<div id="run-the-simulation" class="section level2" number="2.6">
<h2><span class="header-section-number">2.6</span> Run the
simulation</h2>
<p>Create the simulation parameter master from the individual
modules:</p>
<pre class="r"><code>s <- RSsim(land = land, demog = demo, dispersal = disp_a, simul = sim_0, gene = gene, init = init, seed = 987)</code></pre>
<p>Before we run the simulation, let’s get an overview of the
settings:</p>
<pre class="r"><code>s</code></pre>
<pre><code>## Batch # 1
## Seed = 987 (fixed seed)
##
## Simulation # 0
## -----------------
## Replicates = 20
## Years = 1300
## Absorbing = FALSE
## Shifting Environmental Gradient in K_or_DensDep:
## G = 0.02, y_opt = 100
## f = 0
## ShiftRate = 1 rows per year; from year 500 to 800
## File Outputs:
## Range, every 5 years
## Individuals, every 50 years, starting year 0
## Traits/row, every 50 years, starting year 0
##
## Artificial landscape: random structure, binary habitat/matrix code
## Size : 50 x 800 cells
## Resolution : 100 meters
## Proportion of suitable habitat: 0.3
## K or 1/b : 100
##
## Demography:
## Unstructured population:
## Rmax : 4
## bc : 1
## Reproduction Type : 0 (female only)
##
## Dispersal:
## Emigration:
## IndVar = TRUE
## Emigration probabilities:
## [,1] [,2]
## [1,] 0.15 0.05
## TraitScaleFactor = 0.05
##
## Transfer:
## Dispersal Kernel
## Dispersal kernel traits:
## [,1]
## [1,] 200
## Constant mortality prob = 0
##
## Settlement:
## Settlement conditions:
## [,1]
## [1,] 0
## FindMate = FALSE
##
## Genetics:
## Architecture = 0 : One chromosome per trait
## with 3 loci per chromosome.
## ProbMutn = 0.001
## ProbCross = 0.3
## AlleleSD = 0.1
## MutationSD = 1
##
## Initialisation:
## InitType = 0 : Free initialisation
## of all suitable cells/patches.
## InitDens = 0 : At K_or_DensDep
## MaxY = 200</code></pre>
<p>If everything looks alright, we are ready to run the simulation (in
the specified RS directory):</p>
<pre class="r"><code>RunRS(s, dirpath)</code></pre>
</div>
<div id="plot-results" class="section level2" number="2.7">
<h2><span class="header-section-number">2.7</span> Plot results</h2>
<p>To get a first impression of our simulation results, we look at the
time series for total abundance and the number of occupied cells:</p>
<pre class="r"><code>par(mfrow=c(1,2))
plotAbundance(s, dirpath, rep=F, sd=T)
plotOccupancy(s, dirpath,rep=F, sd=T)</code></pre>
<p><img src="tutorial_4_files/figure-html/unnamed-chunk-12-1.png" width="672" /></p>
<p>We observe an initial stabilisation phase in the first <em>500</em>
years, since we initialised all suitable cells at carrying capacity.
During the shift in habitat / carrying capacity between years
<em>500</em> and <em>800</em>, we see a drastic decline in both
abundance and occupancy, which recovers quickly after the shifting stops
in year <em>800</em>.</p>
<p>In order to analyse the evolution of heritable traits - in this case
the emigration probability - we need to process the <em>individuals</em>
output. It contains information about the position and trait values
(among other things) of all individuals at specified time intervals.
Since this can amount to quite large amounts of data, a separate output
file is generated for each replicate.</p>
<p>Like demonstrated in <span class="citation">(Bocedi et al.
2014)</span>, we will create a time series of maps showing the mean
trait value in each cell. For simplicity, we focus on a single replicate
only, although it is straightforward to calculate the trait average over
all replicates.</p>
<p>Let’s read the data file and turn it into a raster stack. The values
of the emigration probability trait are listed in the column named
<em>EP</em>:</p>
<pre class="r"><code># load individuals file for replicate 7 (not that the files names start counting with 0):
inds_rep07 <- read.table(paste0(dirpath,"Outputs/Batch1_Sim0_Land1_Rep6_Inds.txt"), header = T)
ext <- terra::ext(0, 50, 0, 700)
inds_stack <- terra::rast()
for(year in seq(0,1250,by = 50)){
inds_sub <- subset.data.frame(inds_rep07, Year==year)
inds_sub <- aggregate(EP ~ X + Y, data = inds_sub, mean)
inds_sub[,1:2] <- inds_sub[,1:2]+0.5
inds_r <- terra::rast(inds_sub, type="xyz")
inds_r <- terra::extend(inds_r, ext)
names(inds_r) <- year
inds_stack <- c(inds_stack, inds_r)
}</code></pre>
<p>Plot the maps:</p>
<pre class="r"><code>plot(inds_stack,
col=hcl.colors(20, palette = "viridis", rev = TRUE),
mar=c(0,0,0,0),
legend=F, # we will plot the legend later
axes=F,
pax=list(side=2),
box=T,
nc= 26,# plot all layers in one row
maxnl = 26,
type="continuous",
cex.main = 0.8
)
# add a costum legend
legend(x="bottomright", legend = c(round(max(values(inds_stack),na.rm=T),2),"","","",round(max(values(inds_stack),na.rm=T)/2,2),"","","",0.0), fill=hcl.colors(9, palette = "viridis", rev = F),bty="o", horiz=F, border=NA, cex=0.8, y.intersp = 0.5)</code></pre>
<p><img src="tutorial_4_files/figure-html/unnamed-chunk-14-1.png" width="672" /></p>
<p>In equilibrium conditions, when the potential range is stationary,
selection against dispersal prevails across the species’ range except
near the margin, where the emigration probability evolves to be higher.
When the environmental gradient starts to shift, the species lags behind
its suitable environmental space mainly due the very low emigration
probability that evolved throughout most of the range. At the same time,
selection for increased dispersal occurs, especially at the leading
edge. In consequence, emigration probability evolves to be higher, and
the trait surfs back towards the centre and rear edge of the range. This
‘rescue effect’ enables the species to keep up with the shifting
environment. After the shifting stops, high emigration is not
advantageous anymore for the increased risk of dispersal mortality, and
the trait gradually evolves back to values observed prior to
environmental change.</p>
<p>We can also map the mean dispersal distances per cell, which can be
extracted from the individuals output file under the column name
<em>DistMoved</em>. It gives similar results:</p>
<pre class="r"><code>inds_stack <- terra::rast()
for(year in seq(0,1250,by = 50)){
inds_sub <- subset.data.frame(inds_rep07, Year==year)
inds_sub <- aggregate(DistMoved ~ X + Y, data = inds_sub, mean)
inds_sub[,1:2] <- inds_sub[,1:2]+0.5
inds_r <- terra::rast(inds_sub, type="xyz")
inds_r <- terra::extend(inds_r, ext)
names(inds_r) <- year
inds_stack <- c(inds_stack, inds_r)
}
plot(inds_stack,
breaks=seq(0,300,length.out=20),
col=hcl.colors(20, palette = "viridis", rev = TRUE),
mar=c(0,0,0,0),
legend=F, # we will plot the legend later
axes=F,
pax=list(side=2),
box=T,
nc= 26,# plot all layers in one row
maxnl = 26,
type="continuous",
cex.main = 0.8
)
# add a costum legend
legend(x="bottomright", legend = c(300,"","","",150,"","","",0), fill=hcl.colors(9, palette = "viridis", rev = F),bty="o", horiz=F, border=NA, cex=0.8, y.intersp = 0.5)</code></pre>
<p><img src="tutorial_4_files/figure-html/unnamed-chunk-15-1.png" width="672" /></p>
<p>To yield a summary of the trait evolution, we can simply use the
aggregated information stored in the ‘mean traits by rows’ output file.
It contains a time series of the heritable trait values, averaged over
the <em>x</em>-coordinates. This can be useful to avoid the large
storage amount for the individuals output.</p>
<pre class="r"><code># load 'traits by rows' output file
trait_ts <- read.table(paste0(dirpath,"Outputs/Batch1_Sim0_Land1_TraitsXrow.txt"), header = T)
# plot the time series for the replicate chosen above:
trait_ts_07 <- subset.data.frame(trait_ts, Rep==7)</code></pre>
<pre class="r"><code>plot(NULL, type = "n", ylab = "Emigration Prob.", xlab = "y coordinate", xlim=c(0, 500), ylim=c(0,.2), main = "Trait time-series (Replicate 7)")
years <- c(500, 650, 800, 1200)
cols <- hcl.colors(length(years), palette = "Dark 3", alpha = 1, rev = FALSE)
leg.txt <- c()
for(i in 1:length(years)) {
trait_ts_07_yr <- subset.data.frame(trait_ts_07, Year==years[i])
polygon(c(trait_ts_07_yr$y,rev(trait_ts_07_yr$y)),
c((trait_ts_07_yr$meanEP+trait_ts_07_yr$stdEP), rev(pmax(0,trait_ts_07_yr$meanEP-trait_ts_07_yr$stdEP))),
border=NA, col='grey80')
lines(trait_ts_07_yr$y, trait_ts_07_yr$meanEP, type = "l", lwd = 1, col = cols[i])
leg.txt <- c(leg.txt, paste("Year", years[i]))
}
legend("topleft", leg.txt, col = cols, lwd = 1.5)</code></pre>
<p><img src="tutorial_4_files/figure-html/unnamed-chunk-17-1.png" width="672" /></p>
<p>We see that the population moves towards northern latitudes (higher
<em>y</em>) with increasing time. During range shifting (here, years
<em>650</em> and <em>800</em>), the emigration probabilities are
generally higher than before (year <em>500</em>) and after (year
<em>1200</em>) the shift. Moreover, the variation in emigration
probability is much lower during range shifting as selection for
dispersal is high. In contrast, before and after the shift we observe
large variation in emigration probability, with lower emigration in the
range core and higher emigration towards the range margins.</p>
</div>
</div>
<div id="scenario-b-evolution-of-dispersal-distance"
class="section level1" number="3">
<h1><span class="header-section-number">3</span> Scenario b: evolution
of dispersal distance</h1>
<p>We now investigate the same eco-evolutionary process of range
shifting, but this time we keep the emigration probability fixed and
instead let the mean dispersal distance evolve.</p>
<div id="dispersal-parameters" class="section level2" number="3.1">
<h2><span class="header-section-number">3.1</span> Dispersal
parameters</h2>
<p>To achieve this, we can leave the demography and other modules as
they are, but we have to define a new dispersal object. This time, we
activate the inter-individual variation in the <code>Transfer</code>
module, which is again a simple dispersal kernel, and we set the mean
and standard deviation for the mean dispersal distance. Also, we set the
emigration probability to a constant and leave the default options for
settlement.</p>
<pre class="r"><code>trans_b <- DispersalKernel(IndVar = TRUE,
Distances = matrix(c(250, 50), ncol = 2),
TraitScaleFactor = 50)
disp_b <- Dispersal(Emigration = Emigration(EmigProb = 0.1),
Transfer = trans_b,
Settlement = Settlement() )</code></pre>
<p>In addition, we change the simulation index to 1 in the simulation
parameter object, in order to avoid overwriting the previous output.
(All other options stay the same. You can disable the
<em>individuals</em> output with <code>OutIntInd = 0</code> to speed up
the computation, and in case you are only interested in the
summary).</p>
<pre class="r"><code>sim_1 <- Simulation(Simulation = 1,
Replicates = 20,
Years = 1300,
Gradient = 1,
GradSteep = 0.02,
Optimum = 100,
f = 0.0,
Shifting = TRUE,
ShiftRate = 1,
ShiftStart = 500,
ShiftEnd = 800,
OutIntPop = 0,
OutIntInd = 50,
OutIntRange = 5,
OutIntTraitRow = 50)</code></pre>
<p>Finally, the new modules are added to the old parameter master to
define a new one, <em>s_b</em>.</p>
<pre class="r"><code>s_b <- s + disp_b + sim_1</code></pre>
</div>
<div id="run-the-simulation-and-plot-summary" class="section level2"
number="3.2">
<h2><span class="header-section-number">3.2</span> Run the simulation
and plot summary</h2>
<p>Run the new simulation:</p>
<pre class="r"><code>RunRS(s_b, dirpath)</code></pre>
<p>Using similar plots as in scenario (a), we can see how dispersal
distances evolve in a similar way as emigration probability, with
generally higher mean dispersal distances evolving during range
shifting, and higher dispersal distances towards the leading range
edge.</p>
<p>In the output files, the heritable trait of dispersal distance is
named <em>distI</em>. We plot the trait values averaged over <em>x</em>
for the same time steps as we did for scenario (a):</p>
<pre class="r"><code># load 'traits by rows' output file
trait_ts <- read.table(paste0(dirpath,"Outputs/Batch1_Sim1_Land1_TraitsXrow.txt"), header = T)
# plot the time series for the replicate chosen above:
trait_ts_07 <- subset.data.frame(trait_ts, Rep==7)</code></pre>
<p>Plot the average trait values along latitude:</p>
<pre class="r"><code>plot(NULL, type = "n", ylab = "Mean dispersal distance [m]", xlab = "y coordinate", xlim=c(0, 500), ylim=c(150,450), main = "Trait time-series (Replicate 7)")
years <- c(500, 650, 800, 1200)
cols <- hcl.colors(length(years), palette = "Dark 3", alpha = 1, rev = FALSE)
leg.txt <- c()
for(i in 1:length(years)) {
trait_ts_07_yr <- subset.data.frame(trait_ts_07, Year==years[i])
polygon(c(trait_ts_07_yr$y,rev(trait_ts_07_yr$y)),
c((trait_ts_07_yr$mean_distI+trait_ts_07_yr$std_distI), rev(pmax(0,trait_ts_07_yr$mean_distI-trait_ts_07_yr$std_distI))),
border=NA, col='grey80')
lines(trait_ts_07_yr$y, trait_ts_07_yr$mean_distI, type = "l", lwd = 1, col = cols[i])
leg.txt <- c(leg.txt, paste("Year", years[i]))
}
legend("bottomright", leg.txt, col = cols, lwd = 1.5)</code></pre>
<p><img src="tutorial_4_files/figure-html/unnamed-chunk-23-1.png" width="672" /></p>
<p>Dispersal distances for the years <em>800</em> and <em>1200</em> are
very similar and the lines largely overlap, indicating no substantial
change in the trait values after range shifting.</p>
<p>With a fixed emigration probability of <em>0.1</em>, dispersal
distances are not under such strong selection as emigration probability
was, and more variability is maintained under a stationary range. This
might explain why, when the environment starts to shift, the species
does not decline as much before, when only emigration probability
evolved (see below, and Figure 4e in the paper). Moreover, after the
shifting stops, the mean distances stay at the new level for a long time
and do not evolve back towards lower values over the course of the
simulation.</p>
</div>
</div>
<div
id="scenario-c-simultaneous-evolution-of-emigration-probability-and-dispersal-distance"
class="section level1" number="4">
<h1><span class="header-section-number">4</span> Scenario c:
simultaneous evolution of emigration probability and dispersal
distance</h1>
<p>Lastly, we want to model the simultaneous evolution of emigration
probabilities and dispersal distances, as in reality dispersal
strategies are likely to be determined by a suite of traits that come
under selection. However, limited theoretical work has been done to look
at the evolution of such ‘dispersal syndromes’ <span
class="citation">(Travis et al. 2012)</span>.</p>
<div id="dispersal-parameters-1" class="section level2" number="4.1">
<h2><span class="header-section-number">4.1</span> Dispersal
parameters</h2>
<p>Again, we create a new dispersal module. In it, we can re-use the
emigration and transfer modules from scenarios (a) and (b):</p>
<pre class="r"><code>disp_c <- Dispersal(Emigration = emig_a,
Transfer = trans_b,
Settlement = Settlement() )</code></pre>
<p>The new simulation module:</p>
<pre class="r"><code>sim_2 <- Simulation(Simulation = 2,
Replicates = 20,
Years = 1300,
Gradient = 1,
GradSteep = 0.02,
Optimum = 100,
f = 0.0,
Shifting = TRUE,
ShiftRate = 1,
ShiftStart = 500,
ShiftEnd = 800,
OutIntPop = 0,
OutIntInd = 50,
OutIntRange = 5,
OutIntTraitRow = 50)</code></pre>
<p>The new parameter master <em>s_c</em>:</p>
<pre class="r"><code>s_c <- s + disp_c + sim_2</code></pre>
</div>
<div id="compare-population-dynamics" class="section level2"
number="4.2">
<h2><span class="header-section-number">4.2</span> Compare population
dynamics</h2>
<pre class="r"><code>RunRS(s_c, dirpath)</code></pre>
<p>Let’s compare the time series for total abundance and the proportion
of occupied habitat for all three scenarios (Fig. 4b in <span
class="citation">Bocedi et al. (2014)</span>):</p>
<pre class="r"><code>cols <- hcl.colors(3, palette = "Dark 2", alpha = 1, rev = FALSE)
par(mfrow=c(1,2))
rep_means_a <- aggregate(NInds~Year, data = readRange(s , dirpath), FUN = "mean")
rep_means_b <- aggregate(NInds~Year, data = readRange(s_b, dirpath), FUN = "mean")
rep_means_c <- aggregate(NInds~Year, data = readRange(s_c, dirpath), FUN = "mean")
plot(NInds~Year, data=rep_means_a, type = "l", lwd = 2, col = cols[1], ylab = "Total abundance", xlab = "Year")
lines(NInds~Year, data=rep_means_b, type = "l", lwd = 2, col = cols[2])
lines(NInds~Year, data=rep_means_c, type = "l", lwd = 2, col = cols[3])
leg.txt <- c("Scenario a","Scenario b","Scenario c")
legend("topright", leg.txt, col = cols, lwd = 1.5, bty='n')
rep_means_a <- aggregate(Occup.Suit~Year, data = readRange(s , dirpath), FUN = "mean")
rep_means_b <- aggregate(Occup.Suit~Year, data = readRange(s_b, dirpath), FUN = "mean")
rep_means_c <- aggregate(Occup.Suit~Year, data = readRange(s_c, dirpath), FUN = "mean")
plot(Occup.Suit~Year, data=rep_means_a, type = "l", lwd = 2, col = cols[1], ylab = "Proportion of occupied habitat", xlab = "Year")
lines(Occup.Suit~Year, data=rep_means_b, type = "l", lwd = 2, col = cols[2])
lines(Occup.Suit~Year, data=rep_means_c, type = "l", lwd = 2, col = cols[3])</code></pre>
<p><img src="tutorial_4_files/figure-html/unnamed-chunk-28-1.png" width="672" /></p>
<p>Notably, the three different scenarios resulted in large differences
in total population abundance and occupancy, changing the range dynamics
during the shifting phase. The largest decrease in population size
during range shifting was observed in scenario (a). In fact, when both
traits evolved in scenario (c), the rescue of the species was much
quicker than when only one trait evolved. Clearly, more work is needed
to understand the role of simultaneous trait evolution in driving
spatial population dynamics, for example in response to climate
change.</p>
</div>
<div id="compare-trait-values" class="section level2" number="4.3">
<h2><span class="header-section-number">4.3</span> Compare trait
values</h2>
<p>The individuals output and the mean traits output files will now
report values for both traits, named <em>EP</em> and <em>distI</em>.</p>
<pre class="r"><code># load 'traits by rows' output file
trait_ts <- read.table(paste0(dirpath,"Outputs/Batch1_Sim2_Land1_TraitsXrow.txt"), header = T)
# plot the time series for the replicate chosen above:
trait_ts_07 <- subset.data.frame(trait_ts, Rep==7)</code></pre>
<p>Plot the average trait values along latitude:</p>
<pre class="r"><code>par(mar=c(5, 4, 4, 5) + 0.1)
plot(NULL, type = "n", ylab = "Emigration Prob.", xlab = "y coordinate", xlim=c(50, 600), ylim=c(0,.45), main = "Trait time-series (Replicate 7)")
years <- c(500, 650, 800, 1200)
cols <- hcl.colors(length(years), palette = "Dark 3", alpha = 1, rev = FALSE)
leg.txt <- c()
for(i in 1:length(years)) {
trait_ts_07_yr <- subset.data.frame(trait_ts_07, Year==years[i])
polygon(c(trait_ts_07_yr$y,rev(trait_ts_07_yr$y)),
c((trait_ts_07_yr$meanEP+trait_ts_07_yr$stdEP), rev(pmax(0,trait_ts_07_yr$meanEP-trait_ts_07_yr$stdEP))),
border=NA, col='grey80')
lines(trait_ts_07_yr$y, trait_ts_07_yr$meanEP, type = "l", lwd = 1, col = cols[i])
leg.txt <- c(leg.txt, paste("Year", years[i]))
}
par(new = T)
plot(NULL, type = "n", xlab="", ylab="", xlim=c(0, 600), ylim=c(0,450), axes = F)
for(i in 1:length(years)) {
trait_ts_07_yr <- subset.data.frame(trait_ts_07, Year==years[i])
polygon(c(trait_ts_07_yr$y,rev(trait_ts_07_yr$y)),
c((trait_ts_07_yr$mean_distI+trait_ts_07_yr$std_distI), rev(pmax(0,trait_ts_07_yr$mean_distI-trait_ts_07_yr$std_distI))),
border=NA, col='grey90')
lines(trait_ts_07_yr$y, trait_ts_07_yr$mean_distI, type = "l", lwd = 1, lty = 3, col = cols[i])