-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 15.5 KB
/
.Rhistory
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
e1.ann3 %>% filter(pp==11, rr=="dispersed", st=="early", tb==0)
e1.ann3 %>% filter(pp==11, rr=="dispersed", st=="early", tb==1)
e1.ann3 %>% filter(pp==11, rr=="dispersed", st=="early", tb==2)
e1.ann3 %>% filter(pp==11, rr=="dispersed", st=="early", tb==3)
summary(dd)
nrow(dd)
e1.raw <- readRDS("001-00-e1-data.RDS")
e1.sub <- e1.raw %>% select(pp, rr, tb, ix, tl, fl, fr)
e1.ann <- e1.sub %>%
# remove samples that did not look at a tree
filter(fl>0) %>% select(-fl) %>%
# identify consecutive dupes for removal
mutate(dupe = !(is.na(tl != lag(tl)) | tl != lag(tl))) %>%
# remove dupes
filter(dupe==FALSE) %>%
# annotate with revisit
group_by(pp, rr, tb) %>% mutate(isrv = duplicated(tl)) %>% ungroup() %>%
# annotate with max fruit
group_by(pp, rr, tb) %>% mutate(maxf = max(fr)) %>% ungroup() %>%
# remove samples in the tenth fruit
filter(fr !=10)
# make sure each trial has an entry for the zeroth fruit
merge_me = expand_grid(pp = sort(unique(e1.ann$pp)), rr = sort(unique(e1.ann$rr)), tb = sort(unique(e1.ann$tb)), fr = 0)
e1.ann2 <- full_join(e1.ann, merge_me) %>% arrange(pp, rr, tb, fr)
# redo isrv such that the zeroth fruit has FALSE for isrv instead of NA
e1.ann2 <- e1.ann2 %>% replace_na(list(isrv=FALSE))
# redo maxf to replace NA with whatever the max was for that trial
e1.ann2 <- e1.ann2 %>% group_by(pp,rr,tb) %>% mutate(maxf=max(maxf, na.rm=TRUE)) %>% ungroup()
# remove trials with maxf less than 10
e1.ann3 <- e1.ann2 %>% filter(maxf == 10)
# add stage
e1.ann3 <- e1.ann3 %>% mutate(st=ifelse(tb<=5, "early", "late"))
# aggregate such that there is a row for each fruit visited (including the zeroth but not the tenth)
# saying how many (sum) revisits there were while searching for that fruit
e1.agg1 <- e1.ann3 %>%
group_by(pp, rr, st, tb, fr) %>%
summarise(nrev=sum(isrv)) %>%
ungroup()
# aggregate over trials to give mean sum revisits at the relevant fruit for each stage
e1.agg2 <- e1.agg1 %>%
group_by(pp, rr, st, fr) %>%
summarise(murev=mean(nrev)) %>%
ungroup()
dd = e1.agg2
dd <- dd %>% mutate(pp=as_factor(pp), rr=as_factor(rr), st=as_factor(st), fr=as_factor(fr))
# anova
library(ez)
options(contrasts=c("contr.sum","contr.poly"))
dda = ezANOVA(data=dd, dv=murev, wid=pp, within=c(rr, st, fr), type=3)
knitr::kable(dda$ANOVA)
knitr::kable(dda$`Sphericity Corrections`)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(fig.path='e1_figures/')
knitr::opts_chunk$set(fig.width=7, fig.height=5)
options(dplyr.summarise.inform=F)
library(tidyverse)
library(ez)
library(gt)
e1 <- readRDS("001-00-e1-data.RDS")
e1_revisits_df <-
e1 %>%
transmute(
pp = as_factor(pp),
trial = as.numeric(tb), # ranges 1 to 10
resources = rr,
stage = ifelse(trial<=5,"early","late"),
basket = fr,
sample = ix,
tree = tl)
e1_revisits_df <- e1_revisits_df %>%
mutate(revisit = as.numeric(duplicated(tree))) %>%
mutate(
# "is_a" to remind us that the raw data are binary yes/no
# for whether the current visit is a revisit or not
is_a_revisit = as.logical(revisit)
) %>%
# number of revisits can't be greater than zero for the 10th fruit
# because we stop recording data as soon as they get to 10
# so we remove those values because they must have no variance
# and it would distort the analysis to treat those zeros as meaningful.
filter(
basket < 10
) %>%
# basket+1 because the data were recorded such that this
# tally is zero until they get a fruit but we want to say
# that they had a certain number of revisits while they
# were searching for the first fruit, not while they
# had zero fruit in their basket
mutate(
basket = basket +1
) %>%
# set factors
mutate(
trial = as_factor(trial),
stage = as_factor(stage),
tree = as_factor(tree),
basket = as_factor(basket),
resources = as_factor(resources)
)
gt(subset(e1_revisits_df, pp==1&trial==1&resources=="patchy"))
e1_revisits_df_agg1 <-
e1_revisits_df %>%
group_by(pp,trial,stage,resources,basket) %>%
summarise(sum_of_revisits = sum(is_a_revisit))
e1_revisits_df_agg2 <-
expand.grid(
basket=1:10,
trial=c(1:10,1:10),
pp=unique(e1_revisits_df_agg1$pp)
) %>% # is 8400
mutate(
stage=ifelse(trial<=5,"early","late")
) %>%
bind_cols(
resources=as_factor(rep(c("dispersed","patchy"),each=10,times=420))
) %>%
select(
pp,trial,stage,resources,basket
) %>%
mutate(
trial=as_factor(trial),
stage=as_factor(stage),
resources=as_factor(resources),
basket=as_factor(basket)
) %>%
as_tibble() %>%
left_join(
y = e1_revisits_df_agg1,
by = c("pp", "trial", "stage", "resources", "basket")
) %>%
replace_na(list(sum_of_revisits=0))
e1_nrevisits_grand_PARTICIPANT_MEANS <-
e1_revisits_df_agg2 %>%
group_by(pp, resources, stage, basket) %>%
summarise(meanrevisits=mean(sum_of_revisits))
options(contrasts=c("contr.sum","contr.poly"))
e1_nrevisits_grand_ANOVA<-
ezANOVA(data=e1_nrevisits_grand_PARTICIPANT_MEANS,
dv=meanrevisits,
wid=pp,
within=c(resources,stage,basket),
type=3)
e1_nrevisits_grand_ANOVA_TABLE <-
e1_nrevisits_grand_ANOVA$ANOVA %>%
select(-ges) %>%
gt() %>%
tab_header(
title="Number of revisits",
subtitle = "ANOVA table"
) %>%
fmt_number(
columns = c("F"),
rows=everything(),
decimals=2
) %>%
fmt_number(
columns = c("p"),
rows=everything(),
decimals=3
) %>%
cols_align(
columns=`p<.05`,
align="center"
)
gtsave(e1_nrevisits_grand_ANOVA_TABLE, "e1_tables/e1_nrevisits_grand_ANOVA.png")
e1_nrevisits_grand_ANOVA_TABLE
e1_nrevisits_grand_ANOVA_TABLE
rm(list=ls())
e1.raw <- readRDS("001-00-e1-data.RDS")
e1.sub <- e1.raw %>% select(pp, rr, tb, ix, tl, fl, fr)
e1.ann <- e1.sub %>%
# remove samples that did not look at a tree
filter(fl>0) %>% select(-fl) %>%
# identify consecutive dupes for removal
mutate(dupe = !(is.na(tl != lag(tl)) | tl != lag(tl))) %>%
# remove dupes
filter(dupe==FALSE) %>%
# annotate with revisit
group_by(pp, rr, tb) %>% mutate(isrv = duplicated(tl)) %>% ungroup() %>%
# annotate with max fruit
group_by(pp, rr, tb) %>% mutate(maxf = max(fr)) %>% ungroup() %>%
# remove samples in the tenth fruit
filter(fr !=10)
# make sure each trial has an entry for the zeroth fruit
merge_me = expand_grid(pp = sort(unique(e1.ann$pp)), rr = sort(unique(e1.ann$rr)), tb = sort(unique(e1.ann$tb)), fr = 0)
e1.ann2 <- full_join(e1.ann, merge_me) %>% arrange(pp, rr, tb, fr)
# redo isrv such that the zeroth fruit has FALSE for isrv instead of NA
e1.ann2 <- e1.ann2 %>% replace_na(list(isrv=FALSE))
# redo maxf to replace NA with whatever the max was for that trial
e1.ann2 <- e1.ann2 %>% group_by(pp,rr,tb) %>% mutate(maxf=max(maxf, na.rm=TRUE)) %>% ungroup()
# remove trials with maxf less than 10
e1.ann3 <- e1.ann2 %>% filter(maxf == 10)
# add stage
e1.ann3 <- e1.ann3 %>% mutate(st=ifelse(tb<=5, "early", "late"))
# aggregate such that there is a row for each fruit visited (including the zeroth but not the tenth)
# saying how many (sum) revisits there were while searching for that fruit
e1.agg1 <- e1.ann3 %>%
group_by(pp, rr, st, tb, fr) %>%
summarise(nrev=sum(isrv)) %>%
ungroup()
# aggregate over trials to give mean sum revisits at the relevant fruit for each stage
e1.agg2 <- e1.agg1 %>%
group_by(pp, rr, st, fr) %>%
summarise(murev=mean(nrev)) %>%
ungroup()
dd = e1.agg2
dd <- dd %>% mutate(pp=as_factor(pp), rr=as_factor(rr), st=as_factor(st), fr=as_factor(fr))
options(contrasts=c("contr.sum","contr.poly"))
e1_nrevisits_grand_ANOVA <-
ezANOVA(data=dd, dv=murev, wid=pp, within=c(rr, st, fr), type=3)
e1_nrevisits_grand_ANOVA_TABLE <-
e1_nrevisits_grand_ANOVA$ANOVA %>%
select(-ges) %>%
gt() %>%
tab_header(
title="Number of revisits",
subtitle = "ANOVA table"
) %>%
fmt_number(
columns = c("F"),
rows=everything(),
decimals=2
) %>%
fmt_number(
columns = c("p"),
rows=everything(),
decimals=3
) %>%
cols_align(
columns=`p<.05`,
align="center"
)
gtsave(e1_nrevisits_grand_ANOVA_TABLE, "e1_tables/e1_nrevisits_grand_ANOVA.png")
e1_nrevisits_grand_ANOVA_TABLE
knitr::include_graphics("e1_tables/e1_nrevisits_grand_ANOVA.png")
dd
View(dd)
# we want one value from each participant for early and one value from each participant for late, = stage means for the body text
stagemeansPerParticipant <-
e1_revisits_df %>%
ungroup() %>%
select(pp, trial, stage, is_a_revisit) %>%
# get count for each trial
group_by(pp, trial, stage) %>%
summarise(count_revisits_for_trial=sum(is_a_revisit)) %>%
# aggregate trial counts to get a single value for each stage from each participant
group_by(pp,stage) %>%
summarise(mean_revisits=mean(count_revisits_for_trial))
View(e1.ann3)
e1.ann3
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb) %>%
summarise(count=sum(isrv))
stage_means
View(stage_means)
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb) %>%
summarise(count=mean(isrv)) %>%
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb) %>%
summarise(mu=mean(isrv)) %>%
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb) %>%
summarise(mu=mean(isrv))
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb) %>%
summarise(mu=mean(isrv))
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb, fr) %>%
summarise(mu=mean(isrv))
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb, fr) %>%
summarise(mu=mean(isrv)) %>%
group_by(st, pp) %>%
sumamrise(mu=mean(mu))
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb, fr) %>%
summarise(mu=mean(isrv)) %>%
group_by(st, pp) %>%
summarise(mu=mean(mu))
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb, fr) %>%
summarise(mu=mean(isrv)) %>%
group_by(st, pp) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu))
# stage means
stage_means <- e1.ann3 %>%
group_by(pp, st, tb, fr) %>%
summarise(count=sum(isrv)) %>%
group_by(st, pp) %>%
summarise(mu=mean(count)) %>%
summarise(mu=mean(mu))
e1.agg2
View(e1.agg2)
stage_means = e1.agg2 %>%
group_by(pp,st) %>%
mu=mean(murev)
stage_means = e1.agg2 %>%
group_by(pp,st) %>%
summarise(mu=mean(murev))
stage_means = dd %>%
group_by(pp,st) %>%
summarise(mu=mean(murev))
stage_means = dd %>%
group_by(st, pp) %>%
summarise(mu=mean(murev))
stage_means = dd %>%
group_by(st, pp) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu))
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu))
stage_means
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu))
stage_means
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu))
stage_means
dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu))
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu), sd=sd(mu))
stage_means
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(muu=mean(mu), sd=sd(muu))
stage_means
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(muu=mean(mu), sd=sd(mu))
stage_means
fruit_means = dd %>%
group_by(fr, pp, rr, tb) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(muu=mean(mu), sd=sd(mu))
dd
fruit_means = dd %>%
group_by(fr, pp, rr, st) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(muu=mean(mu), sd=sd(mu))
fruit_means
fruit_meansfilter(fr==0)
fruit_means %>% filter(fr==0)
stage_means
stage_means %>% kable()
stage_means %>% kable(digits=2)
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu))
stage_means %>% kable(digits=2)
stage_means
round(stage_means,2)
print(stage_means)
print(stage_means, digits=2)
summarise(muu=round(mean(mu),2), sd=round(sd(mu),2)
stage_means = dd %>%
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu))
stage_means %>% mutate(mean=round(mean,2))
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu))
stage_means %>% mutate(mean=round(mean,2))
stage_means %>% mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means %>% mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means = dd %>%
group_by(fr, pp, rr, st) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(muu=mean(mu), sd=sd(mu))
fruit_means = dd %>%
group_by(fr, pp, rr, st) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu))
fruit_means %>% mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means = dd %>%
group_by(fr, pp, rr, st) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu)) %>%
mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means
fruit_means %>% filter(fr==0)
fruit_means %>% filter(fr==9)
fruit_means %>% filter(fr==0)
fruit_means %>% filter(fr==9)
fruit_means %>% filter(fr%in%c(0,9))
fruit_means = dd %>%
group_by(fr, pp, rr, st) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu)) %>%
mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu))
stage_means %>% mutate(mean=round(mean,2), sd=round(sd,2))
stage_means = dd %>%
group_by(st, pp, rr, fr) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu)) %>%
mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means = dd %>%
group_by(fr, pp, rr, st) %>%
summarise(mu=mean(murev)) %>%
summarise(mu=mean(mu)) %>%
summarise(mu=mean(mu)) %>%
summarise(mean=mean(mu), sd=sd(mu)) %>%
mutate(mean=round(mean,2), sd=round(sd,2))
fruit_means
fruit_means[1,2]
fruit_means[1,3]
`r fruit_means[1,2]` +/- `r fruit_means[1,3]`.
dd
data_for_plot <-
dd %>% group_by(pp, rr, st) %>%
summarise(mean_revisits=mean(murev))
data_for_plot
data_for_plot <-
dd %>% group_by(pp, rr, st) %>%
summarise(mean_revisits=mean(murev))
pd <- position_dodge(0.1)
e1_nrevisits_grand_PLOT2 <-
ggplot(data=data_for_plot, aes(x=st, y=mean_revisits, group=rr, pch=rr, fill=rr))+
theme_bw()+
theme(aspect.ratio = 1, panel.grid=element_blank(), axis.title.y=element_text(angle=0,vjust=0.5,hjust=0.5))+
scale_fill_manual(name="Resource\ndistribution",values=c("white", "black")) +
scale_shape_manual(name="Resource\ndistribution",values=c(24,19)) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=0.1, position=pd) +
stat_summary(fun = mean, geom = "line", position=pd) +
stat_summary(fun = mean, geom = "point", size=4, position=pd)+
ggtitle("Number of Revisits")+
ylab("Number\nof\nRevisits")+
xlab("Trial")+
scale_x_discrete(labels=c("early\ntrials 1 to 5", "late\ntrials 6 to 10"))#+labs(caption="Significant stage effect, no interaction effect")
e1_nrevisits_grand_PLOT2
data_for_plot
data_for_plot <-
dd %>% group_by(rr, st, pp) %>%
summarise(mean_revisits=mean(murev))
data_for_plot
data_for_plot %>% summarise(m=mean(mean_revisits))
data_for_plot %>% summarise(m=mean(mean_revisits)) %>% summarise(m=mean(m))
data_for_plot <-
dd %>% group_by(st, rr, pp) %>%
summarise(mean_revisits=mean(murev))
data_for_plot %>% summarise(m=mean(mean_revisits)) %>% summarise(m=mean(m))
e1_nrevisits_grand_PLOT
source("~/.active-rstudio-document", echo=TRUE)
data_for_plot
View(data_for_plot)
source("~/.active-rstudio-document", echo=TRUE)
source("~/.active-rstudio-document", echo=TRUE)
e1_nrevisits_grand_ANOVA