-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathctmm_error.R
265 lines (195 loc) · 5.65 KB
/
ctmm_error.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
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
#############
# TELEMETRY ERROR MODELING
# https://www.biorxiv.org/content/10.1101/2020.06.12.130195v2.full
#############
#############
## STEP 0: Do you need to model error?
# How do the scales of error compare to the scales of movement?
#############
## STEP 1: Do you have calibrated data or calibration data?
# Calibration data can be collected or opportunistic.
# Without calibration data, you should supply a prior
#############
## IF YOUR DATA NEEDS CALIBRATION
## STEP 1B: What columns do you have in your data?
# DOP values? (HDOP, VDOP, PDOP, GDOP, ...)
# location classes?
# number of satellites?
# time-to-fix timeout?
# ctmm will try to pick out the best data
# load ctmm library
library(ctmm)
# load turtle data
data(turtle)
# or as.telemetry on the turtle data filename
##############
## IF YOUR DATA NEEDS CALIBRATION
## AND ESPECIALLY IF YOUR DATA HAVE NUMEROUS ERROR-RELATED COLUMNS
## STEP 1C: Error model selection
# turtle datasets
names(turtle)
# first two are calibration data - not turtles
# second look at columns
head(turtle[[1]])
# HDOP: horizontal dilution or precision -- proportional to RMS error
# location class:
## Assuming all of the information is good
help("uere.fit")
# fit error parameters to calibration data
UERE <- uere.fit(turtle[1:2])
# do not run uere.fit on tracking data
# estimated error model parameters
summary(UERE)
# apply error model to data
uere(turtle) <- UERE
head(turtle$F231)
plot(turtle$F231)
## If we aren't sure about the error data:
## QUESTION 1: Are the HDOP and location class values informative?
data(turtle)
# make a list to store error models
UERES <- list()
# first attempt: let's use everything
UERES$all <- uere.fit(turtle[1:2])
# do not run uere.fit on tracking data
# summarize error model
summary(UERES$all)
# second attempt: let's drop the location class information
# copy of calibration data
test <- turtle[1:2]
# delete location class column
test[[1]]$class <- NULL
test[[2]]$class <- NULL
uere(test) <- NULL
# store error-model fit (HDOP only)
UERES$HDOP <- uere.fit(test)
# summarize error model
summary(UERES$HDOP)
# third attempt: let's further drop the HDOP values
# delete HDOP column
test[[1]]$HDOP <- NULL
test[[2]]$HDOP <- NULL
# store error-model fit
UERES$nada <- uere.fit(test)
# summarize error model
summary(UERES$nada)
# compare error-models
summary(UERES)
# AICc: super-fancy AIC values
# reduced Z-squared statistic (goodness of fit)
# compare to reduced chi-squared statistic (1 is good)
## PROBLEM 2: Are these GPS tags identical?
# create a list to store individualized error models
indiv <- list()
# calculate individual UEREs
indiv[[1]] <- uere.fit(turtle[[1]])
indiv[[2]] <- uere.fit(turtle[[2]])
# compare calibration parameters
summary(UERES$all) # joint model
summary(indiv[[1]])
summary(indiv[[2]])
# store with joint models
UERES$indiv <- indiv
# compare to joint models
summary(UERES)
#############
# ERROR CALIBRATION
#############
# calibrate turtle data with best error model
uere(turtle) <- UERES$all
# error columns now in data
head(turtle[[1]])
#############
# ERROR-MODEL RESIDUALS
#############
# calculate residuals of calibration data w.r.t best error model
RES <- lapply(turtle[1:2],residuals)
# plot residuals
plot(RES)
# calculate residuals of calibration data w.r.t. worst error model
uere(test) <- UERES$nada
RES2 <- lapply(test,residuals)
plot(RES2)
#############
# ERROR-INFORMED MOVEMENT ANALYSIS
#############
# turtle data again
names(turtle)
# take female turtle 231
DATA <- turtle$F231
# plot data
plot(DATA)
help('outlie')
# look for outliers
OUT <- outlie(DATA)
plot(OUT)
# you may get other useful information here
head(OUT)
# good location estimates
GOOD <- OUT$speed < 0.05 # biological threshold for this species (wood turtle)
# take only good location estimates
DATA <- DATA[GOOD,]
# re-check
plot(DATA)
OUT <- outlie(DATA)
plot(OUT)
# create guesstimate interactively
ctmm.guess(DATA)
# * check the error box
# create guesstimate non-interactively
GUESS <- ctmm.guess(DATA,CTMM=ctmm(error=TRUE),interactive=FALSE)
# new argument CTMM, which can contain extra parameters,
# here error=TRUE
# fit models
FITS <- ctmm.select(DATA,GUESS,verbose=TRUE,trace=3,cores=-1)
# verbose=TRUE returns all candidate models
# I've already run this code for you
# save(FITS,file="data/turtle.rda")
load("data/turtle.rda")
# look at all models
summary(FITS)
# look at best model
summary(FITS[[1]])
# compare to prior model
summary(uere(DATA))
# compare to movement model without error model
GUESS <- FITS[[1]]
GUESS$error <- FALSE
FIT.NE <- ctmm.fit(DATA,GUESS,trace=2)
summary(FITS[[1]])$CI
summary(FIT.NE)$CI
## Smoothing data for other packages (not ctmm)
help('predict',package="ctmm")
SMOOTH <- predict(DATA,FITS[[1]])
plot(DATA)
plot(SMOOTH)
SIM <- simulate(DATA,FITS[[1]])
plot(SIM)
## IF YOU DIDN'T HAVE CALIBRATION DATA, SUPPLY A PRIOR
# load un-calibrated datas
data(turtle)
# will need to match the class structure (2D,3D here)
summary(uere(turtle))
# supply point estimates
# 20-meter 2D error at HDOP=1
# 10-meter 3D error at HDOP=1
uere(turtle) <- c(20,10)
# extract calibration object
PRIOR <- uere(turtle)
# the default uncertainty when assigning numerical error is zero
summary(PRIOR)
PRIOR$DOF
# set DOF for wide credible intervals
PRIOR$DOF[] <- 2
summary(PRIOR)
# assign prior to data
uere(turtle) <- PRIOR
# automated guesstimate for calibrated data
GUESS <- ctmm.guess(turtle[[3]],CTMM=ctmm(error=TRUE),interactive=FALSE)
FIT.PRIOR <- ctmm.select(turtle[[3]],GUESS,trace=3,cores=-1)
# this will take a while, but comes out consistent
# save(FIT.PRIOR,file="data/turtle-prior.rda")
load("data/turtle-prior.rda")
summary(FIT.PRIOR)
# compare update to prior
summary(PRIOR)