-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsims_workflow.Rmd
More file actions
505 lines (313 loc) · 10.9 KB
/
Copy pathsims_workflow.Rmd
File metadata and controls
505 lines (313 loc) · 10.9 KB
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
#---
title: "sims_workflow"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(boot)
library(ggplot2)
library(plyr)
```
Get parameter file and time stamp
```{r}
rm(list=ls())
parameterDF <- read.csv("parameters_moreRangeChangeSims.csv")
#parameteDF is a file with the list of the all the parameters and their values for each simulation run
#decie on which run
type = 22
systemTime <- gsub(" ","_", Sys.time(),fixed=TRUE)
systemTime <- gsub(":","-",systemTime)
```
Load in functions
```{r}
source('R/getParams.R')
source('R/getOccuHistory.R')
source('R/getRepeatVisits.R')
source('R/getSpartaFormat.R')
source('R/getBugsData.R')
source('R/runModel.R')
source('R/helperFunctions.R')
```
Create some replicated worlds
```{r, echo=FALSE, results='hide'}
getParams(parameterDF,mysimNu=type)
Obs <- replicate(300,getSims(),simplify=FALSE)
save(Obs,file=paste0("output/Obs_",type,"_",systemTime,".RData"))
getObsSummaries(Obs)
trueSummaries <- gettrueSummaries(combineObs(Obs))
```
Run the basic model
```{r, message=FALSE, warning=FALSE}
out<- lapply(Obs,function(x)getModels(x,type=type,myModel))
#save(out,file=paste0("output/out_",type,"_",systemTime,".RData"))
```
Compare real and predicted intercept (psi1) and slope (psi change from last to first)
```{r}
checkResults(out,Obs)
```
Get summaries over parameters, model types
```{r}
print(modelSummaries <- getModelSummaries(out))
```
Compare to the analysis of raw data
```{r, message=FALSE, warning=FALSE}
outRaw <- ldply(Obs,function(x)rawdataAnalysis(x))
#save(outRaw,file=paste0("output/outRaw_",type,"_",systemTime,".RData"))
rawSummaries <- getrawSummaries(outRaw)
```
plot comparison
```{r}
plotComparison(modelSummaries,rawSummaries,trueSummaries)
#500/250
```
plot comparison of differences
```{r}
plotDifference(Obs,out,outRaw)
```
Power analysis
```{r}
powerAnalysis(out,outRaw)
```
Later comparison
```{r}
type <- 19
#load outputs
files <- list.files("output/")
setwd("output")
load(files[grepl(paste("Obs",type,sep="_"),files)])
load(files[grepl(paste("out",type,sep="_"),files)])
load(files[grepl(paste("outRaw",type,sep="_"),files)])
#summarise
modelSummaries <- getModelSummaries(out)
rawSummaries <- getrawSummaries(outRaw)
trueSummaries <- gettrueSummaries(combineObs(Obs))
#plot
plotComparison(modelSummaries,rawSummaries,trueSummaries)
plotDifference(Obs,out,outRaw)
```
(1) Autocorrelation
(a) probability of dependence between sequential visits
- at a proportion of sites
- autocorrelation of order p with certain probability
#fix = indicate previous observation as an indicator in the obs model
```{r}
source('R/scenario_autocorrelation.R')
runSims <- function(Obs,propSites=0.5,autoProb=0.5){
#reduce visits in non-atlas years
Obs_auto <-applyAutocorrelation(Obs,propSites,autoProb)
return(Obs_auto)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="Autocorrelation",myModel))
#fixed OD model
outFixed <- lapply(Obs_auto,function(x)fixAutocorrelation(x))
```
(b) Not reporting after first observation
- at prop p of sites
# fix = remove records after first detection??
```{r}
source('R/scenario_autocorrelation.R')
runSims <- function(Obs,propSites=0.5){
#reduce visits in non-atlas years
Obs_auto <-applyFullAutocorrelation(Obs,propSites)
return(Obs_auto)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="FullAutocorrelation",myModel))
#fixed OD model
outFixed <- lapply(Obs_auto,function(x)fixFullAutocorrelation(x))
```
(2) Atlas scenarios
(a) Pulse in recording - increased visits in one year (atlas year)
#fix might not be needed
```{r}
source('R/scenario_pulsedvisits.R')
runSims <- function(Obs,nStandardVisits=3){
#reduce visits in non-atlas years
Obs_pulsed <- pulsedVisits(Obs, nStandardVisits)
#return summary data
return(Obs_pulsed)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw <- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit OD models
out <- lapply(Obs_auto,function(x)getModels(x,bias="Pulsed",myModel))
```
(b) Pulse in recording - fewer sites are visited outside of atlas sites
#as long as unbiased should be ok
```{r}
source('R/scenario_atlassites.R')
runSims <- function(Obs,nStandardSites=10){
#reduce visits in non-atlas years
Obs_spread <- reduceSites(Obs,nStandardSites)
#return summary data
return(Obs_spread)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit OD models
out <- lapply(Obs_auto,function(x)getModels(x,bias="Spread",myModel))
```
(c) Pulse in recording - Lower quality grid cells less likely to be visited outside of atlas years
```{r}
source('R/scenario_pulsedvisits.R')
runSims <- function(Obs,lowProb=0.25){
#reduce visits in non-atlas years
Obs_spread <- pulsedSpreadVisits(Obs,SCovariate,lowProb)
#return summary data
return(Obs_spread)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit OD models
out <- lapply(Obs_auto,function(x)getModels(x,bias="SpreadLower",myModel))
```
(2) Effects of habitat degradation on effort ("car park")
(a) fewer visits to low quality sites (site quality static)
```{r}
source('R/scenario_frequencyvisits.R')
runSims <- function(Obs,type="static",siteEffect=0.15){
Obs_reduced <-applyReducedVisits(Obs,type=type,siteEffect=siteEffect)
return(Obs_reduced)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="ReducedQ",myModel))
```
(b) lower detection probability in lower quality sites (site quality static)
```{r}
source('R/scenario_lowquality.R')
runSims <- function(Obs,meanSiteDetEffect=0.15){
#specify site effect on detection probability
SiteDetEffects <- rep(meanSiteDetEffect,NSpecies)
#get new repeat visits
Obs_lq <- getLQSims(Obs)
return(Obs_lq)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="ReducedD",myModel))
```
(c) as(a) except with site quality of some sites declining (positive interaction term)
fewer visits over time in low quality sites
```{r}
source('R/scenario_frequencyvisits.R')
runSims <- function(Obs,type="dynamic",timeEffect=0.1){
#reduce visits in non-atlas years
Obs_reduced <-applyReducedVisits(Obs,type=type,timeEffect=timeEffect)
return(Obs_reduced)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="ReducedQD",myModel))
```
(d) as(b) except with site quality of some sites declining (positive interaction term)
detection probability declines over time in low quality sites
```{r}
source('R/scenario_lowquality.R')
runSims <- function(Obs,timeEffect=0.1){
#low quality sites are declining
IntDetEffects <<- rep(timeEffect,NSpecies)
#get new repeat visits
Obs_lq <- getLQSims(Obs)
return(Obs_lq)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="ReducedDD",myModel))
```
(3) CS observer bias
(a) Bias towards specialists
- specialist trait covary with abundance (OccProb or Lambda)
- assume this affects species' detection probability
```{r}
source('R/scenario_specialistbias.R')
runSims <- function(Obs,propSites=1){
Obs <- getbiasedSims(propSites)
return(Obs)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="SpecialistSp",myModel))
```
(b) Bias towards declining species
- assume some species are declining
- assume this affects species' detection probability
- at prop p of sites
```{r}
source('R/scenario_specialistbias.R')
runSims <- function(Obs,biasEffect=0.15,propSites=1){
Obs <- getbiaseddeclineSims(biasEffect,propSites)
#get new repeat visits
Obs_lq <- getLQSims(Obs)
return(Obs_lq)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out <- lapply(Obs_auto,function(x)getModels(x,bias="DeclineSp",myModel))
```
(4) Probability of site visitation is higher if species previously seen there
#not work
```{r}
source('R/scenario_frequencyvisits.R')
runSims <- function(Obs){
#reduce visits in non-atlas years
Obs_reduced <-applyBiasedVisits(Obs)
return(Obs_reduced)
}
Obs_auto <- lapply(Obs,runSims)
#raw data analysis
outRaw<- ldply(Obs_auto,function(x)rawdataAnalysis(x))
#fit standard OD model
out_reduced <- lapply(Obs_auto,function(x)getModels(x,bias="ReducedV",myModel))
```
<!-- (5) Mismatch between scale of dynamics and observation -->
<!-- ```{r} -->
<!-- source('C:/Users/db40fysa/Nextcloud/sMon-Analyses/MoreRangeChangeSims/R/scenario_spatialmismatch.R') -->
<!-- runSims <- function(x){ -->
<!-- Occ <- getOccuHistory(OccProb, spatialbeta=Seffects, interactionbeta=Ieffects, spatialcovariates=Scovariate,temporalbeta=Teffects, temporalcovariates=Tcovariate) -->
<!-- Obs <- getRepeatVisits(Occ, NVisits, DetProb, SiteDetEffects,YearDetEffects,IntDetEffects, Scovariate,Tcovariate) -->
<!-- #apply scenario -->
<!-- Obs_grouped <- groupSite(NSites=NSites, Obs=Obs) -->
<!-- Obs_reduced <- reduceVisits(Obs=Obs_grouped) -->
<!-- #with original dataset -->
<!-- modelData <- getSpartaFormat(Obs,focalSpecies=focalSpecies) -->
<!-- bugs.data <- getBugsData(modelData) -->
<!-- modelSummary <- runModel(bugs.data,modelData,focalSpecies) -->
<!-- modelSummary$Type <- "Standard" -->
<!-- #with fixed dataset -->
<!-- #modelData_grouped <- getSpartaFormat(Obs=Obs_grouped,focalSpecies=focalSpecies) -->
<!-- #bugs.data_grouped <- getBugsData(modelData=modelData_grouped) -->
<!-- #modelSummary_grouped <- runModel(bugs.data=bugs.data_grouped,modelData=modelData_grouped,focalSpecies) -->
<!-- #modelSummary_grouped$Type <- "grouped" -->
<!-- #with fixed dataset -->
<!-- modelData_reduced <- getSpartaFormat(Obs=Obs_reduced,focalSpecies=focalSpecies) -->
<!-- bugs.data_reduced <- getBugsData(modelData=modelData_reduced) -->
<!-- modelSummary_reduced <- runModel(bugs.data=bugs.data_reduced, -->
<!-- modelData=modelData_reduced, -->
<!-- focalSpecies,myModel) -->
<!-- modelSummary_reduced$Type <- "Reduced" -->
<!-- return(rbind(modelSummary,modelSummary_reduced)) -->
<!-- } -->
<!-- ``` -->