-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyses.Rmd
1036 lines (856 loc) · 36.2 KB
/
analyses.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: "Adapting reintroduction tactics in successive trials increases the likelihood of establishment for an endangered carnivore in a fenced sanctuary"
author: "Wilson B A, Evans M J, Batson W G, Banks S C, Gordon I J, Fletcher D B, Wimpenny C, Newport J, Belton E, Rypalski A, Portas T & Manning A D"
date: "2 August 2023"
output:
html_document:
toc: true
number_sections: true
toc_depth: 3
toc_float:
collapsed: true
theme: cerulean
highlight: pygments
editor_options:
chunk_output_type: console
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), 'tutorial.html')) })
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, eval=FALSE)
```
# **Data preparation**
First, we installed the [pacman Package Management Tool](https://cran.r-project.org/web/packages/pacman/index.html), which allows us to install and load subsequent packages in a condensed and efficient way.
```{r, eval=FALSE}
#install.packages("pacman")
```
```{r, results='hide', warning=FALSE, message=FALSE}
# Install and load required packages
pacman::p_load(adehabitatLT, brglm, boot, effects, ggmap, ggplot2,
ggpubr, janitor, lme4, lsmeans, maptools, multcomp,
MuMIn, plyr, readr, readxl, rgdal, rstudioapi, sp,
tidyverse, viridis)
```
We also set the working directly to where this R markdown is saved using the `rstudioapi` package.
```{r}
# Set the working directory to where this markdown is saved
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
```
# **Calculating analysis**
```{r, results='hide', warning=FALSE, message=FALSE}
# Assign raw data filename to an object
raw_data <- "data.xlsx"
# Read in den location data
den <- read_excel(raw_data, sheet="den locations") %>%
clean_names() %>%
rename(easting=x, northing=y)
# Read in and project MFWS fence shapefile using rgdal
mfws <- readOGR(dsn="shapefiles/mfws_fence.shp", verbose=FALSE) %>%
spTransform(CRS("+proj=utm +zone=55 +ellps=WGS84")) %>%
# Transform shapefile into a dataframe
fortify(verbose=FALSE) %>%
mutate(lat=as.numeric(lat + 10000000),
long=as.numeric(long))
# Plot den locations on MFWS map for each trial
ggplot() +
geom_path(mfgo, mapping=aes(x=long, y=lat, group=group), col="grey15") +
geom_jitter(den, mapping=aes(x=easting, y=northing,
col=factor(year_of_acquisition)), alpha=0.2) +
coord_sf(xlim=c(695750, 699250), ylim=c(6104250, 6107750)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill="white"),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
legend.key = element_blank(),
strip.background = element_blank()) +
facet_wrap(~factor(year_of_acquisition), ncol=3) +
scale_colour_manual(values = viridis(3, begin=0.9, end=0.1), name="Fence") +
xlab("") + ylab("") + labs(col="Trial")
```
## Cluster dens
1. Create clusters for dens within 10 m of one another
```{r}
den <- den %>%
# Remove some founders
subset(bank_id!="EQ15" & bank_id!="EQ16" & bank_id!="EQ17" &
# Remove duplicated rows
den_id!="119" & den_id!="1682" &
# Select only animals that survived the establishment period
history=="Alive" &
# Remove rows with no valid coordinates
easting!="NA" &
# Select only rows within 42 days of release
days_post_release<43)
# Create an xy dataframe
xy <- data.frame(cbind(den$easting, den$northing))
# Create a distance matrix
distxy <- dist(xy)
# Hierarchically cluster the distance matrix
chc <- hclust(distxy)
# Calculate distance between dens with a 10 m threshold
chc.d10 <- cutree(chc, h=12)
# Append cluster labels to the den df
den$cluster <- chc.d10
```
2. Calculate mean eastings and northings for each cluster
```{r}
meanclusters <- plyr::ddply(den, .(cluster), summarize,
x2=mean(easting), y2=mean(northing))
vlookup <- merge(den, meanclusters, by= "cluster" )
# Sort df by den_id
vlookup <- arrange(vlookup, den_id)
# Sort df by den_id
den <- arrange(den, den_id)
den$mean_x <- vlookup$x2
den$mean_y <- vlookup$y2
```
## Distance per day
Calculate distances travelled for the first quoll, "EQ01".
```{r}
# Subset to consecutive days only
den <- subset(den, days_between==1)
# Remove irrelevant animal
eq <- subset(den, bank_id=="EQ01")
# Convert column to factor
eq$bank_id <- factor(eq$bank_id)
# Convert date to a form R can interact with
pdate <- as.POSIXct(strptime(as.character(eq$posi_xdate), "%Y.%m.%d "))
data_xy=eq[c("mean_x", "mean_y")]
# Create a Spatial Points class for all locations
xysp <- SpatialPoints(data_xy)
proj4string(xysp) <- CRS("+proj=utm +zone=55 +ellps=WGS84")
# Create a spatial df of coordinates
sppt <- data.frame(xysp)
# Create a spatial df of identities
idsp <- data.frame(eq$bank_id)
# Merge identities and dates into same spatial df
merge <- data.frame(idsp)
# Merge identities and date to coordinates
coordinates(merge) <- sppt
# Create an object of class ltraj to store movements
move <- as.ltraj(xy=eq[,c("mean_x", "mean_y")], date=pdate, id=idsp)
# Plot movements and save to jpeg file
jpeg(file="EQ01_plot.jpeg", width=5500, height=5000, units="px", res=800)
# Plot movements
plot(move, xlim=c(695750, 699250), ylim=c(6104250, 6107750),
xlab="Easting", ylab="Northing", main="EQ01")
plot(mfws, add=TRUE)
dev.off()
dist_eq <- move[[1]]
dist_eq$bank_id <- "EQ01"
sum.table <- cbind(eq, dist_eq)
```
```{r}
dist_eq <- move[[1]]
dist_eq$bank_id <- "EQ01"
sum.table <- cbind(eq, dist_eq)
# Create a vector the loop looks at to get values
eq_ids <- levels(den$bank_id)
# Remove the first identity to avoid duplicating later
eq_ids <- eq_ids[c(2:length(eq_ids))]
for(i in eq_ids){
eq <- subset(den, bank_id==i)
eq$bank_id <- factor(eq$bank_id) #converts to factor
pdate <- as.POSIXct(strptime(as.character(eq$posix_date),"%Y.%m.%d")) #convert date to a format POSIX needs
data_xy=eq[c("mean_x", "mean_y")]
xysp <- SpatialPoints(data_xy) #creates a class Spatial Points for all locations
proj4string(xysp) <- CRS("+proj=utm +zone=55 +ellps=WGS84")
sppt <- data.frame(xysp) #creates a Spatial Data Frame from sppt
idsp <- data.frame(eq$bank_id) #creates a spatial data frame of ID
merge <- data.frame(idsp) #merges ID and Date into the same spatial data frame
coordinates(merge) <- sppt #adds ID and Date data frame with locations data frame
move <- as.ltraj(xy=eq[,c("mean_x","mean_y")], date=pdate, id=idsp) #creates an object of class ltraj to store movements
jpeg(file=paste(i, "plot.jpeg", sep="_"),
width=5500, height=5000, units="px", res=800)
plot(move, xlim=c(695750, 699250), ylim=c(6104250, 6107750),
xlab="Easting", ylab="Northing", main=paste(i))
plot(mfws, add=T)
dev.off()
dist_eq <- move[[1]]
dist_eq$bank_id <- i
eqtable <- cbind(eq, dist_eq)
sum.table <- rbind(sum.table, eqtable)
}
```
## Distance from release site
```{r}
den$dfr <- sqrt((den$release_x-den$mean_x)^2 +
(den$release_y-den$mean_y)^2)
```
# **Modelling**
## Data preparation
```{r}
# Read in identities data
animal <- read_excel(raw_data, sheet="identities") %>%
clean_names() %>%
mutate(year = factor(year),
moved_perc = as.numeric(as.character(moved_perc)),
distance_mean = as.numeric(as.character(distance_mean)),
distance_mean_zero = as.numeric(as.character(distance_mean_zero)),
# Create binary survival probability variable
fate = ifelse(fate=="Survived", 1, 0)) %>%
subset(bank_id!="EQ15" & bank_id!="EQ16" & bank_id!="EQ17" &
bank_id!="EQ25" & bank_id!="EQ32" & bank_id!="EQ33" &
bank_id!="EQ34" & bank_id!="EQ35" & bank_id!="EQ36" &
bank_id!="EQ37" & bank_id!="EQ38" & bank_id!="EQ39" &
year!="2017" & year!="2018" & year!="2018")
femanimal <- subset(animal, sex!="M" & year!="2016")
codenanimal <- subset(animal, bank_id!="EQ02" & bank_id!="EQ05" &
bank_id!="EQ10" & bank_id!="EQ11" &
bank_id!="EQ12" & bank_id!="EQ29")
```
## Survival
### By trial (fig_ 2A, model 1)
```{r}
animal$trial <- factor(animal$trial)
mod <- glm(fate ~ trial, data=animal,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(trial="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(trial="Tukey")))
jpeg(file=paste("Survival by trial (fig_ 2A, model 1).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$trial)
effects$trial <- factor(effects$trial)
effects$tukey <- c("a", "b", "b")
mod1 <- ggplot(effects, aes(x=trial, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper),
colour="orange", width=0.1) +
geom_point(shape=21, size=3,
fill="orange", colour="orange") +
theme(axis.line=element_line(colour="black"), legend.position="none",
panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.title=element_text(),
axis.text.y=element_text(angle=0, vjust=0.5,color="black"),
axis.text.x=element_text(angle=0, vjust=0.5,color="black"),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
xlab("Trial") + ylab("Probability of survival") +
geom_text(aes(x=trial, y=upper, label=tukey), nudge_y=0.05)
dev.off()
mod1
```
### By origin (model 2)
```{r}
mod <- glm(fate ~ origin, data=animal,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
mod <- glm(fate ~ origin, data=animal20162017,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(origin="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(origin="Tukey")))
jpeg(file=paste("Survival by origin (model 2).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$origin)
effects$origin <- factor(effects$origin)
effects$tukey <- "a"
mod2 <- ggplot(effects, aes(x=origin, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.1)) +
xlab("Origin") + ylab("Probability of survival") +
geom_text(aes(x=origin, y=upper, label=tukey), nudge_y=0.06)
dev.off()
```
### By sex (fig 2B, model 3)
```{r}
animal$sex <- factor(animal$sex)
mod <- glm(fate ~ sex, data=animal,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
animal2016$sex <- factor(animal2016$sex)
mod <- glm(fate ~ sex, data=animal2016,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(sex="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(sex="Tukey")))
jpeg(file=paste("By sex (fig 2B, model 3).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$sex)
effects$sex <- factor(effects$sex)
effects$tukey <- c("a", "b")
mod3 <- ggplot(effects, aes(x=sex, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), color="brown", width=0.1) +
geom_point(shape=21,size=3, fill="brown", color="brown") +
theme(axis.line=element_line(color="black"),
legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
xlab("Sex") + ylab("Probability of survival") +
geom_text(aes(x=sex, y=upper, label=tukey), nudge_y=0.05)
dev.off()
mod3
```
### By den sharing (model 4)
```{r}
mod <- glm(fate ~ coden_pa, data=codenanimal,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(coden_pa="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(coden_pa="Tukey")))
jpeg(file=paste("Survival by den sharing (model 4).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$coden_pa)
effects$tukey <- c("a", "a")
mod4 <- ggplot(effects, aes(x=coden_pa, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1)) +
xlab("") + ylab("Probability of survival") +
geom_text(aes(x=coden_pa, y=upper, label=tukey), nudge_y=0.06)
dev.off()
```
### By pouch young (model 5)
```{r}
mod <- glm(fate ~ pypa, data=femanimal,
family=binomial(link=logit))
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(pypa="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(pypa="Tukey")))
jpeg(file=paste("Survival by pouch young (model 5).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$pypa)
effects$pypa <- factor(effects$pypa)
effects$tukey <- c("a", "a")
mod5 <- ggplot(effects, aes(x=pypa, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1)) +
xlab("Pouch young") + ylab("Probability of survival") +
geom_text(aes(x=pypa, y=upper, label=tukey), nudge_y=0.06) #not working...
dev.off()
```
## Den sharing
### By trial (model 6)
```{r}
codenanimal$trial <- factor(codenanimal$trial)
mod <- glm(coden_pc ~ trial, data=codenanimal)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(trial="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(trial="Tukey")))
#this plot is not right
jpeg(file=paste("Den sharing by trial (model 6).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$trial)
effects$Year <- factor(effects$trial)
effects$tukey <- c("a", "a", "a")
mod6 <- ggplot(effects, aes(x=trial, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
xlab("Trial") + ylab("Percentage of fixes \n found den sharing (%)") +
geom_text(aes(x=trial, y=upper, label=tukey), nudge_y=3) #letters not in right position...
dev.off()
```
### By sex (model 7)
```{r}
mod <- glm(coden_pc ~ sex, data=codenanimal)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(sex="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(sex="Tukey")))
#this plot is not right
jpeg(file=paste("Den sharing by sex (model 7).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$sex)
effects$sex <- factor(effects$sex)
effects$tukey <- "a"
mod7 <- ggplot(effects, aes(x=sex, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
xlab("Sex") + ylab("Percentage of fixes \n found den sharing (%)") +
geom_text(aes(x=sex, y=upper, label=tukey), nudge_y=3)
dev.off()
```
### By origin (model 7.5)
```{r}
mod <- glm(CodenPC ~ origin, data=codenanimal)
summary(mod)
anova(mod,test=c("Chisq"))
pw <- glht(mod, mcp(origin="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(origin="Tukey")))
#this plot is not right
jpeg(file=paste("Den sharing by origin (model 7).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$origin)
effects$origin <- factor(effects$origin)
effects$tukey <- "a"
mod7 <- ggplot(effects, aes(x=origin, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
xlab("Origin") + ylab("Percentage of fixes \n found den sharing (%)") +
geom_text(aes(x=origin, y=upper, label=tukey), nudge_y=3)
dev.off()
```
## Movement
### Survival by movement (fig 2C, model 8)
```{r}
subdata <- animal[!is.na(animal$moved_perc),] #changed it to a subdata
subdata$fate <- as.numeric(as.character(subdata$fate)) # for some reason it was classified as a character vector
mod <- glm(fate ~ moved_perc, data=subdata,
family=binomial(link=logit)) #should be a binomial model
summary(mod)
anova(mod, test=c("Chisq"))
new.dat <- data.frame(moved_perc=seq(0.12, 0.857, length.out=100)) #creates a vector for the prediction function to predict for
predicted <- predict(mod, newdata=new.dat, se.fit=TRUE, type="link") #predicting on link scale
predicted <- data.frame(predicted)
plotdat <- cbind(new.dat,predicted)
plotdat$fit_r <- inv.logit(plotdat$fit) #creating the predicted value by transforming from the link scale to the response scale
plotdat$lower_r <- inv.logit(plotdat$fit-1.96*plotdat$se.fit) #creating rough 95%CIs before transforming to the response scale
plotdat$upper_r <- inv.logit(plotdat$fit+1.96*plotdat$se.fit) #there are more complicated and robust ways such as profiling and permutations...
jpeg(file=paste("Survival by movement (fig 2C, model 8).jpeg"),
width=2500, height=2500, units="px", res=800)
mod8 <- ggplot(plotdat, aes(x=moved_perc, ylim(0,1), y=fit_r, group=1)) +
geom_ribbon(aes(ymin=lower_r, ymax=upper_r, linetype=NA),
alpha=0.2, fill="forest green") +
geom_line(linetype=1, colour="forest green") +
geom_line(aes(y= upper_r ), linetype=3,
colour="forest green", lwd=0.5) +
geom_line(aes(y= lower_r), linetype=3,
colour="forest green", lwd=0.5) +
theme(axis.line=element_line(colour="black"),
legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_x_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
xlab("Proportion of days moved") + ylab("Probability of survival")
dev.off()
mod8
```
### Survival by distance (model 9)
```{r}
subdata <- animal[!is.na(animal$distance_mean),] #changed it to a subdata
subdata$fate <- as.numeric(as.character(subdata$fate)) # for some reason it was classified as a character vector
mod <- glm(fate ~ distance_mean, data=subdata,
family=binomial(link=logit)) #model including zeros
summary(mod)
anova(mod, test=c("Chisq"))
new.dat <- data.frame(distance_mean=seq(180, 1050, length.out=100)) #creates a vector for the prediction function to predict for
predicted <- predict(mod, newdata=new.dat, se.fit=TRUE, type="link") #predicting on link scale
predicted <- data.frame(predicted)
plotdat <- cbind(new.dat,predicted)
plotdat$fit_r <- inv.logit(plotdat$fit) #creating the predicted value by transforming from the link scale to the response scale
plotdat$lower_r <- inv.logit(plotdat$fit-1.96*plotdat$se.fit) # creating rough 95%CIs before transforming to the response scale
plotdat$upper_r <- inv.logit(plotdat$fit+1.96*plotdat$se.fit)
jpeg(file=paste("Survival by distance (model 9).jpeg"),
width=2500, height=2500, units="px", res=800)
mod9 <- ggplot(plotdat, aes(x=distance_mean, y=fit_r, group=1)) +
geom_ribbon(aes(ymin=lower_r, ymax=upper_r, linetype=NA),
alpha=0.2, colour="grey40", fill="grey40") +
geom_line(linetype=1,colour="grey40") +
geom_line(aes(y= upper_r ),linetype=3, colour="grey40", lwd=0.5) +
geom_line(aes(y= lower_r),linetype=3, colour="grey40", lwd=0.5) +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1)) +
xlab("Distance moved between dens (m)") + ylab("Probability of survival")
dev.off()
```
**Excluding zeros**
```{r}
subdata <- animal[!is.na(animal$distance_mean_zero),] #changed it to a subdata
subdata$fate <- as.numeric(as.character(subdata$fate)) # for some reason it was classified as a character vector
mod <- glm(fate ~ distance_mean_zero, data=subdata,
family=binomial(link=logit)) #model including zeros
summary(mod)
anova(mod,test=c("Chisq"))
mod <- glm(fate ~ distance_mean_zero, data=subdata,
family=binomial(link=logit)) #model excluding zeros
summary(mod)
anova(mod, test=c("Chisq"))
new.dat <- data.frame(distance_mean_zero=seq(180, 1050, length.out=100)) #creates a vector for the prediction function to predict for
predicted <- predict(mod, newdata=new.dat, se.fit=TRUE, type="link") #predicting on link scale
predicted <- data.frame(predicted)
plotdat <- cbind(new.dat,predicted)
plotdat$fit_r <- inv.logit(plotdat$fit) #creating the predicted value by transforming from the link scale to the response scale
plotdat$lower_r <- inv.logit(plotdat$fit-1.96*plotdat$se.fit) # creating rough 95%CIs before transforming to the response scale
plotdat$upper_r <- inv.logit(plotdat$fit+1.96*plotdat$se.fit)
jpeg(file=paste("Survival by distance (nozeros, model 9).jpeg"),
width=2500, height=2500, units="px", res=800)
mod9 <- ggplot(plotdat, aes(x=distance_mean_zero, y=fit_r, group=1)) +
geom_ribbon(aes(ymin=lower_r, ymax=upper_r, linetype=NA),
alpha=0.2, colour="grey40", fill="grey40") +
geom_line(linetype=1, colour="grey40") +
geom_line(aes(y= upper_r ), linetype=3,
colour="grey40", lwd=0.5) +
geom_line(aes(y= lower_r), linetype=3,
colour="grey40", lwd=0.5) +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1)) +
xlab("Distance moved between dens (m)") + ylab("Probability of survival")
dev.off()
```
### By trial (fig 3A, model 10)
```{r}
codenanimal$trial <- factor(codenanimal$trial)
mod <- glm(moved_perc ~ trial, data=codenanimal)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(trial="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(trial="Tukey")))
jpeg(file=paste("Movement by trial (fig 3A, model 10).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$trial)
effects$trial <- factor(effects$trial)
effects$tukey <- c("a", "b", "b")
mod10 <- ggplot(effects, aes(x=trial, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper),
colour="orange", width=0.1) +
geom_point(shape=21, size=3,
fill="orange", colour="orange") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
xlab("Trial") + ylab("Proportion of days moved") +
geom_text(aes(x=trial, y=upper, label=tukey), nudge_y=0.05)
dev.off()
mod10
```
### By origin (model 11)
```{r}
mod <- glm(moved_perc ~ origin, data=animal)
summary(mod)
anova(mod, test=c("Chisq"))
mod <- glm(moved_perc ~ origin, data=animal20162017)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(origin="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(origin="Tukey")))
jpeg(file=paste("Movement by origin (model 11).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$origin)
effects$origin <- factor(effects$origin)
effects$tukey <- "a"
mod11 <- ggplot(effects, aes(x=origin, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21, size=3, fill="black", colour="black") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1)) +
xlab("Origin") + ylab("Proportion of days moved") +
geom_text(aes(x=origin, y=upper, label=tukey), nudge_y=0.06)
dev.off()
```
### By sex (fig 3B, model 12)
```{r}
mod <- glm(moved_perc ~ sex, data=animal)
summary(mod)
anova(mod, test=c("Chisq"))
mod <- glm(moved_perc ~ sex, data=animal2016)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(sex="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(sex="Tukey")))
jpeg(file=paste("Movement by sex (fig 3B, model 12).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$sex)
effects$sex <- factor(effects$sex)
effects$tukey <- c("a", "b")
mod12 <- ggplot(effects, aes(x=sex, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), colour="brown", width=0.1) +
geom_point(shape=21,size=3, fill="brown", colour="brown") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
xlab("Sex") + ylab("Proportion of days moved") +
geom_text(aes(x=sex, y=upper, label=tukey), nudge_y=0.05)
dev.off()
mod12
```
### By den sharing (fig 3C, model 13)
```{r}
hist(logit(codenanimal$moved_perc))
mod <- glm(moved_perc ~ coden_pa, data=codenanimal)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(coden_pa="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(coden_pa="Tukey")))
jpeg(file=paste("Movement by den sharing (fig 3C, model 13).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$coden_pa)
effects$tukey <- c("a", "b")
mod13 <- ggplot(effects, aes(x=coden_pa, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper),
colour="forest green", width=0.1) +
geom_point(shape=21, size=3, fill="forest green", colour="forest green") +
theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"),
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1.04)) +
xlab("") + ylab("Proportion of days moved") +
geom_text(aes(x=coden_pa, y=upper, label=tukey), nudge_y=0.05)
dev.off()
```
### By pouch young (model 14)
```{r}
mod <- glm(moved_perc ~ pypa, data=femanimal)
summary(mod)
anova(mod, test=c("Chisq"))
pw <- glht(mod, mcp(pypa="Tukey"))
ph <- cld(pw, alpha=0.050, Letters=letters, adjust="tukey")
summary(glht(mod, mcp(pypa="Tukey")))
jpeg(file=paste("Movement by pouch young (model 14).jpeg"),
width=2500, height=2500, units="px", res=800)
effects <- allEffects(mod)
effects <- data.frame(effects$pypa)
effects$pypa <- factor(effects$pypa)
effects$tukey <- c("a", "a")
mod14 <- ggplot(effects, aes(x=pypa, y=fit, group=1)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1) +
geom_point(shape=21,size=3,fill="black",colour="black") +
theme(axis.line=element_line(colour="black"),
#legend.position="none" #legend
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.text.y=element_text(angle=0, vjust=0.5, color="black"), #y axis tick label
axis.text.x=element_text(angle=0, vjust=0.5, color="black"),
axis.title=element_text(),
strip.text.y=element_blank()) +
scale_y_continuous(breaks=seq(0, 1, 0.2),
labels=seq(0, 1, 0.2),
limits=c(0, 1)) +
xlab("Pouch young") + ylab("Probability of survival") +
geom_text(aes(x=pypa, y=upper, label=tukey), nudge_y=0.06) #not working...
dev.off()
```
## Combine plots
```{r}
fig2 <- ggarrange(mod1, mod3 + rremove("ylab") +
rremove ("y.text") + rremove ("y.ticks"),
mod8 + rremove("ylab") +
rremove ("y.text") + rremove ("y.ticks"),
heights=c(3,3),widths=c(3,3), #y axis titles
label.x=0.89, label.y=0.99, hjust=-0.5, vjust=1.5, #figure labels
#labels=c("a","b", "c"), #figure labels
ncol=3, nrow=1, #align="h", #arrangement of matrix
font.label=list(size=10, face="bold", color ="black"))
print(fig2)
ggsave(filename="fig_2.tiff", fig2, width=190, height=75, units="mm")
fig3 <- ggarrange(mod10, mod12 + rremove("ylab") +
rremove ("y.text") + rremove ("y.ticks"),
mod13 + rremove("ylab") +
rremove ("y.text") + rremove ("y.ticks"),
heights=c(3, 3),widths=c(3,3) , #y axis titles
label.x=0.89, label.y=0.99, hjust=-0.5, vjust=1.5, #figure labels
#labels=c("a","b","c"), #figure labels
ncol=3, nrow=1, #align="h", #arrangement of matrix
font.label=list(size=10, face="bold", color ="black"))
print(fig3)
ggsave(filename="fig_3.tiff", fig3, width=190, height=75, units="mm")
```
# **Model selection**
## Survival by trial and sex
```{r}
animal <- animal %>%
mutate(trial = factor(trial),
sex = factor(sex))
mod1 <- glm(fate ~ trial + sex, data=animal,
family=binomial(link=logit))
mod2 <- glm(fate ~ trial, data=animal,
family=binomial(link=logit))
mod3 <- glm(fate ~ sex, data=animal,
family=binomial(link=logit))
AICc(mod1, mod2, mod3)
summary(mod2)
```
## Movement by trial and sex
```{r}
mod1 <- glm(moved_perc ~ trial + sex, data=animal)
mod2 <- glm(moved_perc ~ trial * sex, data=animal)
mod3 <- glm(moved_perc ~ trial, data=animal)
mod4 <- glm(moved_perc ~ sex, data=animal)
AICc(mod1, mod2, mod3, mod4)
summary(mod2)
```
## Combine plots
```{r}
fig2 <- ggarrange(mod1, mod3 + rremove("ylab") +
rremove ("y.text") + rremove ("y.ticks"),