-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathABMmodels_model19_evolution_of_SLstrategies.Rmd
1398 lines (930 loc) · 65.7 KB
/
ABMmodels_model19_evolution_of_SLstrategies.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
---
title: "Simulation Models of Cultural Evolution in R"
author: "Alex Mesoudi"
output: pdf_document
---
# Model 19: The evolution of social learning strategies
## Introduction
Model 18b showed that social learning can readily evolve when combined with individual learning. So-called 'critical learners' who first try to socially learn behaviour from another agent, and if that behaviour is incorrect then try individual learning, out-compete both pure social learners who only copy others and pure individual learners who never copy (Enquist et al. 2007). But in Model 18, social learning took the form of unbiased transmission. Social learners picked a random agent from the previous generation and copied their behaviour. This is, of course, quite unrealistic.
In Model 19 we will therefore look at the evolution of the more sophisticated social learning strategies (Laland 2004) that we have covered previously: *directly biased transmission* from Model 3, where agents preferentially copy traits based on their characteristics, in this case their payoffs (hence 'payoff bias' for short), and *conformist biased transmission* from Model 5, where agents preferentially copy the most common trait amongst a set of demonstrators irrespective of trait characteristics. While those previous models examined the cultural dynamics that each of these transmission biases or strategies generate, here we are interested in when and why a capacity or tendency to employ such strategies evolve in the first place.
Previous modelling work (e.g. Nakahashi et al. 2012) has suggested that the answer to this question partly depends on whether environments vary *temporally*, as in Model 18, where the optimal behaviour occasionally changes over time, or *spatially*, as in the migration model of Model 7, where different groups inhabit different environments in which different behaviours are optimal. Hence Model 19a assumes temporally varying environments and Model 19b assumes spatially varying environments. The driving question in both is: when and why do payoff and conformist biased social learning strategies out-compete unbiased transmission?
## Model 19a: Temporally varying environments
Here we add social learning strategies to the critical learning strategy of Model 18b, assuming that critical learning has already evolved. As in Model 18b, there is one behaviour that is 'correct' and gives higher fitness of $1 + b$ compared to an 'incorrect' behaviour that just gives the baseline fitness of 1. We keep pure individual learners (IL) for reference and to populate the first generation. As before, individual learners discover the correct behaviour in each generation with probability $p_i$ at cost $c_i$. We remove pure social learners because we know from Model 18 that critical learning is always superior.
Now critical learners employ one of three strategies: unbiased transmission (UB), conformist bias (CB) or payoff bias (PB).
Critical learners employing unbiased learning (UB) copy a member of the previous generation at random, as per Model 18b. There is no cost to UB.
Critical learners employing conformist bias (CB) are disproportionately more likely to copy the majority trait amongst $n$ randomly chosen demonstrators as in Model 5. However unlike Model 5, we use the formulation of conformity introduced in Model 17c (and also used by Nakahashi et al. 2012). This formulation is more flexible than the one in Model 5 as it can handle more than two traits, which will be needed in Model 19b. As a reminder, Equation 17.2 specified the probability of adopting trait $i$, $p_i$, as
$$ p_i = \frac{n_i^f}{\sum_{j=1} ^k {n_j^f}}$$
where $n_i$ is the number of the $n$ demonstrators who have trait $i$, the exponent $f$ controls the strength of conformity, and the denominator gives the sum of the number of agents who have each of $k$ traits, each raised to the power $f$. When $f = 1$ transmission is unbiased, as the equation gives the exact proportion of agents with trait $i$. As $f$ increases, CB agents are increasingly likely to pick the most-common trait. Whether the behaviour is correct or incorrect does not matter for conformity. It works purely on behaviour frequency. CB agents bear a fixed cost $c_c$ representing the time and energy required to identify the majority behaviour amongst $n$ demonstrators.
Critical learners employing payoff bias (PB) copy the correct behaviour if it is exhibited by any of $n$ randomly chosen demonstrators, otherwise they copy the incorrect behaviour. This version of directly biased transmission resembles horizontal transmission from Model 6c (with $s_h = 1$, i.e. the correct behaviour is always copied successfully; this reduces the number of parameters in this more-complex model). As noted in Model 6, there are many ways of implementing direct bias, but this one nicely mirrors conformist bias in that it involves picking from $n$ demonstrators. Note that this payoff-based direct bias is different to the payoff-based indirect bias from Model 4a: the former involves copying traits based on payoffs, while the latter involves copying individuals based on payoffs. Here, PB agents are copying traits/behaviours. PB agents bear a fixed cost $c_p$ representing the time and energy required to identify the correct behaviour amongst $n$ demonstrators.
Now for the model. First we create our $N$ agents, the same as in Model 18b, all initially individual learners (IL):
```{r}
N <- 1000
# create agents, initially all individual learners
agent <- data.frame(learning = rep("IL", N),
behaviour = rep(NA, N),
fitness = rep(NA, N))
head(agent)
```
Next we create the *output* dataframe, this time holding the frequency and mean fitness of all four types of agents:
```{r}
t_max <- 500
# keep track of freq and fitnesses of each type
output <- data.frame(ILfreq = rep(NA, t_max),
UBfreq = rep(NA, t_max),
PBfreq = rep(NA, t_max),
CBfreq = rep(NA, t_max),
ILfitness = rep(NA, t_max),
UBfitness = rep(NA, t_max),
PBfitness = rep(NA, t_max),
CBfitness = rep(NA, t_max))
head(output)
```
Before getting to the main code, let's simulate the first generation of individual learning to provide the critical learners with demonstrators in generation two onwards. The following code, all familiar from Model 18, runs individual learning and calculates fitnesses:
```{r}
t <- 1
p_i <- 0.5
b <- 1
c_i <- 0.2
#individual learning for ILs
agent$behaviour[agent$learning == "IL"] <- sample(c(1,0),
sum(agent$learning == "IL"),
replace = TRUE,
prob = c(p_i, 1 - p_i))
# get fitnesses
agent$fitness <- ifelse(agent$behaviour, 1 + b, 1)
agent$fitness[agent$learning == "IL"] <- agent$fitness[agent$learning == "IL"] - c_i
head(agent)
```
And now we record frequencies and mean fitnesses of each learning type, now including the (currently absent) UB, PB and CB agents:
```{r}
# record frequencies and fitnesses
output$ILfreq[t] <- sum(agent$learning == "IL") / N
output$UBfreq[t] <- sum(agent$learning == "UB") / N
output$PBfreq[t] <- sum(agent$learning == "PB") / N
output$CBfreq[t] <- sum(agent$learning == "CB") / N
output$ILfitness[t] <- mean(agent$fitness[agent$learning == "IL"])
output$UBfitness[t] <- mean(agent$fitness[agent$learning == "UB"])
output$PBfitness[t] <- mean(agent$fitness[agent$learning == "PB"])
output$CBfitness[t] <- mean(agent$fitness[agent$learning == "CB"])
head(output)
```
And finally mutation, with an unrealistically high mutation rate for demonstration purposes:
```{r}
mu <- 0.5
# mutation
mutate <- runif(N) < mu
IL_mutate <- agent$learning == "IL" & mutate
UB_mutate <- agent$learning == "UB" & mutate
PB_mutate <- agent$learning == "PB" & mutate
CB_mutate <- agent$learning == "CB" & mutate
agent$learning[IL_mutate] <- sample(c("UB","PB","CB"), sum(IL_mutate), replace = TRUE)
agent$learning[UB_mutate] <- sample(c("IL","PB","CB"), sum(UB_mutate), replace = TRUE)
agent$learning[PB_mutate] <- sample(c("IL","UB","CB"), sum(PB_mutate), replace = TRUE)
agent$learning[CB_mutate] <- sample(c("IL","UB","PB"), sum(CB_mutate), replace = TRUE)
head(agent[,1:2], 10)
```
Now we can start the full sequence of events that occur in each generation at $t = 2$. First we put *agent* into *previous_agent* so that critical learners can copy the previous unaltered generation. Then individual learners engage in individual learning:
```{r}
t <- 2
# 1. store previous timestep agent
previous_agent <- agent
# 2. individual learning for ILs
agent$behaviour[agent$learning == "IL"] <- sample(c(1,0),
sum(agent$learning == "IL"),
replace = TRUE,
prob = c(p_i, 1 - p_i))
```
Next is social learning. As in Model 18, with probability $v$ the environment changes meaning that all critical learners must copy the incorrect behaviour. Otherwise, agents engage in social learning, as explored next.
```{r}
v <- 0.9
# 3. social learning for critical learners
# if environment has changed, all social learners have incorrect beh
if (v < runif(1)) {
agent$behaviour[agent$learning != "IL"] <- 0 # incorrect
} else {
# social learning code
}
```
Now to fill in the social learning code. First we pick $n$ random demonstrators from the previous generation for each agent and store them in a matrix called *dems*:
```{r}
n <- 3
# create matrix for holding n demonstrators for N agents
# fill with randomly selected agents from previous gen
dems <- matrix(data = sample(previous_agent$behaviour, N*n, replace = TRUE),
nrow = N, ncol = n)
head(dems)
```
Each row of *dems* contains the behaviours (1 indicating correct and 0 indicating incorrect) of $n$ randomly chosen demonstrators for one agent.
UB agents then copy the behaviour of the first of these demonstrators (in column 1 of *dems*, i.e. *dems[,1]*), ignoring the rest. For illustration, we calculate the proportion correct behaviours before and after UB.
```{r}
# proportion correct before social learning
mean(agent$behaviour[agent$learning == "UB"])
# for UBs, copy the behaviour of the 1st dem in dems
agent$behaviour[agent$learning == "UB" & dems[,1]] <- 1 # correct
agent$behaviour[agent$learning == "UB" & !dems[,1]] <- 0 # incorrect
# proportion correct after social learning
mean(agent$behaviour[agent$learning == "UB"])
```
There is probably not much change here, given that unbiased transmission is random.
Next, PB agents copy the correct behaviour if at least one demonstrator is correct, as calculated using **rowSums()** > 0. Otherwise they adopt the incorrect behaviour.
```{r}
# proportion correct before social learning
mean(agent$behaviour[agent$learning == "PB"])
# for PB, copy correct if at least one of dems is correct
agent$behaviour[agent$learning == "PB" & rowSums(dems) > 0] <- 1 # correct
agent$behaviour[agent$learning == "PB" & rowSums(dems) == 0] <- 0 # incorrect
# proportion correct after social learning
mean(agent$behaviour[agent$learning == "PB"])
```
There are many more correct PB agents after payoff bias than before, given that payoff bias favours correct behaviour.
Finally, CB agents get the probability of copying the correct behaviour from the $n$ demonstrators according to Equation 17.2 above, then copy the correct behaviour with this probability, otherwise adopting the incorrect behaviour. Note that because there are only two behaviours (correct and incorrect), the denominator of Equation 17.2 is the sum of the number of correct demonstrators *CB_rowSums* raised to the power $f$, plus the sum of the number incorrect, which will be $n$ - *CB_rowSums*, also raised to the power $f$. And because there are only two behaviours, we only need to calculate the probability of one of them.
```{r}
f <- 3
# proportion correct before social learning
mean(agent$behaviour[agent$learning == "CB"])
# for CB, copy majority behaviour according to parameter f
copy_probs <- rowSums(dems)^f / (rowSums(dems)^f + (n - rowSums(dems))^f)
probs <- runif(N)
agent$behaviour[agent$learning == "CB" & probs < copy_probs] <- 1 # correct
agent$behaviour[agent$learning == "CB" & probs >= copy_probs] <- 0 # incorrect
# proportion correct after social learning
mean(agent$behaviour[agent$learning == "CB"])
```
Because conformity is blind to payoffs, and in this second generation there won't be a majority of correct behaviours, the frequency of correct behaviour amongst CB agents is unlikely to have increased much, if at all.
Now that social learning is complete, all incorrect social learners engage in individual learning as per the critical learner strategy (see Model 18b):
```{r}
# 4. incorrect social learners engage in individual learning
incorrect_SL <- agent$learning != "IL" & agent$behaviour == 0
agent$behaviour[incorrect_SL] <- sample(c(1,0),
sum(incorrect_SL),
replace = TRUE,
prob = c(p_i, 1-p_i))
```
Next we calculate fitnesses, this time including the fitness costs to PB and CB, as well as the cost of individual learning for incorrect social learners:
```{r}
c_p <- 0.02
c_c <- 0.02
# 5. calculate fitnesses
# all correct agents get bonus b
agent$fitness <- ifelse(agent$behaviour, 1 + b, 1)
# costs of learning
agent$fitness[agent$learning == "IL"] <- agent$fitness[agent$learning == "IL"] - c_i
agent$fitness[agent$learning == "PB"] <- agent$fitness[agent$learning == "PB"] - c_p
agent$fitness[agent$learning == "CB"] <- agent$fitness[agent$learning == "CB"] - c_c
agent$fitness[incorrect_SL] <- agent$fitness[incorrect_SL] - c_i
head(agent, 10)
```
There should be a range of fitnesses here, depending on the agent learning type, whether they are correct or incorrect, and whether critical learners engaged in individual learning.
Step 6 involves recording frequencies and mean fitnesses of each agent type, as before:
```{r}
# 6. record frequencies and fitnesses of each type
output$ILfreq[t] <- sum(agent$learning == "IL") / N
output$UBfreq[t] <- sum(agent$learning == "UB") / N
output$PBfreq[t] <- sum(agent$learning == "PB") / N
output$CBfreq[t] <- sum(agent$learning == "CB") / N
output$ILfitness[t] <- mean(agent$fitness[agent$learning == "IL"])
output$UBfitness[t] <- mean(agent$fitness[agent$learning == "UB"])
output$PBfitness[t] <- mean(agent$fitness[agent$learning == "PB"])
output$CBfitness[t] <- mean(agent$fitness[agent$learning == "CB"])
head(output)
```
The *output* dataframe shows that IL agents drop sharply in frequency, replaced with roughly equal proportions of the other agent types. Consistent with this, all critical learners have higher fitness than IL agents. And as hinted at above, PB agents have slightly higher mean fitness than UB and CB agents.
Step 7 is selection and reproduction. Agents reproduce their learning type with probability equal to their relative fitness. We can use **table()** to show the proportions of each type before and after selection.
```{r}
# before selection
table(agent$learning)/N
# 7. selection and reproduction
relative_fitness <- agent$fitness / sum(agent$fitness)
agent$learning <- sample(agent$learning,
N,
prob = relative_fitness,
replace = TRUE)
# after selection
table(agent$learning)/N
```
Reflecting their fitness differences, IL agents probably dropped in frequency while PB agents increased.
The final within-generation step is mutation, which we have already seen:
```{r}
# 8. mutation
mutate <- runif(N) < mu
IL_mutate <- agent$learning == "IL" & mutate
UB_mutate <- agent$learning == "UB" & mutate
PB_mutate <- agent$learning == "PB" & mutate
CB_mutate <- agent$learning == "CB" & mutate
agent$learning[IL_mutate] <- sample(c("UB","PB","CB"), sum(IL_mutate), replace = TRUE)
agent$learning[UB_mutate] <- sample(c("IL","PB","CB"), sum(UB_mutate), replace = TRUE)
agent$learning[PB_mutate] <- sample(c("IL","UB","CB"), sum(PB_mutate), replace = TRUE)
agent$learning[CB_mutate] <- sample(c("IL","UB","PB"), sum(CB_mutate), replace = TRUE)
```
The following function **SLStemporal** combines all these chunks, with a larger $N$ and lower $\mu$ as defaults.
```{r}
SLStemporal <- function(N=5000, t_max=500, p_i=0.5, b=1, c_i=0.2, mu=0.001, v=0.9,
n=3, f=3, c_c=0.02, c_p=0.02) {
# create agents, initially all individual learners
agent <- data.frame(learning = rep("IL", N),
behaviour = rep(NA, N),
fitness = rep(NA, N))
# keep track of freq and fitnesses of each type
output <- data.frame(ILfreq = rep(NA, t_max),
UBfreq = rep(NA, t_max),
PBfreq = rep(NA, t_max),
CBfreq = rep(NA, t_max),
ILfitness = rep(NA, t_max),
UBfitness = rep(NA, t_max),
PBfitness = rep(NA, t_max),
CBfitness = rep(NA, t_max))
# start timestep loop
for (t in 1:t_max) {
# 1. store previous timestep agent
previous_agent <- agent
# 2. individual learning for ILs
agent$behaviour[agent$learning == "IL"] <- sample(c(1,0),
sum(agent$learning == "IL"),
replace = TRUE,
prob = c(p_i, 1 - p_i))
# 3. social learning for critical learners
# if environment has changed, all social learners have incorrect beh
if (v < runif(1)) {
agent$behaviour[agent$learning != "IL"] <- 0 # incorrect
} else {
# otherwise create matrix for holding n demonstrators for N agents
# fill with randomly selected agents from previous gen
dems <- matrix(data = sample(previous_agent$behaviour, N*n, replace = TRUE),
nrow = N, ncol = n)
# for UBs, copy the behaviour of the 1st dem in dems
agent$behaviour[agent$learning == "UB" & dems[,1]] <- 1 # correct
agent$behaviour[agent$learning == "UB" & !dems[,1]] <- 0 # incorrect
# for PB, copy correct if at least one of dems is correct
agent$behaviour[agent$learning == "PB" & rowSums(dems) > 0] <- 1 # correct
agent$behaviour[agent$learning == "PB" & rowSums(dems) == 0] <- 0 # incorrect
# for CB, copy majority behaviour according to parameter f
copy_probs <- rowSums(dems)^f / (rowSums(dems)^f + (n - rowSums(dems))^f)
probs <- runif(N)
agent$behaviour[agent$learning == "CB" & probs < copy_probs] <- 1 # correct
agent$behaviour[agent$learning == "CB" & probs >= copy_probs] <- 0 # incorrect
}
# 4. incorrect social learners engage in individual learning
incorrect_SL <- agent$learning != "IL" & agent$behaviour == 0
agent$behaviour[incorrect_SL] <- sample(c(1,0),
sum(incorrect_SL),
replace = TRUE,
prob = c(p_i, 1-p_i))
# 5. calculate fitnesses
# all correct agents get bonus b
agent$fitness <- ifelse(agent$behaviour, 1 + b, 1)
# costs of learning
agent$fitness[agent$learning == "IL"] <- agent$fitness[agent$learning == "IL"] - c_i
agent$fitness[agent$learning == "PB"] <- agent$fitness[agent$learning == "PB"] - c_p
agent$fitness[agent$learning == "CB"] <- agent$fitness[agent$learning == "CB"] - c_c
agent$fitness[incorrect_SL] <- agent$fitness[incorrect_SL] - c_i
# 6. record frequencies and fitnesses of each type
output$ILfreq[t] <- sum(agent$learning == "IL") / N
output$UBfreq[t] <- sum(agent$learning == "UB") / N
output$PBfreq[t] <- sum(agent$learning == "PB") / N
output$CBfreq[t] <- sum(agent$learning == "CB") / N
output$ILfitness[t] <- mean(agent$fitness[agent$learning == "IL"])
output$UBfitness[t] <- mean(agent$fitness[agent$learning == "UB"])
output$PBfitness[t] <- mean(agent$fitness[agent$learning == "PB"])
output$CBfitness[t] <- mean(agent$fitness[agent$learning == "CB"])
# 7. selection and reproduction
relative_fitness <- agent$fitness / sum(agent$fitness)
agent$learning <- sample(agent$learning,
N,
prob = relative_fitness,
replace = TRUE)
# 8. mutation
mutate <- runif(N) < mu
IL_mutate <- agent$learning == "IL" & mutate
UB_mutate <- agent$learning == "UB" & mutate
PB_mutate <- agent$learning == "PB" & mutate
CB_mutate <- agent$learning == "CB" & mutate
agent$learning[IL_mutate] <- sample(c("UB","PB","CB"), sum(IL_mutate), replace = TRUE)
agent$learning[UB_mutate] <- sample(c("IL","PB","CB"), sum(UB_mutate), replace = TRUE)
agent$learning[PB_mutate] <- sample(c("IL","UB","CB"), sum(PB_mutate), replace = TRUE)
agent$learning[CB_mutate] <- sample(c("IL","UB","PB"), sum(CB_mutate), replace = TRUE)
}
# add predicted IL fitness to output and export
output$predictedILfitness <- 1 + b*p_i - c_i
output
}
```
The following two plotting functions plot the frequencies and fitnesses of the four strategies:
```{r}
SLSfreqplot <- function(output) {
plot(output$ILfreq,
type = 'l',
lwd = 2,
col = "royalblue",
ylim = c(0,1),
ylab = "proportion of learners",
xlab = "generation")
lines(output$UBfreq,
type = 'l',
lwd = 2,
col = "orange")
lines(output$PBfreq,
type = 'l',
lwd = 2,
col = "springgreen4")
lines(output$CBfreq,
type = 'l',
lwd = 2,
col = "orchid")
legend(x = nrow(output)/5, y = 0.6,
legend = c("individual learning (IL)",
"unbiased transmission (UB)",
"payoff bias (PB)",
"conformist bias (CB)"),
col = c("royalblue", "orange", "springgreen4","orchid"),
lty = 1,
lwd = 2,
bty = "n",
cex = 0.8)
}
SLSfitnessplot <- function(output) {
alpha = 150
plot(output$ILfitness,
type = 'l',
lwd = 2,
col = rgb(65,105,225,max=255,alpha), # royalblue
ylim = c(0.5,2),
ylab = "mean fitness",
xlab = "generation")
lines(output$UBfitness,
type = 'l',
lwd = 2,
col = rgb(255,165,0,max=255,alpha)) # orange
lines(output$PBfitness,
type = 'l',
lwd = 2,
col = rgb(0,139,69,max=255,alpha)) # springgreen4
lines(output$CBfitness,
type = 'l',
lwd = 2,
col = rgb(218,112,214,max=255,alpha)) # orchid
legend(x = nrow(output)/5, y = 0.9,
legend = c("individual learning (IL)",
"unbiased transmission (UB)",
"payoff bias (PB)",
"conformist bias (CB)"),
col = c("royalblue", "orange", "springgreen4","orchid"),
lty = 1,
lwd = 2,
bty = "n",
cex = 0.8)
abline(h = output$predictedILfitness[1],
lty = 2)
}
```
We can now run the model with default parameter values:
```{r}
data_model19a <- SLStemporal()
SLSfreqplot(data_model19a)
```
Payoff bias clearly outperforms the other strategies. Individual learning is virtually absent, as expected given that the critical learners all incorporate individual learning. Unbiased and conformist learners each have negligible frequencies.
We can plot the fitnesses to explore this further:
```{r}
SLSfitnessplot(data_model19a)
```
Individual learners fluctuate around their expected mean fitness indicated with the horizontal dotted line. They fluctuate quite wildly given that there are so few of them. The critical learners peak much higher, with drops when the environment changes. Let's zoom in to the first few generations for a closer look:
```{r}
SLSfitnessplot(data_model19a[1:100, ])
```
Here the advantage of payoff bias becomes apparent. While UB agents peak slightly higher due to their lower costs of learning, PB agents acquire the correct behaviour quicker after an environmental change than UB and CB agents.
This makes sense. Immediately after an environmental change, all critical learners will be incorrect, and therefore fall back on individual learning. Some will discover the new correct behaviour, others won't, as determined by $p_i$. The following generation, critical learners can potentially copy the correct behaviour. PB agents have more chance of copying the correct behaviour amongst their $n$ demonstrators than UB agents who only copy a single demonstrator, assuming $n > 1$. CB agents copy the majority of $n$ demonstrators, but immediately after an environmental change the majority is likely to still be the old, now-incorrect behaviour.
To get an idea of which strategy wins over a range of parameter values and avoid the temptation to cherry-pick particular values, or accidentally miss interesting parts of the parameter space, we can run the simulation over multiple values of two parameters and plot the results in a heat map. The function **SLStemporalheatmap** takes as its first argument a list of 2 parameters and their values, e.g. `list(n = 1:5, v = seq(0.5,1,0.1))`, and runs **SLStemporal** for each parameter value combination. The more values you enter, the higher the resolution of the heatmap (and the longer it takes to run). The winning strategy for each run is the one that exceeds the frequency *cutoff* after *t_max* generations. By default *cutoff* is 0.5. The winning strategy is stored in a matrix called *strategy* which is then used to create a heatmap with cells coloured according to the winning strategy. If no strategy exceeds *cutoff* then the cell is coloured grey. To speed things up when running multiple simulations, we reduce $N$ without much loss of inference.
```{r}
SLStemporalheatmap <- function(parameters, cutoff = 0.5,
N = 1000, t_max = 1000, p_i=0.5, b=1, c_i=0.2, mu=0.001,
v=0.9, n=3, f=3, c_c=0.02, c_p=0.02) {
# for brevity
p <- parameters
# list of default arguments
arguments <- data.frame(N, t_max, p_i, b, c_i, mu, v,
n, f, c_c, c_p)
# for modifying later on
p1_pos <- which(names(arguments) == names(p[1])) # position of p1 in arguments
p2_pos <- which(names(arguments) == names(p[2])) # position of p2 in arguments
# matrix to hold winning strategies
# 0=none,1=IL,2=UB,3=PB,4=CB
strategy <- matrix(nrow = length(p[[1]]),
ncol = length(p[[2]]))
for (p1 in p[[1]]) {
for (p2 in p[[2]]) {
# set this run's parameter values
arguments[p1_pos] <- p1
arguments[p2_pos] <- p2
# run the model
output <- SLStemporal(as.numeric(arguments[1]),
as.numeric(arguments[2]),
as.numeric(arguments[3]),
as.numeric(arguments[4]),
as.numeric(arguments[5]),
as.numeric(arguments[6]),
as.numeric(arguments[7]),
as.numeric(arguments[8]),
as.numeric(arguments[9]),
as.numeric(arguments[10]),
as.numeric(arguments[11]))
# record which strategy has frequency above cutoff; if none, set to zero
if (length(which(output[nrow(output),1:4] > cutoff) > 0)) {
strategy[which(p[[1]] == p1), which(p[[2]] == p2)] <-
which(output[nrow(output),1:4] > cutoff)
} else {
strategy[which(p[[1]] == p1), which(p[[2]] == p2)] <- 0
}
}
}
# draw heatmap from strategy
heatmap(strategy,
Rowv = NA,
Colv = NA,
labRow = p[[1]],
labCol = p[[2]],
col = c("grey","royalblue","orange","springgreen4","orchid"),
breaks = c(-.5,0.5,1.5,2.5,3.5,4.5),
scale = "none",
ylab = names(p[1]),
xlab = names(p[2]))
legend("bottomleft",
legend = c("IL", "UB", "PB", "CB"),
fill = c("royalblue", "orange", "springgreen4","orchid"),
cex = 0.8,
bg = "white")
# export strategy
strategy
}
```
Here we create a heatmap across values of $v$ (rate of environmental change) and $p_i$ (accuracy of individual learning):
```{r}
parameters <- list(p_i = seq(0.5,1,0.05), v = seq(0.5,1,0.05))
strategy <- SLStemporalheatmap(parameters)
```
The heatmap above shows that PB is favoured at lower values of $v$ and $p_i$, otherwise UB is favoured. This makes sense. When $v$ is high, environments are relatively stable, and the advantage to PB of more rapidly identifying the new correct behaviour following environmental change is not so useful. UB agents with their lower costs do better once enough critical learners with the correct behaviour have accumulated in the population. Similarly, when $p_i$ is high, IL agents or critical learners using individual learning can rapidly identify the correct behaviour following an environmental change. UB agents can then copy one of these correct agents at random at a lower cost than PB agents.
Are there any parameter values where CB outperforms PB? This should depend first on the relative cost to CB vs PB:
```{r}
parameters <- list(c_c = c(0,0.001,0.005,0.01,0.05,0.1),
c_p = c(0,0.001,0.005,0.01,0.05,0.1))
strategy <- SLStemporalheatmap(parameters)
```
This heatmap shows that CB is favoured when the cost to CB $c_c$ is low, and the cost to PB $c_p$ is high (the bottom right of the heatmap). When the cost to both is high then the costless UB is favoured (top right). Otherwise PB is favoured.
If we set the relative cost of PB to be very high such that PB is effectively removed from the simulation, and reduce the cost of CB so that it has a bigger advantage over UB, we can explore the effect of $n$ and $f$ on the success of CB:
```{r}
parameters <- list(n = c(1,3,5,7,9,11), f = c(1,2,3,5,10,12))
strategy <- SLStemporalheatmap(parameters, c_p = 0.1, c_c = 0.01)
```
CB is favoured at higher values of $f$ and $n$. Given that $f$ is the strength of conformity, this makes sense. Conformity will also be more effective with more demonstrators (larger $n$), as it is more likely that the majority correct behaviour in the entire sub-population is the majority in the sample of $n$ demonstrators. However, larger $n$ will also favour payoff bias, which as we have already seen out-performs CB unless $c_p$ is very large.
## Model 19b: Spatially varying environments
Now let's make environments vary spatially rather than temporally and see if the results change. This situation is similar to Model 7 (migration) where we simulated different groups or sub-populations within the larger population with occasional migration between those groups. We assume here that each group has a different optimal behaviour. The rate of spatial environmental variation is therefore the rate of migration, i.e. the probability that an agent finds themselves in a new group/environment. We remove temporal variation entirely, so optimal behaviours within groups stay the same throughout the simulation.
More specifically, and following Nakahashi et al. (2012), we assume $G$ groups each containing $N$ agents, such that there are now $NG$ agents in total. There are also $G$ different behaviours labelled 1, 2, 3...$G$. In each group, one behaviour is correct and all the others are (equally) incorrect. Hence in group 1, behaviour 1 is correct and gives a fitness of $1 + b$ while behaviours 2 to $G$ are all incorrect and give a fitness of 1. In group 2, behaviour 2 gives fitness $1 + b$ and the others give fitness of 1, and so on. There are no behaviours that are incorrect in all groups.
As in Model 7, the rate of migration is $m$ and migration follows Wright's island model. Each generation, each agent has a probability $m$ of being taken out of its group and into a pool of migrants. These migrants are then randomly put back into the now-empty slots ignoring group structure.
Social learning, whether UB, CB or PB, occurs within groups. UB agents pick a single random member of their group to copy, CB agents adopt the majority behaviour amongst $n$ demonstrators from their group, and PB agents copy the correct behaviour if it is present in at least one of $n$ demonstrators from their group.
Individual learners are unaffected by spatial environmental variation just as they were unaffected by temporal environmental variation. The critical learners are affected, but differently to temporal variation. Whereas temporal environmental change renders all previous agents' behaviour incorrect, migrants to a new group can learn from demonstrators who have been in that group previously and potentially already acquired the correct behaviour.
The first step is to create our agent dataframe, this time with a variable *group* denoting the group id of each agent. Although currently unspecified, the *behaviour* variable of *agent* will now hold the numeric behaviour value from 1 to $G$, rather than 0 or 1. Agents are correct and get a fitness bonus when their *behaviour* value matches their *group* id. The *output* dataframe is identical to Model 19a.
```{r}
N <- 100
G <- 5
t_max <- 100
# create agents, initially all individual learners
agent <- data.frame(group = rep(1:G, each = N),
learning = rep("IL", N*G),
behaviour = rep(NA, N*G), # this is now 1,2...G
fitness = rep(NA, N*G))
# keep track of freq and fitnesses of each type
output <- data.frame(ILfreq = rep(NA, t_max),
UBfreq = rep(NA, t_max),
PBfreq = rep(NA, t_max),
CBfreq = rep(NA, t_max),
ILfitness = rep(NA, t_max),
UBfitness = rep(NA, t_max),
PBfitness = rep(NA, t_max),
CBfitness = rep(NA, t_max))
```
As before, we first run one generation of individual learning before composing the full set of timestep events. Note the new code for individual learning which is now group-specific. We cycle through each group and set the behaviour to match the group id $g$ with probability $p_i$, otherwise another random id from 1 to $G$ excluding $g$ (denoted by `(1:G)[-g]`) with equal probability. Fitness is also group specific, with a bonus only if the agent's behaviour matches their group. The code for recording type frequency and fitness, and for mutation, are identical to Model 19a except now there are $NG$ agents rather than $N$.
```{r}
t <- 1
p_i <- 0.5
b <- 1
c_i <- 0.2
mu <- 0.5
# individual learning for ILs
# for each group, acquire correct group beh with prob p_i, otherwise random other
for (g in 1:G) {
ingroup <- agent$learning == "IL" & agent$group == g
agent$behaviour[ingroup] <- sample(c(g,(1:G)[-g]),
sum(ingroup),
replace = TRUE,
prob = c(p_i, rep((1-p_i)/(G-1), G-1)))
}
# get fitnesses
agent$fitness <- ifelse(agent$behaviour == agent$group, 1 + b, 1)
agent$fitness[agent$learning == "IL"] <- agent$fitness[agent$learning == "IL"] - c_i
head(agent)
# record frequencies and fitnesses of each type
output$ILfreq[t] <- sum(agent$learning == "IL") / (N*G)
output$UBfreq[t] <- sum(agent$learning == "UB") / (N*G)
output$PBfreq[t] <- sum(agent$learning == "PB") / (N*G)
output$CBfreq[t] <- sum(agent$learning == "CB") / (N*G)
output$ILfitness[t] <- mean(agent$fitness[agent$learning == "IL"])
output$UBfitness[t] <- mean(agent$fitness[agent$learning == "UB"])
output$PBfitness[t] <- mean(agent$fitness[agent$learning == "PB"])
output$CBfitness[t] <- mean(agent$fitness[agent$learning == "CB"])
head(output)
# mutation
mutate <- runif(N) < mu
IL_mutate <- agent$learning == "IL" & mutate
UB_mutate <- agent$learning == "UB" & mutate
PB_mutate <- agent$learning == "PB" & mutate
CB_mutate <- agent$learning == "CB" & mutate
agent$learning[IL_mutate] <- sample(c("UB","PB","CB"), sum(IL_mutate), replace = TRUE)
agent$learning[UB_mutate] <- sample(c("IL","PB","CB"), sum(UB_mutate), replace = TRUE)
agent$learning[PB_mutate] <- sample(c("IL","UB","CB"), sum(PB_mutate), replace = TRUE)
agent$learning[CB_mutate] <- sample(c("IL","UB","PB"), sum(CB_mutate), replace = TRUE)
```
Now we can start the sequence of within-generation events. First we record the previous agent dataframe and do individual learning, as above.
```{r}
t <- 2
# 1. store previous timestep agent
previous_agent <- agent
# 2. individual learning for ILs
# for each group, acquire correct group beh with prob p_i, otherwise random other
for (g in 1:G) {
ingroup <- agent$learning == "IL" & agent$group == g
agent$behaviour[ingroup] <- sample(c(g,(1:G)[-g]),
sum(ingroup),
replace = TRUE,
prob = c(p_i, rep((1-p_i)/(G-1), G-1)))
}
```
Next is social learning. Now that demonstrators must come from the agent's own group, we need slightly more complex code to create the *dems* dataframe. The following code cycles through each group, samples $n$ demonstrators for each agent within that group, and adds them to the *dems* matrix using the **rbind** command.
```{r}
n <- 3
# 3. social learning for critical learners
# select group-specific demonstrators
dems <- NULL
# cycle through each group
for (g in 1:G) {
ingroup <- agent$group == g
dems <- rbind(dems,
matrix(data = sample(previous_agent$behaviour[ingroup], N*n, replace = TRUE),
nrow = N, ncol = n))
}
head(dems)
```
UB agents copy the first demonstrator stored in the first column of *dems*. As before, there is no directional change in the frequency of correct behaviour due to UB.
```{r}
# proportion correct before social learning
mean(agent$behaviour[agent$learning == "UB"] == agent$group[agent$learning == "UB"])
# for UBs, copy the behaviour of the 1st dem in dems
agent$behaviour[agent$learning == "UB"] <- dems[agent$learning == "UB", 1]
# proportion correct after social learning
mean(agent$behaviour[agent$learning == "UB"] == agent$group[agent$learning == "UB"])
```
PB agents copy the correct behaviour in their group if at least one demonstrator has the correct behaviour, otherwise they copy their first (incorrect) random demonstrator. Here *PB_correct* identifies PB agents who observe at least one correct demonstrator, and *PB_incorrect* identifies PB agents who observe all incorrect demonstrators.
```{r}
# proportion correct before social learning
mean(agent$behaviour[agent$learning == "PB"] == agent$group[agent$learning == "PB"])
# for PB, copy correct if at least one of dems is correct
PB_correct <- agent$learning == "PB" & rowSums(dems == agent$group) > 0
PB_incorrect <- agent$learning == "PB" & rowSums(dems == agent$group) == 0
agent$behaviour[PB_correct] <- agent$group[PB_correct]
agent$behaviour[PB_incorrect] <- dems[PB_incorrect, 1]
# proportion correct after social learning
mean(agent$behaviour[agent$learning == "PB"] == agent$group[agent$learning == "PB"])
```
As in Model 19a, you should see the frequency of correct behaviours increase after payoff biased copying.
Finally CB agents are disproportionately more likely to copy the majority trait amongst the demonstrators. Because there are now potentially more than two behaviours when $G > 2$, we need to calculate the number of times each trait appears amongst the $n$ demonstrators for each agent (stored in *counts*), get copy probabilities for each trait based on the counts and Equation 17.2 (stored in *copy_probs*), then actually pick a trait based on these probabilities using **apply** on *copy_probs*.
```{r}
f <- 3
# proportion correct before social learning
mean(agent$behaviour[agent$learning == "CB"] == agent$group[agent$learning == "CB"])
# for CB, copy majority behaviour according to parameter f
# get number of times each trait appears in n dems for each agent
counts <- matrix(nrow = N*G, ncol = G)
for (g in 1:G) counts[,g] <- rowSums(dems == g)
# get copy probs based on counts and f
copy_probs <- matrix(nrow = N*G, ncol = G)
for (g in 1:G) copy_probs[,g] <- rowSums(dems == g)^f / rowSums(counts^f)
# pick a trait based on copy_probs
CB_beh <- apply(copy_probs, 1, function(x) sample(1:G, 1, prob = x))
# CB agents copy the trait
agent$behaviour[agent$learning == "CB"] <- CB_beh[agent$learning == "CB"]
# proportion correct after social learning
mean(agent$behaviour[agent$learning == "CB"] == agent$group[agent$learning == "CB"])
```
As in Model 19a, CB is unlikely to have increased the frequency of correct behaviours by much, if at all.
Steps 4, 5 and 6 are similar to before: incorrect critical learners engage in individual learning, fitnesses are calculated, and frequencies and mean fitness of each type are recorded in *output*.
```{r}
c_c <- 0.02
c_p <- 0.02
# 4. incorrect social learners engage in individual learning
incorrect_SL <- agent$learning != "IL" & agent$behaviour != agent$group
for (g in 1:G) {
ingroup <- incorrect_SL & agent$group == g
agent$behaviour[ingroup] <- sample(c(g,(1:G)[-g]),
sum(ingroup),
replace = TRUE,
prob = c(p_i, rep((1-p_i)/(G-1), G-1)))
}
# 5. calculate fitnesses
# all correct agents get bonus b
agent$fitness <- ifelse(agent$behaviour == agent$group, 1 + b, 1)
# costs of learning
agent$fitness[agent$learning == "IL"] <- agent$fitness[agent$learning == "IL"] - c_i
agent$fitness[agent$learning == "PB"] <- agent$fitness[agent$learning == "PB"] - c_p
agent$fitness[agent$learning == "CB"] <- agent$fitness[agent$learning == "CB"] - c_c
agent$fitness[incorrect_SL] <- agent$fitness[incorrect_SL] - c_i
# 6. record frequencies and fitnesses of each type
output$ILfreq[t] <- sum(agent$learning == "IL") / (N*G)
output$UBfreq[t] <- sum(agent$learning == "UB") / (N*G)
output$PBfreq[t] <- sum(agent$learning == "PB") / (N*G)
output$CBfreq[t] <- sum(agent$learning == "CB") / (N*G)
output$ILfitness[t] <- mean(agent$fitness[agent$learning == "IL"])
output$UBfitness[t] <- mean(agent$fitness[agent$learning == "UB"])
output$PBfitness[t] <- mean(agent$fitness[agent$learning == "PB"])
output$CBfitness[t] <- mean(agent$fitness[agent$learning == "CB"])
head(output)
```
Step 7 is selection and reproduction. We make this occur within groups; if social learning occurs within groups, it makes sense that mating and reproduction also happens within groups.
```{r}
# before selection
table(agent$learning)/(N*G)
# 7. selection and reproduction
# (now within-group)
for (g in 1:G) {
ingroup <- agent$group == g
relative_fitness <- agent$fitness[ingroup] / sum(agent$fitness[ingroup])
agent$learning[ingroup] <- sample(agent$learning[ingroup],
N,
prob = relative_fitness,
replace = TRUE)
}
# after selection
table(agent$learning)/(N*G)
```
Next is mutation, identical to above, and finally some new code implementing migration. Just as in Model 4, we select migrants with probability $m$ per agent, add these migrants' learning type and behaviour to a *migrants* dataframe, then put these randomly back into the migrant slots ignoring group id.
```{r}
m <- 0.1
# 8. mutation
mutate <- runif(N*G) < mu