-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis-experiment-1.Rmd
1292 lines (1086 loc) · 45.1 KB
/
analysis-experiment-1.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: "Analysis for 'Colour biases in learned foraging preferences in Trinidadian guppies'"
subtitle: "Experiment 1 Data Preparation and Analysis"
author:
- name: "M. Wyatt Toure^1^, Simon M. Reader^1^"
affiliation: "^1^McGill University, Department of Biology, 1205 Docteur Penfield, Montreal, Quebec H3A 1B1, Canada"
date: "Last Update: `r format(Sys.Date(), '%b %d %Y')`"
output:
bookdown::html_document2:
includes:
in_header: docs/header.html
toc: true
toc_depth: 4
toc_float:
collapsed: false
number_sections: false
split_by: section
css: styles.css
bibliography: ["references/references.bib"]
csl: references/elife-citation-style.csl
link-citations: yes
knit: (function(input, ...) {
rmarkdown::render(
input,
output_file = 'docs/analysis-experiment-1')
})
---
***
## Overview
This page reports the analyses for the first experiment described in 'Colour
biases in learned foraging preferences in Trinidadian guppies'. The R script to
reproduce the analysis and this site are in the
[analysis-experiment-1.Rmd](https://github.com/wyatt-toure/guppy-colour-learning-project/blob/main/analysis-experiment-1.Rmd)
file. The raw data used to conduct these analyses are available in the
[colour-learning-experiment-1-data.csv](https://github.com/wyatt-toure/guppy-colour-learning-project/blob/main/data/colour-learning-experiment-1-data.csv)
file. Note the code blocks that produce the figures and tables are not shown on
this page as they are rather long, however the code to produce the figures and
tables can also be seen in
[analysis-experiment-1.Rmd](https://github.com/wyatt-toure/guppy-colour-learning-project/blob/main/analysis-experiment-1.Rmd).
To get straight to the results go to the [Models](#models) section. To see how
to reproduce these results please visit the [How to Reproduce the
Results](https://github.com/wyatt-toure/guppy-colour-learning#how-to-reproduce-the-results)
section of the README.
***
```{r library-prep, include=FALSE}
# Loading required packages
library(lme4)
library(tidyr)
library(lmerTest)
library(ggplot2)
library(ggpubr)
library(DHARMa)
library(dplyr)
library(broom)
library(broom.mixed)
library(knitr)
library(emmeans)
library(report)
library(glmmTMB)
library(MASS)
source("R/format-p-value.R")
source("R/rename-lme4-model.R")
source("R/geom-flat-violin.R")
source("R/report-est-plus-minus-std-error.R")
source("R/report-ci.R")
```
## Data preparation
At the time experiment 1 was conducted I (MWT) still performed much data
preparation manually in Excel so unlike in experiment 2, we do not format the
data in R for experiment 1. Additionally, several variables were created in
Excel. This Excel sheet was then exported as the `.csv` file
[colour-learning-experiment-1-data.csv](https://github.com/wyatt-toure/guppy-colour-learning-project/blob/main/data/colour-learning-experiment-1-data.csv).
All measures in
[colour-learning-experiment-1-data.csv](https://github.com/wyatt-toure/guppy-colour-learning-project/blob/main/data/colour-learning-experiment-1-data.csv)
except `ate` (denoting whether a fish ate during a trial or not) are derived
from the first 16 columns of that data sheet. Please see the [Variable
Creation](https://wyatt-toure.github.io/guppy-colour-learning/analysis-experiment-2.html#Variable_creation)
section of Experiment 2's analysis page to see how the additional measures are
derived. Variables necessary for the analysis which were not created manually
are created below. Descriptions of the variables found in the data set are given
in the Experiment 1 Metadata section of
[`metadata.md`](https://github.com/wyatt-toure/guppy-colour-learning/blob/main/data/metadata.md#experiment-1-metadata).
### Variable creation
The preference metrics `green.object.preference` and
`rewarding.object.preference` are created by subtracting the time spent near the
blue object from the time spent near the green object and subtracting the time
spent near the untrained object from the time spent near the trained object
respectively. The proportional rewarding object preference
`prop.rewarding.object.preference` is created by dividing the time spent near
the trained object by the time spent near both objects.
```{r data-prep}
# Reading in Data
my_data <- read.csv("data/colour-learning-experiment-1-data.csv")
# Creating new variables
## Green object preference
my_data <- my_data %>%
mutate(
green.object.preference =
case_when(
rewarding.object.colour == "green" ~
time.with.trained.object - time.with.untrained.object,
rewarding.object.colour == "blue" ~
time.with.untrained.object - time.with.trained.object
)
)
## Rewarding object preference
my_data <- my_data %>%
mutate(
rewarding.object.preference =
time.with.trained.object - time.with.untrained.object
)
## Rewarding object proportional preference
my_data <- my_data %>%
mutate(
prop.rewarding.object.preference =
(time.with.trained.object / (time.with.trained.object + time.with.untrained.object))
)
```
### Subsetting data
We remove males from our analyses due to consistently low feeding motivation in
this particular experimental design across all males. However, we note that the
conclusions of the experiment do not change even if males are included, one can
remove the line of code below removing males from the analyses and re run the R
script and find that the statistical conclusions are maintained. Additionally,
two fish died before testing in Experiment 1 and were not included in the
analyses.
```{r remove-males}
# Removing males
my_data <- my_data %>%
filter(
id != "a3", id != "b3",
id != "c3", id != "e3",
id != "g3", id != "v3",
id != "w3", id != "x3",
id != "b1", id != "b2"
) %>%
droplevels()
```
To conduct the analyses we planned, we create subsets of the full data set that
are restricted to the training trials (reinforced), the test trials
(unreinforced), and the initial test trial (unreinforced) using the `filter()`
function from `dplyr`. We change trial to a factor for the unreinforced test
trial data subset since there are two levels of trial being compared to each
other for the analysis on this data set. Trial in the training data subset is
coded as integer to allow us to look at trends in the shift of rewarding object
preference during training.
```{r data-subets}
# Restrict data to training data
training_data <- my_data %>%
filter(trial.type == "training")
# Restrict data to only the baseline and re-test data
test_data <- my_data %>%
filter(trial.type == "test")
# Restrict data to only the baseline data
baseline_data <- my_data %>%
filter(trial == 0)
# Change trial to factor
test_data$trial <- as.factor(test_data$trial)
baseline_data$trial <- as.factor(baseline_data$trial)
```
### Figures directory
This script will generate figures but to store them we need to create specific
directories that have been hard coded into the script. We create the `figs/`
directory and all the subdirectories within it using the next line of code. Now
all figures that are created in this script are accessible as individual files
in the `figs/` directory. If this script is run multiple times it will return a
warning saying that the directory already exists. However, this is not
problematic since the figures are always regenerated by running the script so we
set `showWarnings` to `FALSE`. Every figure seen on this page will be available
as an individual file in the `figs/` directory if one runs all the code chunks
of `analysis-experiment-1.Rmd`.
```{r}
dir.create(file.path("figs/exp-1/residual-plots"),
recursive = TRUE,
showWarnings = FALSE
)
```
***
## Models
We analysed the data from our experiment using linear, linear mixed effect,
generalized linear mixed effect, and generalized linear models with the `lm()`,
`lmer()`, `glmmTMB()`, and `glm.nb()` functions from the `stats`, `lme4`,
`glmmTMB`, and `MASS` packages respectively. P-values and effective degrees of
freedom for `lme4` models were obtained using the `lmerTest` package. Model
residuals were checked they met distributional assumptions with the `DHARMa`
package, you can click the 'See Model Residuals' button below the model formulas
to see the residual diagnostic plots produced by `DHARMa` for that particular
model.
### Model 1 - Preference for the green object at baseline
This first model contains the data for all individual guppies. We looked at the
green object preference of all guppies in an intercept only model to see if the
green object preference at baseline was significantly different from zero.
`green.object.preference` is the time spent near the green object subtracted by
the time spent near the blue object
```{r model-1, echo=TRUE}
baseline_data_model <-
lm(green.object.preference ~ 1,
data = baseline_data
)
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName"> See Model 1 Residuals </button>
<div id="BlockName" class="collapse">
```{r, message=FALSE}
simulationOutput <- simulateResiduals(fittedModel = baseline_data_model)
plot(simulationOutput)
# Saving plot to figs directory
ggsave(
filename = "exp-1-model-1-residual-plot.png",
plot = (plot(simulationOutput)),
path = "figs/exp-1/residual-plots/",
device = "png",
dpi = 300
)
```
</div>
</br>
##### Result
```{r tidying-model-1, echo=FALSE, message=FALSE}
# Setting table row names
baseline_table_row_name_vec <- c("Intercept")
# Converting data frame to tibble
tidy_baseline_model <- broom.mixed::tidy(baseline_data_model)
# Changing tibble header names
tidy_baseline_model <- rename_tidy_lme4_cols(tidy_baseline_model)
# Changing tibble row names
tidy_baseline_model[1:1, 1] <- baseline_table_row_name_vec
```
```{r, results=TRUE, echo=FALSE}
knitr::kable(tidy_baseline_model %>%
mutate_if(is.numeric, round, digits = 3))
```
During the initial test, there was no significant preference for the green
object across all guppies (green object preference:
`r report_est_and_std_error(tidy_baseline_model, rounding = 0)` seconds, p =
`r tidy_baseline_model$'P value' %>% round(3)`).
```{r baseline-pref-plot, echo=FALSE, message=FALSE, fig.cap="Preference for the green object relative to the blue object across all guppies at baseline. Negative values represent more time spent with the blue object, positive values indicate more time spent with the green object. Data are means +/- 95% CI", fig.id="baseline-pref-plot", warning=FALSE, message=FALSE}
###### Baseline green object preference plot ######
baseline_data_x_axis_label <- "Initial Test"
ggplot(
baseline_data,
aes(
x = trial,
y = green.object.preference
)
) +
theme_minimal() +
ylab("Green object preference (sec)") +
xlab("") +
theme(
legend.position = "none",
axis.text = element_text(size = 14),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, hjust = 0.5)
) +
geom_hline(yintercept = 0, linetype = "dashed", alpha = 0.5) +
geom_jitter(width = 0.04, alpha = 0.3) +
stat_summary(
geom = "point",
fun = "mean",
size = 4.5,
shape = 15
) +
stat_summary(
geom = "errorbar",
fun.data = "mean_ci", position = position_dodge(width = 0), width = 0.1
) +
scale_x_discrete(labels = baseline_data_x_axis_label)
ggsave(
filename = "exp-1-model-1-baseline-data-plot.png",
path = "figs/exp-1/",
device = "png",
dpi = 300
)
```
***
### Model 2 - Preference for the rewarding object during training
To see whether fish were responsive during training our second model asks
whether the preference for the rewarding object changes throughout training and
whether the change in rewarding object preference is different between the
treatments.
- **Response variable:** `rewarding.object.preference` is the time (seconds)
spent near the rewarding object subtracted by the time spent near the
unrewarding object
- **Fixed effect:** `rewarding.object.colour` is the identity of the rewarding
object (blue or green)
- **Fixed effect:** `trial` is the number of the training trial. In this model
it is supplied as an integer
- **Random effect:** `id` is the identity of the individual fish
```{r model-2, echo=TRUE}
training_data_model <-
lmer(rewarding.object.preference ~ rewarding.object.colour * trial + (1 | id),
data = training_data
)
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName2"> See Model 2 Residuals </button>
<div id="BlockName2" class="collapse">
```{r, message=FALSE}
# Residual diagnostics
simulationOutput <- simulateResiduals(
fittedModel = training_data_model,
n = 1000
)
plot(simulationOutput)
# Saving plot to figs directory
ggsave(
filename = "exp-1-model-2-residual-plot.png",
plot = (plot(simulationOutput)),
path = "figs/exp-1/residual-plots/",
device = "png",
dpi = 300
)
```
We see that there is a slight amount of structure in the residuals, however,
visual inspection of the residuals reveals that this structure is minor. There
is not an indication of a gross model misfit so our model is still suitable.
</div>
```{r tidying-model-2, echo=FALSE, message=FALSE}
# Setting table row names
training_model_table_row_name_vec <- c(
"Intercept",
"Rewarding object colour",
"Trial",
"Rewarding object colour X Trial"
)
# Converting data frame to tibble
tidy_training_data_model <- broom.mixed::tidy(training_data_model)
# Formatting p value
tidy_training_data_model$p.value <- format_p_value(tidy_training_data_model$p.value)
# Changing tibble header names
tidy_training_data_model <- rename_tidy_lme4_cols(tidy_training_data_model)
# Changing tibble row names
tidy_training_data_model[1:4, 3] <- training_model_table_row_name_vec
```
</br>
##### Results
```{r, results=TRUE, echo=FALSE}
knitr::kable(tidy_training_data_model[1:4, ] %>%
dplyr::select(-group, -effect) %>%
mutate_if(is.numeric, round, digits = 3))
```
There was a significant effect of trial. Over the 20 training trials, guppies in
the two treatments increased their relative preference for their respective
rewarded objects by
`r report_est_and_std_error(tidy_training_data_model, rounding = 0)[3]` seconds
each trial (Figure \@ref(fig:colour-pref-training-plot), p
`r tidy_training_data_model$'P value'[3]`). There was also a significant effect
of rewarded-object colour (p = `r tidy_training_data_model$'P value'[2]`).
during training green-rewarded guppies expressed a stronger preference for their
rewarded object (the green object) than did blue-rewarded guppies did for the
blue object, a difference of
`r report_est_and_std_error(tidy_training_data_model, rounding = 0)[2]` seconds.
However, there was no significant interaction effect between rewarding object
colour and trial
(`r report_est_and_std_error(tidy_training_data_model, rounding = 0)[4]`
seconds, p = `r tidy_training_data_model$'P value'[4]`), *i.e.*, the rate of
increase in object preference over trials did not significantly differ between
the treatments.
```{r colour-pref-training-plot, echo=FALSE, message=FALSE, fig.cap="Relative preference for the green object in both treatments during training trials (trials 1-20). Negative values represent more time spent with the blue object, positive values indicate more time spent with the green object. Light lines connect individuals across trials and bold lines represents a linear fit with 95% CI (grey shading). Subjects were consistently rewarded for approaching the blue object (dashed lines) or the green object (solid lines).", fig.id="colour-pref-training-plot", warning=FALSE, message=FALSE}
###### Time with rewarding object plot during training ######
ggplot(
training_data,
aes(
x = trial,
y = green.object.preference,
color = rewarding.object.colour,
shape = rewarding.object.colour,
linetype = rewarding.object.colour
)
) +
theme_minimal() +
ylab("Green object preference (sec)") +
xlab("Trial") +
labs(col = "Rewarding object colour") +
theme(
legend.position = "none",
axis.text = element_text(size = 14),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, hjust = 0.5)
) +
scale_color_manual(values = c("#2980b9", "#27ae60")) +
scale_linetype_manual(values = c("longdash", "solid")) +
scale_shape_manual(values = c(15, 16)) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_point(alpha = 0.3) +
geom_line(aes(group = id), alpha = 0.2) +
scale_x_continuous(breaks = c(1:20)) +
scale_y_continuous(breaks = seq(-300, 300, by = 100)) +
geom_smooth(method = "lm", se = TRUE, alpha = 0.25) +
stat_summary(fun = "mean", size = 0.8)
ggsave(
filename = "exp-1-model-2-colour-pref-training-plot.png",
path = "figs/exp-1/",
device = "png",
dpi = 300
)
```
***
### Model 3 - Preference for the rewarded object during testing depending on treatment {#model-3}
For the main effects of training and rewarding object colour on rewarding object
preference we fit a generalized linear mixed effects model with a Gaussian
distribution which modeled the variances to account for variance heterogeneity
using the package
[`glmmTMB`](https://cran.r-project.org/web/packages/glmmTMB/index.html). Our
third model asks whether the preference for the rewarding object changed between
baseline and final test and looks for an interaction with rewarded object
colour. To control for heterogeneous variance across
trials we additionally modelled the variance due to trial across both colour
treatments.
```{r model-3, echo=TRUE}
test_data_model_glm <-
glmmTMB(rewarding.object.preference ~
trial * rewarding.object.colour + (1|id) +
diag(0 + rewarding.object.colour:trial |id),
data = test_data,
family = gaussian
)
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName3"> See Model 3 Residuals </button>
<div id="BlockName3" class="collapse">
```{r, include=TRUE, message=FALSE}
simulationOutput <- simulateResiduals(fittedModel = test_data_model_glm, n = 1000)
plot(simulationOutput)
# Saving plot to figs directory
ggsave(
filename = "exp-1-model-3-residual-plot.png",
plot = (plot(simulationOutput)),
path = "figs/exp-1/residual-plots/",
device = "png",
dpi = 300
)
```
</div>
```{r tidying-model-3, echo=FALSE, message=FALSE}
# Setting table row names
test_model_table_row_name_vec <- c(
"Intercept",
"Trial",
"Rewarding object colour",
"Rewarding object colour X Trial"
)
# Converting data frame to tibble
tidy_test_data_model <- broom.mixed::tidy(test_data_model_glm)
# Formatting p value
tidy_test_data_model$p.value <- format_p_value(tidy_test_data_model$p.value)
# Changing tibble header names
tidy_test_data_model <- rename_tidy_lme4_cols(tidy_test_data_model)
# Changing tibble row names
tidy_test_data_model[1:4, 4] <- test_model_table_row_name_vec
```
</br>
##### Results
```{r model-3-table, results=TRUE, echo=FALSE}
knitr::kable(tidy_test_data_model[1:4, ] %>%
dplyr::select(-group, -effect, -component) %>%
mutate_if(is.numeric, round, digits = 3),
caption = "Summary table for Model 3. Estimates ± standard error (SE) of the
effects of trial and rewarding object colour on the rewarding object preference
from the generalized linear mixed effect model containing the effects Trial,
Rewarding object colour, and their interaction effect (Trial X Rewarding object
colour)."
)
```
When comparing the initial and final preference test, both conducted after
training and without food rewards present, we found a significant interaction
effect between test and rewarding object colour (p =
`r tidy_test_data_model$'P value'[4]`). Guppies that had been green-rewarded had
a shift in their rewarded object preference that was on average
`r report_est_and_std_error(tidy_test_data_model, rounding = 0)[4]` seconds
stronger than the shift in rewarded object preference of guppies trained to blue
(Figure \@ref(fig:test-data-pref-plot)).
```{r test-data-pref-plot, echo=FALSE, message=FALSE, fig.cap="Changes in object preference from an initial test before training to a final test after training. During training, fish were rewarded for approaching the blue object (blue squares and lines) or the green object (green squares and lines). At test, no food reward was present. Dashed line represents an equal preference for either object. Data are means ± 95% CI; lighter points and lines are data for each individual. The final preference for green-rewarded fish is stronger than that of fish that has been rewarded for approaching the blue object.", fig.id="test-data-pref-plot", warning=FALSE, message=FALSE}
###### Time with rewarding object plot during testing ######
testing_data_x_axis_labels <- c("Initial test", "Final test")
object_labels <- c("Blue-trained", "Green-trained")
names(object_labels) <- c("blue", "green")
ggplot(
test_data,
aes(
x = trial,
y = rewarding.object.preference,
color = rewarding.object.colour,
shape = rewarding.object.colour
)
) +
theme_minimal() +
geom_jitter(width = 0, alpha = 0.3) +
stat_summary(geom = "point", fun = "mean", size = 4.5) +
stat_summary(
geom = "errorbar",
fun.data = "mean_ci",
position = position_dodge(width = 0),
width = 0.1
) +
ylab("Rewarding object preference (sec)") +
xlab("Trial") +
labs(col = "Rewarding object colour") +
theme(
legend.position = "none",
axis.text = element_text(size = 14),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, hjust = 0.5),
strip.text.x = element_text(size = 14, face = "bold")
) +
scale_color_manual(values = c("#2980b9", "#27ae60")) +
scale_shape_manual(values = c(15, 16)) +
geom_hline(yintercept = 0, linetype = "dashed", alpha = 0.6) +
geom_line(aes(group = id), alpha = 0.2) +
scale_x_discrete(labels = testing_data_x_axis_labels) +
facet_grid(~rewarding.object.colour,
labeller = labeller(rewarding.object.colour = object_labels)
) +
stat_summary(fun = mean,
geom = "line",
aes(group = rewarding.object.colour)) +
scale_y_continuous(breaks = seq(-50, 200, by = 25))
ggsave(
filename = "exp-1-model-3-test-data-pref-plot.png",
path = "figs/exp-1/",
device = "png",
dpi = 300
)
```
</br>
#### Post-hoc Comparisons
To determine whether the means of the final rewarding object preference for the
two treatments were different We conducted post-hoc comparisons with the package
[`emmeans`](https://cran.r-project.org/package=emmeans). We compared the
following means:
- Final test blue-trained and initial test blue-trained
- Final test green-trained and initial test green-trained
- Final test green-trained and final test blue-trained
- Initial test green-trained and initial test blue-trained
```{r post-hoc-comparisons, echo=TRUE}
test_data_model_emmeans <- emmeans(test_data_model_glm,
specs = ~ trial:rewarding.object.colour
)
# Making vectors to represent means of interest from emmeans() output
blue0 <- c(1, 0, 0, 0)
blue21 <- c(0, 1, 0, 0)
green0 <- c(0, 0, 1, 0)
green21 <- c(0, 0, 0, 1)
# Set seed to prevent confidence intervals from changing when code is re-run
set.seed(123)
custom_contrasts <- contrast(test_data_model_emmeans,
method = list(
"21 blue - 0 blue" = blue21 - blue0,
"21 green - 0 green " = green21 - green0,
"21 green - 21 blue" = green21 - blue21,
"0 green - 0 blue" = green0 - blue0
), adjust = "mvt"
) %>%
summary(infer = TRUE)
```
</br>
##### Results
```{r contrasts-table-formatting, echo=FALSE, fig.id="contrasts-table", warning=FALSE}
# Convert contrasts to data frame
pairwise_comparison_contrasts_table <- as.data.frame(custom_contrasts)
# Formatting p values
pairwise_comparison_contrasts_table$p.value <- format_p_value(pairwise_comparison_contrasts_table$p.value)
```
```{r contrasts-table-display, echo=FALSE}
knitr::kable(pairwise_comparison_contrasts_table %>%
dplyr::select(contrast, estimate, lower.CL, upper.CL, df, p.value) %>%
mutate_if(is.numeric, round, digits = 3) %>%
rename(
"Contrast" = contrast,
"Estimate" = estimate,
"Lower CL" = lower.CL,
"Upper CL" = upper.CL,
"P Value" = p.value
),
caption = "Table of post-hoc tests with a multivariate-t adjustment for multiple
comparisons of a selected set of means. The numbers represent the initial test
(0) and the final test (21). The colour corresponds to the identity of the
rewarding object (blue for blue-rewarded guppies, green for green-rewarded
guppies). Values are all rounded to 3 decimal places. CL = confidence limit."
)
```
Post-hoc comparisons (Table \@ref(tab:contrasts-table-display)) reveal that
initially, before training, there was no significant difference in the strength
of preference for the rewarded object between the treatments
(`r report_est_and_std_error(pairwise_comparison_contrasts_table, 0, F)[4]`
seconds, p = `r pairwise_comparison_contrasts_table$p.value[4]`). The shift in
rewarded object preference between the initial and final preference tests was
significant for green-trained guppies but not for blue-trained guppies: green
trained guppies increased their preference for the green object by
`r report_est_and_std_error(pairwise_comparison_contrasts_table, 0, F)[2]`
seconds, from initial to final (p
`r pairwise_comparison_contrasts_table$p.value[2]`) test whereas blue-trained
guppies increased their preference for the blue object by
`r report_est_and_std_error(pairwise_comparison_contrasts_table, 0, F)[1]`
seconds an effect that was not statistically significant (p =
`r pairwise_comparison_contrasts_table$p.value[1]`). At final test,
green-rewarded guppies had a significantly stronger preference for the
previously rewarded object compared to the blue-rewarded guppies (Difference in
preference:
`r report_est_and_std_error(pairwise_comparison_contrasts_table, 0, F)[3]`
seconds, p = `r pairwise_comparison_contrasts_table$p.value[3]`).
Additionally, we compared the mean for each colour group in each test trial
against zero to see if the preference for the rewarding object was significantly
different from zero within a test trial.
```{r, eval=FALSE}
set.seed(123)
emmeans(test_data_model_glm,
specs = ~ rewarding.object.colour:trial,
adjust = "mvt"
) %>%
summary(infer = TRUE)
```
```{r, echo=FALSE}
set.seed(123)
contrast_against_chance <- as.data.frame(
emmeans(test_data_model_glm,
specs = ~ rewarding.object.colour:trial,
adjust = "mvt"
) %>%
summary(infer = TRUE)
) %>%
mutate_if(is.numeric, round, digits = 3)
contrast_against_chance$p.value <-
format_p_value(contrast_against_chance$p.value)
contrast_against_chance$trial <- c(
"Initial Test",
"Initial Test",
"Final Test",
"Final Test"
)
contrast_against_chance %>%
rename(
"Rewarding object colour" = rewarding.object.colour,
"Trial" = trial,
"Estimate" = emmean,
"Std. Error" = SE,
"T ratio" = t.ratio,
"P Value" = p.value,
"Lower CL" = lower.CL,
"Upper CL" = upper.CL
) %>%
kable(caption = "Comparisons of estimated marginal means of rewarding object preference by rewarding object colour for every test trial in Experiment 1 against zero. A multivariate t adjustment was used to address multiple comparisons.")
```
We find that only green-trained guppies during the final test trial display a
rewarding object preference that is significantly different from chance (green
object preference: `r contrast_against_chance$emmean[4] %>% round(0)` ±
`r contrast_against_chance$SE[4] %>% round(0)` seconds, p
`r contrast_against_chance$p.value[4]`).
***
```{r feeding-data-prep, include = FALSE}
#### Get feeding data #####
# Group by ID and count the number of sessions in which an individual ate
feeding <- my_data %>%
group_by(id) %>%
count(feeding.count = ate == "yes")
# Remove NAs from this
feeding <- na.omit(feeding)
# Count only the yeses
feeding <- feeding %>%
filter(feeding.count == "TRUE")
# Remove the column feeding.count to keep only the counts
feeding <- feeding %>%
dplyr::select(-feeding.count)
# Add the feeding values to the main data frame so we can get treatment IDs
my_feeding_data <- left_join(baseline_data, feeding, by = "id")
# Replace NAs with 0, rename n to feeding count, and extract id, feeding count,
# and rewarding object colour treatment
my_feeding_data <- my_feeding_data %>%
replace_na(list(n = 0)) %>%
rename(feeding.count = n) %>%
dplyr::select(id, feeding.count, rewarding.object.colour)
```
### Model 4 - Is there a difference in feeding attempts between treatments? {#model-4}
A discrepancy in reinforcement between treatments may influence performance on a
final preference test. To see whether there was a difference in feeding between
treatments we counted the number of trials in which an individual fish ate
throughout all of training and compared the feeding counts between treatments.
To do this we fit a generalized linear model with a negative binomial
distribution. The response variable 'feeding count' is a sum of the number of
trials in which a guppy ate.
##### Model
```{r model-4, echo=TRUE}
feeding_data_model <-
glm.nb(feeding.count ~ rewarding.object.colour,
data = my_feeding_data
)
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName4"> See Model 4 Residuals </button>
<div id="BlockName4" class="collapse">
```{r, warning=FALSE, message=FALSE}
simulationOutput <- simulateResiduals(fittedModel = feeding_data_model)
plot(simulationOutput)
# Saving plot to figs directory
ggsave(
filename = "exp-1-model-4-residual-plot.png",
plot = (plot(simulationOutput)),
path = "figs/exp-1/residual-plots/",
device = "png",
dpi = 300
)
```
</div>
```{r tidying-model-4, echo=FALSE, message=FALSE}
# Setting table row names
feeding_model_table_row_name_vec <- c(
"Intercept",
"Rewarding object colour"
)
# Converting data frame to tibble
tidy_feeding_data_model <- broom.mixed::tidy(feeding_data_model)
# Changing tibble header names
tidy_feeding_data_model <- rename_tidy_lme4_cols(tidy_feeding_data_model)
# Changing tibble row names
tidy_feeding_data_model[1:2, 1] <- feeding_model_table_row_name_vec
```
</br>
##### Results
```{r, results=TRUE, echo=FALSE}
knitr::kable(tidy_feeding_data_model[1:2, ] %>%
mutate_if(is.numeric, round, digits = 3))
```
We found no significant difference in the number of trials individuals fed
between green-rewarded and blue-rewarded fish (Figure
\@ref(fig:feeding-count-plot), p =
`r tidy_feeding_data_model$'P value'[2] %>% round(3)`).
We also incorporated feeding count as a covariate in [Model 3](#model-3),
finding the same pattern of results (See [ESM Model 1](#esm-model-1)).
```{r feeding-count-plot, echo=FALSE, message=FALSE, fig.cap="Average number of trials in which a fish fed during training. Data are means ± 95% confidence intervals with probability density functions of the data to the right of the raw data.", fig.id="training-data-ate-plot", warning=FALSE}
ggplot(
my_feeding_data,
aes(
x = rewarding.object.colour,
y = feeding.count,
fill = rewarding.object.colour,
colour = rewarding.object.colour
)
) +
geom_point(position = position_jitter(width = 0.05), alpha = 0.8) +
stat_summary(geom = "point", fun = "mean", size = 4.5, shape = 15) +
stat_summary(
geom = "errorbar",
fun.data = "mean_ci",
position = position_dodge(width = 0),
width = 0.1
) +
ylim(-3, 23) +
ylab("Rewarding object preference") +
xlab("Trial") +
theme_minimal() +
guides(fill = "none", colour = "none") +
ylab("Number of trials fed") +
xlab("Rewarding object colour") +
labs(col = "Rewarding object colour") +
theme(
legend.position = "top",
axis.text = element_text(size = 14),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, hjust = 0.5)
) +
scale_color_manual(values = c("#2980b9", "#27ae60")) +
scale_fill_manual(values = c("#2980b9", "#27ae60"))
# Saving plot
ggsave(
filename = "exp-1-model-4-feeding-count-plot.png",
path = "figs/exp-1/",
device = "png",
dpi = 300
)
```
***
## ESM Models
In this section we describe models not included in the main text.
### ESM Model 1 - Including feeding count as a covariate {#esm-model-1}
This model was run to address the potential role of feeding count on test
performance. The concern was that the results may be explained by differential
levels of reinforcement between the groups or between individuals during
training. To ensure our results were robust to matters arising from feeding
count variation we included feeding count as a co-variate and re-ran the
analysis in [Model 3](#model-3). With this model we looked to see whether
including the amount of trials an individual fed as a covariate in the model
changes the conclusions.
```{r, include=FALSE}
test_feeding_data <- left_join(test_data, feeding, by = "id")
test_feeding_data <- test_feeding_data %>% replace_na(list(n = 0))
test_feeding_data <- rename(test_feeding_data, feeding.count = n)
retest_feeding_data <- test_feeding_data %>% filter(trial == "21")
```
```{r, echo = TRUE}
test_data_feeding_controlled_model <-
glmmTMB(rewarding.object.preference ~
trial * rewarding.object.colour + feeding.count + (1 | id) +
diag(0 + rewarding.object.colour * trial | id),
data = test_feeding_data,
family = gaussian
)
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName7"> See ESM Model 1 Residuals </button>
<div id="BlockName7" class="collapse">
```{r, message=FALSE}
simulationOutput <- simulateResiduals(fittedModel = test_data_feeding_controlled_model)
plot(simulationOutput)
# Saving plot to figs directory
ggsave(
filename = "exp-1-ESM-model-1-residual-plot.png",
plot = (plot(simulationOutput)),
path = "figs/exp-1/residual-plots/",
device = "png",
dpi = 300
)
```
</div>
</br>
```{r, echo=FALSE, results=FALSE}
# Setting table row names
test_data_feeding_controlled_model_table_row_name_vec <- c(
"Intercept",
"Trial",
"Rewarding object colour",
"Feeding count",
"Rewarding object colour X Trial"
)
tidy_fit_test_data_feeding_controlled_model <- broom.mixed::tidy(test_data_feeding_controlled_model)
# formatted p value
tidy_fit_test_data_feeding_controlled_model$p.value <- format_p_value(tidy_fit_test_data_feeding_controlled_model$p.value)
# Changing tibble header names
tidy_fit_test_data_feeding_controlled_model <- rename_tidy_lme4_cols(tidy_fit_test_data_feeding_controlled_model)
tidy_fit_test_data_feeding_controlled_model[1:5, 4] <- test_data_feeding_controlled_model_table_row_name_vec
```
##### Results
```{r feeding-controlled-table, results=TRUE, echo=FALSE}
knitr::kable(tidy_fit_test_data_feeding_controlled_model[1:5, ] %>%
dplyr::select(-group, -effect, -component) %>%
mutate_if(is.numeric, round, digits = 3),
caption = "Summary table for a modification of Model 3. This model is the same
as that described in Table 1 except it includes feeding count as a covariate.
Estimates ± standard error (SE) of the effects of trial and rewarding object
colour of the rewarding object colour from the generalized linear mixed effect
model containing the effects (Trial, Rewarding object colour, and their
interaction effect)."
)
```
The main results do not change if we control for feeding count. Table
\@ref(tab:feeding-controlled-table) is the output for the feeding controlled