-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispersal simulations.Rmd
2247 lines (1363 loc) · 73.9 KB
/
dispersal simulations.Rmd
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
```{r setup, include=FALSE}
library(tidyverse)
library(dplyr)
library(tidyr)
library(foreach)
library(som.nn)
library(mcomsimr)
```
```{r setting up values for model runs with source sink dynamics}
# Initizalize model setup
patches <- 10 # Number of patches
species <- 2 # Number of species
time <- 100 # Length of time (generations) to run model
results <- array(NA, c(species, patches, time))
results[,,1] <- 1000
###2 species with same competitive ability (a) ####
a <- 1/2000 # Beverton-holt alpha for all species
## s2 species with the same dispersal rate (d) ###
d<- 0.05 ## dispersal###
# Density independent growth rate R###
#### setting up an array of matrices with different R## From No spatial variation, to lots of spatial variation####
species=2
patches=10
meanR=1.5 ###mean growth rate source patch ###
sdR= 0.0 ### std in growth rate### ##if 0, no variation, if >meanR/2, source sink dynamics####
p= 0.4 ### When p = 1, no variation across patches###
Rfunc <- function(species, patches, meanR,sdR, p) {
R <- matrix(NA, nrow=species, ncol=patches)
R[1, 1:(patches/2)] <- rnorm(patches/2, mean = ifelse(p<1, meanR+(1-p), meanR), sd = sdR) ###source sp 1 ####
R[1, ((patches/2)+1):patches] <- rnorm(patches/2, mean = meanR*p, sd = sdR)###sink sp 1####
R[2, 1:(patches/2)] <- rnorm(patches/2, mean = meanR*p, sd = sdR) ### sink sp 2 ####
R[2, ((patches/2)+1):patches] <- rnorm(patches/2, mean = ifelse(p<1, meanR+(1-p), meanR), sd = sdR) ## source sp 2 ####
return(R)
}
R<-Rfunc(species,patches,meanR,sdR,p)
###bookeeping###
speciesnames <- vector("numeric", length=species)
for(i in 1:species){
speciesnames[i] <- paste("species", i)
}
patchnames <- vector("numeric", length=patches)
for(i in 1:patches){
patchnames[i] <- paste("patch", i)
}
timenames <- vector("numeric", length=time)
for(i in 1:time){
timenames[i] <- paste("timestep", i)
}
```
```{r deconstruct coexistence random movement}
##Determining Chessonian coexistence strength and contributions from
# the storage effect, fitness density covariance, and nonspatial fitness
runs <- 1 # Number of runs (for stochastic models)
nonspatial <- storage <- fitness <- overall <- check <- rep(NA, runs)
invader <- 1 # Resident dynamics (all species that are not the invader) run to equilibrium.
results <- array(NA, c(species, patches, time))
results[,,1] <- 1000
results[invader,,1] <- 0 # Remove the invader species from the metacommunity
# Running the model to equilibrium without the invader
modelRun <- rdet(R,d,a,patches,species,results,time)
# Then run invader to equilibrium spatial distribution, holding the global
# abundance to a very low density
iabun <- .005*(sum(modelRun[,,time]))
invade <- rep(iabun/patches,patches) ##divides equally among the patches##
# Set up array to store results with the invader at low density
co_results <- array(NA, c(species, patches, time))
co_results[,,1] <- modelRun[,,time]
co_results[invader,,1] <- invade # species i invades at time step 1
modelRunChesson <- rdetChesson(R,d,a,patches,species,co_results,invader,iabun,time) ##each patch at it's equilibrium densityy##
# Now do one more time step, keeeping track of changes in population before vs. after
N_start <- modelRunChesson[,,time] # starting population size
N_startj <- N_start[-invader,] # j is all resident species
N_starti <- N_start[invader,] # i is invader species
# Determine relative abundances
v <- matrix(NA, nrow=species, ncol=patches)
for (i in 1:species) {
v[i,] <- N_start[i,]/mean(N_start[i,])
}
v_i <- v[invader,]
v_j <- v[-invader,]
# Determine fitness
N_birth <- b(R, N_start, patches, species, a) # Population after 1 time step
N_birthi <- N_birth[invader,]
N_birthj <- N_birth[-invader,]
# Calculate average fitness across the metacommunity
R_bar <- rep(NA,species)
for (i in 1:species) {
R_bar[i] <- mean(R[i,])
}
R_bari <- R_bar[invader]
R_barj <- R_bar[-invader]
# Calculate environmental component
E <- R/R_bar
Ei <- E[invader,]
Ej <- E[-invader,]
# Calculate competitive term
C <- matrix(NA, nrow=species, ncol=patches)
for (p in 1: patches) {
C[,p] <-rep(1-bevHoltC(N_start[,p],a), species)
}
C <- C[invader,]
#Overall invasion criterion
lambda_tilde_i <- sum(N_birthi) / sum(N_starti) #Overall global growth rate
lambda_i_community_level <- (lambda_tilde_i - 1) / R_bari
lambda_tilde_j <- sum(N_birthj) / sum(N_startj) #Overall global growth rate
lambda_j_community_level <- (lambda_tilde_j - 1) / R_barj
print(paste("Metacommunity Growth Rate is", round(lambda_i_community_level-lambda_j_community_level,3)))
print(paste(" Invade?", lambda_i_community_level-lambda_j_community_level > 0))
# ----------------------------------------------------------------
# Components for coexistence, broxen down into the specific mechanisms
lambda_tilde_prime <- (1/mean(R_barj))-(1/R_bari) # Non-spatial fitness
storageEffect <- -covp( Ei-Ej,C ) # Storage effect
fd1 <- covp( R[invader,]*(1-C), v_i )/R_bari
fd2 <- covp( R[-invader,]*(1-C), v_j )/R_barj
fitnessDensityCov <- fd1- fd2 # Fitness density covariance
total <- lambda_tilde_prime + storageEffect + fitnessDensityCov
# Store results of coexistence mechanisms
nonspatial<-lambda_tilde_prime
storage <- storageEffect
fitness <- fitnessDensityCov
overallr <- total
coexistrandom<-as.data.frame(rbind(overallr, storage, fitness, nonspatial))
coexistrandom1 <- tibble::rownames_to_column(coexistrandom, "Mechanism")
coexistrandom2<-coexistrandom1 %>% mutate(Mechanism2 =Mechanism)%>% mutate(model ="Passive dispersal")
coexistrandom2$Mechanism2<-plyr::mapvalues(coexistrandom2$Mechanism, from=c("overallr", "storage", "fitness","nonspatial" ), to=c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
coexistrandom2$Mechanism2<- factor(coexistrandom2$Mechanism2, levels = c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
###
```
```{r deconstruct coexistence directed movement}
##Determining Chessonian coexistence strength and contributions from
# the storage effect, fitness density covariance, and nonspatial fitness
runs <- 1 # Number of runs (for stochastic models)
nonspatial <- storage <- fitness <- overall <- check <- rep(NA, runs)
invader <- 1 # Resident dynamics (all species that are not the invader) run to equilibrium.
results <- array(NA, c(species, patches, time))
results[,,1] <- 1000
results[invader,,1] <- 0 # Remove the invader species from the metacommunity
# Running the model to equilibrium without the invader
modelRundirected <- rdetdirected(R,d,a,patches,species,results,time)
# Then run invader to equilibrium spatial distribution, holding the global
# abundance to a very low density
iabun <- .005*(sum(modelRundirected [,,time]))
invade <- rep(iabun/patches,patches) ##divides equally among the patches##
# Set up array to store results with the invader at low density
co_results <- array(NA, c(species, patches, time))
co_results[,,1] <- modelRundirected[,,time]
co_results[invader,,1] <- invade # species i invades at time step 1
modelRunChessondirected <- rdetdirectedChesson(R,d,a,patches,species,co_results,invader,iabun,time) ##each patch at it's equilibrium densityy##
# Now do one more time step, keeeping track of changes in population before vs. after
N_start <- modelRunChessondirected[,,time] # starting population size
N_startj <- N_start[-invader,] # j is all resident species
N_starti <- N_start[invader,] # i is invader species
# Determine relative abundances
v <- matrix(NA, nrow=species, ncol=patches)
for (i in 1:species) {
v[i,] <- N_start[i,]/mean(N_start[i,])
}
v_i <- v[invader,]
v_j <- v[-invader,]
# Determine fitness
N_birth <- b(R, N_start, patches, species, a) # Population after 1 time step
N_birthi <- N_birth[invader,]
N_birthj <- N_birth[-invader,]
# Calculate average fitness across the metacommunity
R_bar <- rep(NA,species)
for (i in 1:species) {
R_bar[i] <- mean(R[i,])
}
R_bari <- R_bar[invader]
R_barj <- R_bar[-invader]
# Calculate environmental component
E <- R/R_bar
Ei <- E[invader,]
Ej <- E[-invader,]
# Calculate competitive term
C <- matrix(NA, nrow=species, ncol=patches)
for (p in 1: patches) {
C[,p] <-rep(1-bevHoltC(N_start[,p],a), species)
}
C <- C[invader,]
#Overall invasion criterion
lambda_tilde_i <- sum(N_birthi) / sum(N_starti) #Overall global growth rate
lambda_i_community_level <- (lambda_tilde_i - 1) / R_bari
lambda_tilde_j <- sum(N_birthj) / sum(N_startj) #Overall global growth rate
lambda_j_community_level <- (lambda_tilde_j - 1) / R_barj
print(paste("Metacommunity Growth Rate is", round(lambda_i_community_level-lambda_j_community_level,3)))
print(paste(" Invade?", lambda_i_community_level-lambda_j_community_level > 0))
# ----------------------------------------------------------------
# Components for coexistence, broxen down into the specific mechanisms
lambda_tilde_prime <- (1/mean(R_barj))-(1/R_bari) # Non-spatial fitness
storageEffect <- -covp( Ei-Ej,C ) # Storage effect
fd1 <- covp( R[invader,]*(1-C), v_i )/R_bari
fd2 <- covp( R[-invader,]*(1-C), v_j )/R_barj
fitnessDensityCov <- fd1- fd2 # Fitness density covariance
total <- lambda_tilde_prime + storageEffect + fitnessDensityCov
# Store results of coexistence mechanisms
nonspatial<-lambda_tilde_prime
storage <- storageEffect
fitness <- fitnessDensityCov
overallr <- total
coexistdirected<-as.data.frame(rbind(overallr, storage, fitness, nonspatial))
coexistdirected1 <- tibble::rownames_to_column(coexistdirected, "Mechanism")
coexistdirected2<-coexistdirected1 %>% mutate(Mechanism2 =Mechanism)%>% mutate(model ="Fitness-directed dispersal")
coexistdirected2$Mechanism2<-plyr::mapvalues(coexistdirected2$Mechanism, from=c("overallr", "storage", "fitness","nonspatial" ), to=c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
coexistdirected2$Mechanism2<- factor(coexistdirected2$Mechanism2, levels = c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
###
```
```{r deconstruct coexistence density-dependent (-) immigration}
##Determining Chessonian coexistence strength and contributions from
# the storage effect, fitness density covariance, and nonspatial fitness
runs <- 1 # Number of runs (for stochastic models)
nonspatial <- storage <- fitness <- overall <- check <- rep(NA, runs)
invader <- 1 # Resident dynamics (all species that are not the invader) run to equilibrium.
results <- array(NA, c(species, patches, time))
results[,,1] <- 1000
results[invader,,1] <- 0 # Remove the invader species from the metacommunity
# Running the model to equilibrium without the invader
modelRunddi <- rdetddi(R,d,a,patches,species,results,time)
# Then run invader to equilibrium spatial distribution, holding the global
# abundance to a very low density
iabun <- .005*(sum(modelRunddi[,,time]))
invade <- rep(iabun/patches,patches) ##divides equally among the patches##
# Set up array to store results with the invader at low density
co_results <- array(NA, c(species, patches, time))
co_results[,,1] <- modelRunddi[,,time]
co_results[invader,,1] <- invade # species i invades at time step 1
modelRunChessonddi <- rdetddiChesson(R,d,a,patches,species,co_results,invader,iabun,time) ##each patch at it's equilibrium densityy##
# Now do one more time step, keeeping track of changes in population before vs. after
N_start <- modelRunChessonddi[,,time] # starting population size
N_startj <- N_start[-invader,] # j is all resident species
N_starti <- N_start[invader,] # i is invader species
# Determine relative abundances
v <- matrix(NA, nrow=species, ncol=patches)
for (i in 1:species) {
v[i,] <- N_start[i,]/mean(N_start[i,])
}
v_i <- v[invader,]
v_j <- v[-invader,]
# Determine fitness
N_birth <- b(R, N_start, patches, species, a) # Population after 1 time step
N_birthi <- N_birth[invader,]
N_birthj <- N_birth[-invader,]
# Calculate average fitness across the metacommunity
R_bar <- rep(NA,species)
for (i in 1:species) {
R_bar[i] <- mean(R[i,])
}
R_bari <- R_bar[invader]
R_barj <- R_bar[-invader]
# Calculate environmental component
E <- R/R_bar
Ei <- E[invader,]
Ej <- E[-invader,]
# Calculate competitive term
C <- matrix(NA, nrow=species, ncol=patches)
for (p in 1: patches) {
C[,p] <-rep(1-bevHoltC(N_start[,p],a), species)
}
C <- C[invader,]
#Overall invasion criterion
lambda_tilde_i <- sum(N_birthi) / sum(N_starti) #Overall global growth rate
lambda_i_community_level <- (lambda_tilde_i - 1) / R_bari
lambda_tilde_j <- sum(N_birthj) / sum(N_startj) #Overall global growth rate
lambda_j_community_level <- (lambda_tilde_j - 1) / R_barj
print(paste("Metacommunity Growth Rate is", round(lambda_i_community_level-lambda_j_community_level,3)))
print(paste(" Invade?", lambda_i_community_level-lambda_j_community_level > 0))
# ----------------------------------------------------------------
# Components for coexistence, broxen down into the specific mechanisms
lambda_tilde_prime <- (1/mean(R_barj))-(1/R_bari) # Non-spatial fitness
storageEffect <- -covp( Ei-Ej,C ) # Storage effect
fd1 <- covp( R[invader,]*(1-C), v_i )/R_bari
fd2 <- covp( R[-invader,]*(1-C), v_j )/R_barj
fitnessDensityCov <- fd1- fd2 # Fitness density covariance
total <- lambda_tilde_prime + storageEffect + fitnessDensityCov
# Store results of coexistence mechanisms
nonspatial<-lambda_tilde_prime
storage <- storageEffect
fitness <- fitnessDensityCov
overallr <- total
coexistddi<-as.data.frame(rbind(overallr, storage, fitness, nonspatial))
coexistddi1 <- tibble::rownames_to_column(coexistddi, "Mechanism")
coexistddi2<-coexistddi1%>% mutate(Mechanism2 =Mechanism)%>% mutate(model ="Density-dependent (-) dispersal")
coexistddi2$Mechanism2<-plyr::mapvalues(coexistddi2$Mechanism, from=c("overallr", "storage", "fitness","nonspatial" ), to=c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
coexistddi2$Mechanism2<- factor(coexistddi2$Mechanism2, levels = c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
###
```
```{r deconstruct coexistence density-dependent (+) immigration}
##Determining Chessonian coexistence strength and contributions from
# the storage effect, fitness density covariance, and nonspatial fitness
runs <- 1 # Number of runs (for stochastic models)
nonspatial <- storage <- fitness <- overall <- check <- rep(NA, runs)
invader <- 1 # Resident dynamics (all species that are not the invader) run to equilibrium.
results <- array(NA, c(species, patches, time))
results[,,1] <- 1000
results[invader,,1] <- 0 # Remove the invader species from the metacommunity
# Running the model to equilibrium without the invader
modelRunddi_pos <- rdetddi_pos(R,d,a,patches,species,results,time)
# Then run invader to equilibrium spatial distribution, holding the global
# abundance to a very low density
iabun <- .005*(sum(modelRunddi_pos[,,time]))
invade <- rep(iabun/patches,patches) ##divides equally among the patches##
# Set up array to store results with the invader at low density
co_results <- array(NA, c(species, patches, time))
co_results[,,1] <- modelRunddi_pos[,,time]
co_results[invader,,1] <- invade # species i invades at time step 1
modelRunChessonddi_pos <- rdetddiChesson_pos(R,d,a,patches,species,co_results,invader,iabun,time) ##each patch at it's equilibrium densityy##
# Now do one more time step, keeeping track of changes in population before vs. after
N_start <- modelRunChessonddi_pos[,,time] # starting population size
N_startj <- N_start[-invader,] # j is all resident species
N_starti <- N_start[invader,] # i is invader species
# Determine relative abundances
v <- matrix(NA, nrow=species, ncol=patches)
for (i in 1:species) {
v[i,] <- N_start[i,]/mean(N_start[i,])
}
v_i <- v[invader,]
v_j <- v[-invader,]
# Determine fitness
N_birth <- b(R, N_start, patches, species, a) # Population after 1 time step
N_birthi <- N_birth[invader,]
N_birthj <- N_birth[-invader,]
# Calculate average fitness across the metacommunity
R_bar <- rep(NA,species)
for (i in 1:species) {
R_bar[i] <- mean(R[i,])
}
R_bari <- R_bar[invader]
R_barj <- R_bar[-invader]
# Calculate environmental component
E <- R/R_bar
Ei <- E[invader,]
Ej <- E[-invader,]
# Calculate competitive term
C <- matrix(NA, nrow=species, ncol=patches)
for (p in 1: patches) {
C[,p] <-rep(1-bevHoltC(N_start[,p],a), species)
}
C <- C[invader,]
#Overall invasion criterion
lambda_tilde_i <- sum(N_birthi) / sum(N_starti) #Overall global growth rate
lambda_i_community_level <- (lambda_tilde_i - 1) / R_bari
lambda_tilde_j <- sum(N_birthj) / sum(N_startj) #Overall global growth rate
lambda_j_community_level <- (lambda_tilde_j - 1) / R_barj
print(paste("Metacommunity Growth Rate is", round(lambda_i_community_level-lambda_j_community_level,3)))
print(paste(" Invade?", lambda_i_community_level-lambda_j_community_level > 0))
# ----------------------------------------------------------------
# Components for coexistence, broxen down into the specific mechanisms
lambda_tilde_prime <- (1/mean(R_barj))-(1/R_bari) # Non-spatial fitness
storageEffect <- -covp( Ei-Ej,C ) # Storage effect
fd1 <- covp( R[invader,]*(1-C), v_i )/R_bari
fd2 <- covp( R[-invader,]*(1-C), v_j )/R_barj
fitnessDensityCov <- fd1- fd2 # Fitness density covariance
total <- lambda_tilde_prime + storageEffect + fitnessDensityCov
# Store results of coexistence mechanisms
nonspatial<-lambda_tilde_prime
storage <- storageEffect
fitness <- fitnessDensityCov
overallr <- total
coexistddi_pos<-as.data.frame(rbind(overallr, storage, fitness, nonspatial))
coexistddi1_pos <- tibble::rownames_to_column(coexistddi_pos, "Mechanism")
coexistddi2_pos<-coexistddi1_pos%>% mutate(Mechanism2 =Mechanism)%>% mutate(model ="Density-dependent (+) dispersal")
coexistddi2_pos$Mechanism2<-plyr::mapvalues(coexistddi2_pos$Mechanism, from=c("overallr", "storage", "fitness","nonspatial" ), to=c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
coexistddi2_pos$Mechanism2<- factor(coexistddi2_pos$Mechanism2, levels = c("Total", "Storage effect", "Fitness-density cov", "Non-spatial"))
###
```
```{r plot results strength coexistence random}
coexistdispersal<-rbind(coexistrandom2, coexistdirected2, coexistddi2, coexistddi2_pos)
coexistdispersal2<-subset(coexistdispersal, model=="Passive dispersal" & Mechanism2!="Non-spatial")
LDGRpassive<-ggplot(data=coexistdispersal2, aes(x=Mechanism2, y=V1, fill=Mechanism2)) +
geom_bar(stat="identity")+xlab("")+ylab("LDGR")+mytheme +theme(axis.text.x = element_text(angle = 35, hjust = 1))+ scale_fill_manual(values = c("gray30", "#ff8c00", "#346299"))+ggtitle("Passive dispersal")
jpeg("C:/Users/Melissa/Dropbox/Coexistance stuff/Dispersal/LDGRpassive.jpeg", width = 4, height = 4, units = 'in', res = 600)
LDGRpassive
dev.off()
###
```
```{r plot results strength coexistence random vs. directed vs. density-dependent (+ and -)}
passive=coexistrandom2$V1
coexistdirected22<-cbind(coexistdirected2, passive)
coexistddi22<-cbind(coexistddi2, passive)
coexistddi_pos22<-cbind(coexistddi2_pos, passive)
directedall<-rbind(coexistdirected22, coexistddi22, coexistddi_pos22)
directedall2<-directedall %>% mutate(deltachange=(V1-passive)*100) %>% filter(Mechanism2 != "Non-spatial")
directedall2$model <- factor(directedall2$model, levels = c("Fitness-directed dispersal", "Density-dependent (-) dispersal", "Density-dependent (+) dispersal"))
mechanismsdisp<-ggplot(data=directedall2, aes(x=Mechanism2, y=deltachange, fill=Mechanism2)) +
geom_bar(stat="identity")+xlab("")+ylab(" % change in LDGR")+facet_grid(~model)+mytheme +theme(axis.text.x = element_text(angle = 35, hjust = 1))+ scale_fill_manual(values = c("gray30", "#ff8c00", "#346299"))+ theme(legend.position = "none")
jpeg("C:/Users/Melissa/Dropbox/Coexistance stuff/Dispersal/LDGRchangemechanisms.jpeg", width =8, height = 4, units = 'in', res = 600)
mechanismsdisp
dev.off()
###
```
#### plot time series####
```{r passive movement with varying dispersal}
# Initizalize model setup
patches <- 10 # Number of patches
species <- 2 # Number of species
time <- 100 # Length of time (generations) to run model
results <- array(NA, c(species, patches, time))
results[,,1] <- 100
a <- 1/2000 # Beverton-holt alpha for all species
# Density independent growth rate R###
meanR=1.6 ###mean growth rate patch ###
sdR= 0 ### std in growth rate### ##if 0, no variation, if >meanR/2, source sink dynamics####
SS=2 ###Source sink dynamics##, if 1 no SS, if >2, source sink dynamics#####
Rfunc <- function(species, patches, meanR,sdR, SS) {
R <- matrix(NA, nrow=species, ncol=patches)
R[1, 1:(patches/2)] <- rnorm(patches/2, mean = meanR, sd = sdR) ###source sp 1 ####
R[1, ((patches/2)+1):patches] <- rnorm(patches/2, mean = meanR/SS, sd = sdR)###sink sp 1####
R[2, 1:(patches/2)] <- rnorm(patches/2, mean = meanR/SS, sd = sdR) ### sink sp 2 ####
R[2, ((patches/2)+1):patches] <- rnorm(patches/2, mean = meanR, sd = sdR) ## source sp 2 ####
return(R)
}
R<-Rfunc(species,patches,meanR,sdR,SS)
## setting up matrix d vals #### different dispersal rates### (still same between sp 1 and 2)
##
d_vals2=c(0, 0.05, 0.1, 0.5, 0.9)
d_vals<-as.matrix(rbind(d_vals2, d_vals2))
###bookeeping###
dispnames <- vector("numeric", length=length(d_vals))
for(i in 1:length(d_vals)){
dispnames[i] <- paste("d=", d_vals[i])
}
speciesnames <- vector("numeric", length=species)
for(i in 1:species){
speciesnames[i] <- paste("species", i)
}
patchnames <- vector("numeric", length=patches)
for(i in 1:patches){
patchnames[i] <- paste("patch", i)
}
timenames <- vector("numeric", length=time)
for(i in 1:time){
timenames[i] <- paste("timestep", i)
}
###global dispersal###
dispscenariosrandom12 <- foreach(i=d_vals[1:2,], j=d_vals[,1:length(d_vals2)]) %do%
rdet(R,d=c(i,j),a,patches,species,results,time)
dispscenariosrandom122<-array(as.numeric(unlist(dispscenariosrandom12 )), dim=c(species,patches,time,length(d_vals)), dimnames=list(speciesnames, patchnames, timenames,dispnames)) ##array with labels####
#
```
```{r plot time series results random movement varying d , echo=FALSE}
###turn array into dataframe###
dispdatarandom <- as.data.frame.table(dispscenariosrandom122)
colnames(dispdatarandom) <- c("species", "patch", "timestep", "d", "N")
dispdatarandom$species<-as.numeric(dispdatarandom$species)
dispdatarandom$patch<-as.numeric(dispdatarandom$patch)
dispdatarandom$timestep<-as.numeric(dispdatarandom$timestep)
dispdatarandom$d <- with(dispdatarandom, gsub("d=","", d))
##assign R values to patches###
species1<-as.numeric(dispdatarandom$species)
patch1<-as.numeric(dispdatarandom$patch)
Rvals <- vector("numeric", length=length(species1))
for (i in 1:length(species1)){
s<-species1[i]
p<-patch1[i]
Rvals[i]<-c(R[s,p])
}
##merge back to dataframe###
dispdatarandom2<-cbind(dispdatarandom, Rvals)
##column for patch type###
dispdatarandom3<-dispdatarandom2 %>% mutate(patchtype=ifelse(species==1 & Rvals>mean(dispdatarandom2$Rvals),"Source sp 1 / Sink sp 2", ifelse(species==2 & Rvals < mean(dispdatarandom2$Rvals),"Source sp 1 / Sink sp 2", "Source sp 2 / Sink sp 1")))
###sum across patch types####
sumpatchrandom<-dispdatarandom3 %>%
group_by(species, patch, timestep, d, patchtype) %>%
summarise(N = sum(N))
sumpatchrandom$species=as.factor(sumpatchrandom$species)
sumpatchrandom$timestep<-as.numeric(sumpatchrandom$timestep)
##subset##
sumpatchrandom2<-subset(sumpatchrandom, d==" 0"|d==" 0.05"|d==" 0.5")
#plot##
mytheme<- theme_bw()+ theme(axis.line.x= element_line(colour = "black", size=0.3))+theme(axis.line.y= element_line(colour = "black", size=0.3))+theme(axis.text.x=element_text(size=10, colour = "black"))+theme(axis.text.y=element_text(size=10, colour = "black"))+theme(axis.title=element_text(size=12))+theme(plot.title=element_text(size=12) +theme(plot.title = element_text(hjust = 0.5)))+theme(plot.title = element_text(margin=margin(0,0,5,0)))
randommovement2spp<-ggplot(sumpatchrandom2 ,aes(x=timestep,y=N,colour=species, shape=patchtype), show.legend = F) +geom_line(aes(linetype=patchtype), size=1.5)+facet_grid(d~species, labeller = label_both)+xlab("Time")+ylim(0,2500)+ylab("Abundance")+ggtitle("Passive dispersal")+mytheme+ scale_colour_manual(values = c("black", "brown"))
#jpeg("C:/Users/Melissa/Dropbox/Coexistance stuff/Dispersal/passivedisp1.jpeg", width = 6, height = 5, units = 'in', res = 600)
#randommovement2spp
#dev.off()
###
```
```{r passive movement with varying R}
# Initizalize model setup
patches <- 10 # Number of patches
species <- 2 # Number of species
time <- 100 # Length of time (generations) to run model
results <- array(NA, c(species, patches, time))
results[,,1] <- 100
a <- 1/2000 # Beverton-holt alpha for all species
# Density independent growth rate R###
species=2
patches=10
meanR=1.5 ###mean growth rate source patch ###
sdR= 0.001 ### std in growth rate### ##if 0, no variation, if >meanR/2, source sink dynamics####
p= 1 ### When p = 1, no variation across patches###
Rfunc <- function(species, patches, meanR,sdR, p) {
R <- matrix(NA, nrow=species, ncol=patches)
R[1, 1:(patches/2)] <- rnorm(patches/2, mean = ifelse(p<1, meanR+(1-p), meanR), sd = sdR) ###source sp 1 ####
R[1, ((patches/2)+1):patches] <- rnorm(patches/2, mean = meanR*p, sd = sdR)###sink sp 1####
R[2, 1:(patches/2)] <- rnorm(patches/2, mean = meanR*p, sd = sdR) ### sink sp 2 ####
R[2, ((patches/2)+1):patches] <- rnorm(patches/2, mean = ifelse(p<1, meanR+(1-p), meanR), sd = sdR) ## source sp 2 ####
return(R)
}
R<-Rfunc(species,patches,meanR,sdR,p)
p_vals=seq(from=1, to=0.33, by=-0.05) ###### mid way point = source sink dynamics###
R_vals=foreach(i=p_vals) %do% Rfunc(species,patches,meanR, sdR, p=c(i))
## setting up
###bookeeping###
Rnames <- vector("numeric", length=length(p_vals))
for(i in 1:length(p_vals)){
Rnames[i] <- paste("R=", p_vals[i])
}
speciesnames <- vector("numeric", length=species)
for(i in 1:species){
speciesnames[i] <- paste("species", i)
}
patchnames <- vector("numeric", length=patches)
for(i in 1:patches){
patchnames[i] <- paste("patch", i)
}
timenames <- vector("numeric", length=time)
for(i in 1:time){
timenames[i] <- paste("timestep", i)
}
###global dispersal###
###global dispersal###
d=c(0.05, 0.05)
disprandom_varR <- foreach(i=R_vals[]) %do%
rdet(R=i,d,a,patches,species,results,time)
disprandom_varR_2<-array(as.numeric(unlist(disprandom_varR )), dim=c(species,patches,time,length(R_vals)), dimnames=list(speciesnames, patchnames, timenames,Rnames)) ##array with labels####
#
```
```{r plot time series results random movement varying R , echo=FALSE}
###turn array into dataframe###
disprandomvarRdf<- as.data.frame.table(disprandom_varR_2)
colnames(disprandomvarRdf) <- c("species", "patch", "timestep", "R", "N")
disprandomvarRdf$species<-as.numeric(disprandomvarRdf$species)
disprandomvarRdf$patch<-as.numeric(disprandomvarRdf$patch)
disprandomvarRdf$timestep<-as.numeric(disprandomvarRdf$timestep)
disprandomvarRdf$R <- with(disprandomvarRdf, gsub("R=","", R))
disprandomvarRdf$R<-as.numeric(disprandomvarRdf$R)
##column for patch type###
####when R >
disprandomvarRdf_2<-disprandomvarRdf %>% mutate(patchtype=ifelse(R > 0.67, "Source both sp", ifelse(patch <6, "Source sp 1 / Sink sp 2", "Source sp 2 / Sink sp 1")))
###sum across patch types####
sumpatchrandom_varR<-disprandomvarRdf_2 %>%
group_by(species, timestep, R, patchtype) %>%
summarise(N = sum(N))
sumpatchrandom_varR$species=as.factor(sumpatchrandom_varR$species)
sumpatchrandom_varR$timestep<-as.numeric(sumpatchrandom_varR$timestep)
##subset##
##subset##
sumpatchrandom_varR2<-subset(sumpatchrandom_varR, R==1.00|R==0.8|R==0.35)
sumpatchrandom_varR2$R=factor(sumpatchrandom_varR2$R, levels=c("1", "0.8", "0.35"))
#plot##
mytheme<- theme_bw()+ theme(axis.line.x= element_line(colour = "black", size=0.3))+theme(axis.line.y= element_line(colour = "black", size=0.3))+theme(axis.text.x=element_text(size=10, colour = "black"))+theme(axis.text.y=element_text(size=10, colour = "black"))+theme(axis.title=element_text(size=12))+theme(plot.title=element_text(size=12) +theme(plot.title = element_text(hjust = 0.5)))+theme(plot.title = element_text(margin=margin(0,0,5,0)))
randommovement2spp_varR<-ggplot(sumpatchrandom_varR2 ,aes(x=timestep,y=N,colour=species, shape=patchtype), show.legend = F) +geom_line(aes(linetype=patchtype), size=1.5)+facet_grid(R~species, labeller = label_both)+xlab("Time")+ylab("Abundance")+ggtitle("Passive dispersal")+mytheme+ scale_colour_manual(values = c("black", "brown"))+ scale_linetype_manual(values=c("dotted", "solid", "dashed"))
jpeg("C:/Users/Melissa/Dropbox/Coexistance stuff/Dispersal/passivedisp_varR.jpeg", width = 6, height = 5, units = 'in', res = 600)
randommovement2spp_varR
dev.off()
###
```
```{r plot results random movement , echo=FALSE}
###turn array into dataframe###
dispdatarandom <- as.data.frame.table(dispscenariosrandom122)
colnames(dispdatarandom) <- c("species", "patch", "timestep", "d", "N")
dispdatarandom$species<-as.numeric(dispdatarandom$species)
dispdatarandom$patch<-as.numeric(dispdatarandom$patch)
dispdatarandom$timestep<-as.numeric(dispdatarandom$timestep)
dispdatarandom$d <- with(dispdatarandom, gsub("d=","", d))
##assign R values to patches###
species1<-as.numeric(dispdatarandom$species)
patch1<-as.numeric(dispdatarandom$patch)
Rvals <- vector("numeric", length=length(species1))
for (i in 1:length(species1)){
s<-species1[i]
p<-patch1[i]
Rvals[i]<-c(R[s,p])
}
##merge back to dataframe###
dispdatarandom2<-cbind(dispdatarandom, Rvals)
##column for patch type###
dispdatarandom3<-dispdatarandom2 %>% mutate(patchtype=ifelse(species==1 & Rvals>mean(dispdatarandom2$Rvals),"Source sp 1 / Sink sp 2", ifelse(species==2 & Rvals < mean(dispdatarandom2$Rvals),"Source sp 1 / Sink sp 2", "Source sp 2 / Sink sp 1")))
###sum across patch types####
sumpatchrandom<-dispdatarandom3 %>%
group_by(species, patch, timestep, d, patchtype) %>%
summarise(N = sum(N))
sumpatchrandom$species=as.factor(sumpatchrandom$species)
sumpatchrandom$timestep<-as.numeric(sumpatchrandom$timestep)
##subset##
sumpatchrandom2<-subset(sumpatchrandom, d==" 0"|d==" 0.05"|d==" 0.5")
#plot##
mytheme<- theme_bw()+ theme(axis.line.x= element_line(colour = "black", size=0.3))+theme(axis.line.y= element_line(colour = "black", size=0.3))+theme(axis.text.x=element_text(size=10, colour = "black"))+theme(axis.text.y=element_text(size=10, colour = "black"))+theme(axis.title=element_text(size=12))+theme(plot.title=element_text(size=12) +theme(plot.title = element_text(hjust = 0.5)))+theme(plot.title = element_text(margin=margin(0,0,5,0)))
randommovement2spp<-ggplot(sumpatchrandom2 ,aes(x=timestep,y=N,colour=species, shape=patchtype), show.legend = F) +geom_line(aes(linetype=patchtype), size=1.5)+facet_grid(d~species, labeller = label_both)+xlab("Time")+ylim(0,2500)+ylab("Abundance")+ggtitle("Passive dispersal")+mytheme+ scale_colour_manual(values = c("black", "brown"))
jpeg("C:/Users/Melissa/Dropbox/Coexistance stuff/Dispersal/passivedisp1.jpeg", width = 6, height = 5, units = 'in', res = 600)
randommovement2spp
dev.off()
###
```
```{r fitness movement with varying dispersal}
# Initizalize model setup
patches <- 10 # Number of patches
species <- 2 # Number of species
time <- 100 # Length of time (generations) to run model
results <- array(NA, c(species, patches, time))
results[,,1] <- 100
a <- 1/2000 # Beverton-holt alpha for all species
# Density independent growth rate R###