-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport2.Rmd
346 lines (274 loc) · 15.6 KB
/
report2.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
---
title: "Spiders vs Wasps"
author: "Marcela Alfaro Córdoba"
date: "12/16/2019"
output:
md_document:
variant: markdown_github
toc: true
toc_depth: 3
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(tidyverse)
require(foreign)
require(nnet)
require(ggplot2)
require(reshape2)
library(gridExtra)
library(extrafont)
library(grDevices)
devtools::install_github('haleyjeppson/ggmosaic')
library(ggmosaic)
theme_set(theme_bw(base_size=12, base_family='Arial'))
```
# Experiments of spiders with live prey
## Methods
A total of 136 trials with three groups of spiders (68 field-juveniles, 51 field-adults and 17 captive-adults) and live prey were carried out during which the following data were recorded: spider type (juvenile, adult, reared), wasp color (BOB or black), spider behavior (behavioral scale explained before), time when the spider reacted (0-5, 5-10, 10-20, 20-30, 30-40 minutes), the distance between the spider and the prey during each behavior, presence/absence of a silk dragline, and wasp and spider body length. For each of the 136 trials consisting of a wasp with a spider, the size of each was measured (S3 Fig.). Although there is size variation within each group, predator (spiders) and prey (wasps) fall within the same general range, with field-captured adult spiders being the largest.
A multinomial logistic regression [26] was fitted to a simplified response: the most common activity for each spider was classified as “detect”, “attack” or “avoid”, in order to correct for zero or near zero counts in each response category. The full model includes the following covariates: wasp color, spider type, wasp size, spider size, wasp genus and presence/absence of silk dragline. This model was compared with simplified versions, with the result being that the model with wasp color and spider type was the model with the best fit, according to the AIC statistic. Additionally, frequency of actions for each group of spiders were calculated according to 10 minute slots, in order to define a clear timeline of spider behavior in the controlled experiments.
A separate experiment with lures in the automated cage included 30 trials, during which the following variables were recorded: presence/absence of silk dragline, spider size, background experimental arena color (black or white) and color of false prey (BOB vs black) that first attracted the spider.
The response variable in this case was constructed by registering whether L. jemineus responded in the same way to black and BOB lures coded as 1, and whether L. jemineus responded in a different way to black and BOB lures coded as 2. Contingency tables were calculated, along with a Chi Square independence test for the response versus each of the variables: presence/absence of silk dragline, background color and prey color that first attracted the spider.
## Results
For each of the 136 trials consisting of a wasp with a spider, the size of each was measured (Fig. 3 A, B). Although there is size variation within each group, predator (spiders) and prey (wasps) fall within the same general range, with field-captured adult spiders being the largest.
```{r}
source(file="read_data.R")
datos <- datos %>%
mutate(s_type=recode_factor(s_type,
"Field Juvenile"="Field-Juvenile",
"Field Adult"="Field-Adult",
"Captivity Adult"="Captive Adult"))
resumen <- datos %>% group_by(ID) %>%
summarise(type=(unique(type)),
typegr=(unique(typegr)),
w_color=(unique(w_color)),
w_size=(unique(w_size)),
s_type=(unique(s_type)),
s_size=(unique(s_size)),
drag=(unique(drag))) %>% arrange(type, w_color, s_type)
ftable(table(resumen$w_color,resumen$type))
ftable(table(resumen$w_color,resumen$typegr))
ftable(table(resumen$w_color,resumen$s_type))
```
```{r fig.width=6}
bp1 <- ggplot(resumen, aes(x=w_color, y=w_size)) +
geom_boxplot()+ facet_wrap(~type, ncol=4)+theme(legend.position = "none")+
# dejar la misma escala de spider size
theme(strip.text = element_text(face = "italic"))+
labs(title="A",x="Wasp Color", y = "Wasp Size (mm)") +
ylim(c(min(resumen$w_size),max(resumen$s_size)))
bp2 <- ggplot(resumen, aes(x=s_type, y=s_size)) +
geom_boxplot()+labs(title="B",x="Type", y = "Spider Size (mm)") +
ylim(c(min(resumen$w_size),max(resumen$s_size)))
grid.arrange(bp1, bp2, nrow = 1, widths=2:1)
```
### Association between sizes (not included in the paper):
```{r}
bp <- ggplot(resumen, aes(x=w_size, y=s_size, color=s_type)) + geom_point()+ geom_smooth(method='lm', aes(color=s_type))+labs(color = "Spider Type")+
labs(title="",x="Wasp Size (mm)", y = "Spider Size (mm)")
bp
summary(lm(s_size~w_size*s_type, data=resumen))
```
```{r}
## Wrangle data:
datos <- datos %>%
mutate(h_resp3=recode_factor(h_resp2,
"3.Crouch, jump and contact prey"="Attack",
"4.Pierce and ingestion"="Attack",
"1.Prey alert and swivel"="Detect",
"2.Follow or stalk"="Detect",
"5.Undetected or ignored"="Avoid",
"6.Detect visually and withdraw"="Avoid",
"7.None"="Avoid"))
d2<-datos %>% group_by(ID) %>% count(h_resp3) %>% top_n(1)
rep <- d2 %>% group_by(ID) %>% filter(n()>1) %>%
summarize(h_resp3=unique(h_resp3)[1], n=n())
datos2<-bind_rows(rep,d2[!d2$ID%in%rep$ID,]) %>% arrange(ID)
datos_model <- inner_join(datos2, resumen, by="ID")
with(datos_model, ftable(h_resp3,w_color, s_type))
with(datos_model, ftable(h_resp3,w_color, type))
```
### Multinomial Model
The multinomial logistic regression was fitted to a simplified response, where the most common activity for each spider was classified as “detect”, “attack” or “avoid”. A summary of the data included in the final model is presented in Figure 4. Note that in this figure the number of spiders differs between groups, which is why the bar for lab-reared (captive) adults is thinner than the ones for field adults and field juveniles. The full model includes the following covariates: wasp color, spider type, wasp size, spider size, wasp genus and presence/absence of silk dragline. This model was compared with simplified versions, with the result being that the model with presence/absence of silk dragline and spider type was the model with the best fit (AIC = 212.09).
```{r results="hide"}
datos_model$h_resp3 <- relevel(as.factor(datos_model$h_resp3),
ref = "Attack")
test00 <- multinom(h_resp3 ~ drag+s_type, data = datos_model)
test01 <- multinom(h_resp3 ~ s_type, data = datos_model)
test02 <- multinom(h_resp3 ~ s_type + w_color + type, data = datos_model)
test03 <- multinom(h_resp3 ~ s_type*w_color*type, data = datos_model)
test04 <- multinom(h_resp3 ~ s_type+w_color+s_size+w_size+drag+type, data = datos_model)
test05 <- multinom(h_resp3 ~ 1, data = datos_model)
```
```{r}
c(test00$AIC,test01$AIC,test02$AIC,
test03$AIC,test04$AIC,test05$AIC)
test <- test00
summary(test)
z <- summary(test)$coefficients/summary(test)$standard.errors
p <- (1 - pnorm(abs(z), 0, 1)) * 2
p
exp(summary(test)$coefficients)
```
According to the aforementioned model, being a field adult is a significant factor for increasing the odds of detecting versus attacking (pvalue = 0.0108), and for avoiding versus attacking (pvalue = 0.0189). This shows that field adults are more likely to detect or avoid than to attack, compared to adults reared in captivity which appear to prefer attacking. Similarly, not using silk is associated with an increased chance of detecting versus attacking (pvalue = 0.0160), which makes sense since these spiders use silk only when attacking. Being a field-caught juvenile versus an adult reared in captivity did not make a difference in the most common spider actions. Other factors such as wasp color, wasp size, spider size, and wasp genus did not make a statistically significant difference when included in the full model. It should however be noted that field-caught adults only attacked black wasps, and adults reared in captivity avoided only BOB wasps.
```{r}
datos_model <- datos_model %>%
mutate(h_resp3=recode_factor(h_resp3, "Detect"="Detect","Attack"="Attack","Avoid"="Avoid"))
ggplot(data = datos_model) +
geom_mosaic(aes(x = product(h_resp3, s_type),
fill=w_color),
show.legend=TRUE,
na.rm=TRUE,
divider=mosaic("v")) +
theme(legend.position = "none") +
scale_fill_manual(values=c("#999999", "#E69F00")) +
labs(x = "Action", title='', y="")
```
```{r}
# ggplot(data = datos_model) +
# geom_mosaic(aes(x = product(h_resp3, s_type),
# fill=w_color),
# show.legend=TRUE,
# na.rm=TRUE,
# divider=mosaic("v")) +
# theme(legend.position = "none") +
# scale_fill_manual(values=c("#999999", "#E69F00")) +
# labs(x = "Action", title='', y="")
ggplot(data=datos_model, aes(x=h_resp3, y=..count.., fill=w_color)) +
geom_bar(position="dodge") +
facet_wrap(~s_type, ncol=5)+
scale_fill_manual(name = "Wasp Color", values=c("#999999", "#E69F00")) +
labs(x = "Most Common Action per Trial n = 136", title='', y="Count")+
#theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
with(datos_model, ftable(h_resp3,w_color, s_type))
```
### Timelines (original version)
```{r fig.height=15, fig.width=8}
options <- sort(unique(datos$h_resp3))
datos <- datos %>% mutate(time3= recode_factor(time2,
"0-5 min" = "0-10 min",
"5-10 min" = "0-10 min",
"10-20 min" = "10-20 min",
"20-30 min" = "20-30 min",
"30-40 min" = "30-40 min"))
dat<-datos[datos$h_resp3=="Detect",]
ta1<- ggplot(data = dat) +
geom_mosaic(data=dat,aes(x = product(w_color, time3),
fill=w_color,
conds=product(s_type)),
na.rm=TRUE, divider=mosaic("v")) +
scale_fill_manual(values=c("#999999", "#E69F00")) +
labs(x = "Time", title='A', y="")+
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
dat<-datos[datos$h_resp3=="Attack",]
ta2<- ggplot(data = dat) +
geom_mosaic(data=dat,aes(x = product(w_color, time3),
fill=w_color,
conds=product(s_type)),
na.rm=TRUE, divider=mosaic("v")) +
scale_fill_manual(values=c("#999999", "#E69F00")) +
labs(x = "Time", title='B', y="")+
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
dat<-datos[datos$h_resp3=="Avoid",]
ta3<- ggplot(data = dat) +
geom_mosaic(data=dat,aes(x = product(w_color, time3),
fill=w_color,
conds=product(s_type)),
na.rm=TRUE, divider=mosaic("v")) +
scale_fill_manual(values=c("#999999", "#E69F00")) +
labs(x = "Time", title='C', y="")+
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
grid.arrange(ta1, ta2, ta3, nrow = 3)
```
### Timelines (bar plots option)
```{r fig.height=15, fig.width=8}
options <- sort(unique(datos$h_resp3))
dat<-datos[datos$h_resp3=="Detect",] %>%
group_by(s_type, w_color, time3) %>% summarise(n=n())
t1<- ggplot(data=dat, aes(x=time3, y=n, fill=w_color)) +
geom_bar(stat="identity", position="dodge") + ylim(c(0,80))+
facet_wrap(~s_type, ncol=5)+
scale_fill_manual(name = "Wasp Color", values=c("#999999", "#E69F00")) +
labs(x = "Time", title='A: Detect ', y="Count of individual actions")+
#theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
dat<-datos[datos$h_resp3=="Attack",] %>%
group_by(s_type, w_color, time3) %>% summarise(n=n())
t2<- ggplot(data=dat, aes(x=time3, y=n, fill=w_color)) +
geom_bar(stat="identity", position="dodge") + ylim(c(0,80))+
facet_wrap(~s_type, ncol=5)+
scale_fill_manual(name = "Wasp Color", values=c("#999999", "#E69F00")) +
labs(x = "Time", title='B: Attack', y="Count of individual actions")+
#theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
dat<-datos[datos$h_resp3=="Avoid",] %>%
group_by(s_type, w_color, time3) %>% summarise(n=n())
t3<- ggplot(data=dat, aes(x=time3, y=n, fill=w_color)) +
geom_bar(stat="identity", position="dodge") + ylim(c(0,80))+
facet_wrap(~s_type, ncol=5)+
scale_fill_manual(name = "Wasp Color",values=c("#999999", "#E69F00")) +
labs(x = "Time", title='C: Avoid', y="Count of individual actions")+
# theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
grid.arrange(t1, t2, t3, nrow = 3)
```
### Re-coding of BOB for Table 1
```{r}
# Re-code BOB and Black:
datos_model$w_color2<-if_else(datos_model$w_color=="Black","Black",
if_else(datos_model$typegr =="Macroteleia"&datos_model$w_color=="BOB","BOB","PseudoBOB"))
datos_model <- datos_model %>%
mutate(w_color2=recode_factor(w_color2, "Black"="Black",
"PseudoBOB"="PseudoBOB",
"BOB"="BOB"))
with(datos_model, ftable(type,w_color2,h_resp3))
ftable(table(datos_model$w_color2,datos_model$h_resp3))
ftable(table(datos_model$h_resp3,
datos_model$s_type,
datos_model$w_color))
datos_model <- datos_model %>%
mutate(h_resp3=recode_factor(h_resp3, "Detect"="Detect",
"Attack"="Attack",
"Avoid"="Avoid"))
```
# Experiment of spiders with false prey in automated cage
## Methods
The experiment with false prey (lures) in the automated cage included 30 trials, during which the following variables were recorded: presence/absence of silk dragline, spider size, background experimental arena color (black or white) and color of false prey (BOB vs black) that first attracted the spider.
The response variable in this case was constructed following the algorithm:
1. L. jemineus responded similarly to black and BOB lures = 1
2. L. jemineus responded differently to black and BOB lures = 2
Contingency tables were calculated, along with a Chi Square independence test for the response versus each of the variables: presence/absence of silk dragline, background color and prey color that first attracted the spider.
## Results
Table 1 presents the results from the experiment with false prey. Each contingency table was tested for independence, and the results were that there is no evidence of dependence in any of the cases. In other words, spider behaviors are not associated with background color, the color of lure that was detected first, the use or non-use of silk, or predator size.
```{r}
library(DescTools)
names(datos_cont)
tab1<-table(datos_cont$resp,
datos_cont$backgr)
GTest(tab1)
prop.table(tab1)
tab2<-table(datos_cont$resp,
datos_cont$first)
GTest(tab2)
prop.table(tab2)
tab3<-table(datos_cont$resp,
datos_cont$silk)
GTest(tab3)
prop.table(tab3)
```
Statistical analysis was performed using the computing environment R (R Core Team, 2019). Data formatting and figures were prepared using the Tidyverse packages (Wickham, 2017). Multinomial logistic regression was done using the nnet package (Venables et al, 2002).
# Colophon
This report was generated on `r Sys.time()` using the following computational environment and dependencies:
```{r colophon, cache = FALSE}
# which R packages and versions?
if ("devtools" %in% installed.packages()) devtools::session_info()
```
The current Git commit details are:
```{r}
# what commit is this file at?
if ("git2r" %in% installed.packages() & git2r::in_repository(path = ".")) git2r::repository(here::here())
```