-
Notifications
You must be signed in to change notification settings - Fork 2
/
ctmm_interactions.R
191 lines (141 loc) · 4.95 KB
/
ctmm_interactions.R
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
# This script details methods that can be used for studying
# interactions between individuals. This includes:
# - Home-range overlap
# - Encounter location distributions (CDE)
# - Pairwise distances
# - Proximity ratios
# - Encounter rates
# These analyses are conditional on fitted movement models and HR estimates
# (see: https://github.com/ctmm-initiative/ctmmlearn/blob/main/ctmm_akde.R)
library(ctmm)
data("buffalo")
projection(buffalo) <- median(buffalo)
load("data/buffalo.rda") # Fitted movement models; object called 'FITS'
load("data/buffalo_akdes.rda") # Estimated HR areas; object called 'AKDES'
#-----------------------------------------------------
# Home-range overlap
#-----------------------------------------------------
# Do individuals share the same space?
# Relevant paper: https://doi.org/10.1111/2041-210X.13027
help("overlap")
#Estimate HR overlap for all pairs
#Note: these all must have compatible resolutions and alignments
OVER <- overlap(AKDES)
# This will generate an error because of incompatible grids
overlap(list(akde(buffalo$Pepper, FITS$Pepper),
akde(buffalo$Queen, FITS$Pepper)))
# But this works because HRs are estimated simultaneously (and consistently)
overlap(akde(list(buffalo$Pepper,buffalo$Queen),
list(FITS$Pepper, FITS$Queen)))
# look at everything
OVER
# pairwise CIs
OVER$CI["Pepper","Toni",]
OVER$CI["Queen","Toni",]
# point estimates
OVER$CI[,,"est"]
#-----------------------------------------------------
# Encounter location distributions (CDE)
#-----------------------------------------------------
# where encounters are expected to take place
# Relevant paper: https://doi.org/10.1111/2041-210X.13597
help("cde")
#Plot the data and HR estimates
plot(buffalo[c("Pepper", "Queen")],
UD=AKDES[c("Pepper", "Queen")],
col = c("#e76f51", "#264653"),
col.DF=c("#f4a261", "#2a9d8f"),
col.grid = NA)
#Estimate the home range overlap
overlap(AKDES[c("Pepper", "Queen")])
#Estimate the CDE
CDE <- cde(AKDES[c("Pepper", "Queen")])
#Visualise the CDE
plot(buffalo[c("Pepper", "Queen")],
col=c("#e76f51", "#264653"),
UD=CDE,
col.DF="#046C9A",
col.grid = NA)
#-----------------------------------------------------
# Pairwise proximity and distance metrics
#-----------------------------------------------------
# metrics that takes time into account (paper coming)
help("proximity")
#Pairwise separation distances
DISTS <- distances(buffalo[c("Cilla","Mvubu")],
FITS[c("Cilla","Mvubu")])
#Visualise the separation distances
plot(DISTS$est ~ DISTS$timestamp,
type = "l",
col = "#5e548e")
# Internal plotting function (work in progress)
ctmm:::ts.plot(DISTS)
# what would totally independent motion look like?
cilla_sim <- simulate(FITS$Cilla, t = buffalo$Cilla$t)
mvubu_sim <- simulate(FITS$Mvubu, t = buffalo$Mvubu$t)
sim_dists <- distances(list(cilla_sim, mvubu_sim),
FITS[c("Cilla","Mvubu")])
#Plot the data
par(mfrow = c(2,2))
plot(buffalo[c("Cilla", "Mvubu")],
col = c("#e76f51", "#264653"),
main = "Empirical data")
plot(list(cilla_sim, mvubu_sim),
col = c("#e76f51", "#264653"),
main = "Simulated data")
plot(DISTS$est ~ DISTS$timestamp,
type = "l",
col = "#5e548e",
main = "Empirical distances",
ylab = "Distance (m)",
xlab = "Time",
ylim = c(0,max(sim_dists$est)))
plot(sim_dists$est ~ sim_dists$timestamp,
type = "l",
col = "#5e548e",
main = "Simulated distances",
ylab = "Distance (m)",
xlab = "Time",
ylim = c(0,max(sim_dists$est)))
# Proximity ratio (note: can be slow)
help('proximity')
PROXIMITY <- proximity(buffalo[c("Cilla","Mvubu")],
FITS[c("Cilla","Mvubu")])
load("data/buffalo_proximity.rda")
PROXIMITY
# Proximity ratio for simulated animals
SIM_PROXIMITY <- proximity(list(cilla_sim, mvubu_sim),
FITS[c("Cilla","Mvubu")])
load("data/simulated_proximity.rda")
SIM_PROXIMITY
#-----------------------------------------------------
# Encounters
#-----------------------------------------------------
help("encounter")
# Relevant paper: https://doi.org/10.1101/2023.06.07.544097
#Empirical encounters
DISTS$encounter <- ifelse(DISTS$est <= 100, 1, 0)
#Visualise the results
par(mfrow = c(1,1))
plot(DISTS$encounter ~ DISTS$timestamp)
cdplot(as.factor(DISTS$encounter) ~ DISTS$timestamp)
#Empirical Encounter rate (n/day)
n <- sum(DISTS$encounter)
t <- "day" %#% (DISTS$t[nrow(DISTS)] - DISTS$t[1])
n/t
#If you do this, run a sensitivity analysis
enc_rad <- 1:1000
N <- vector("numeric", 1000)
for(i in 1:length(enc_rad)){
N[i] <- sum(ifelse(DISTS$est <= enc_rad[i], 1, 0))
}
#visualise the results
plot(N ~ enc_rad,
ylab = "Encounters",
xlab = "Encounter radius",
type = "l",
col = "#5e548e")
#Estimate relative encounter rates
RATES <- encounter(AKDES,method="PDF")
RATES$CI["Cilla","Mvubu",] * 100^2 # good for small distances
tanh(sqrt(RATES$CI["Cilla","Mvubu",])*100)^2 # more reliable