forked from adammwilson/SpatialAnalysisTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhSDM_Tinamou.Rmd
534 lines (404 loc) · 18.4 KB
/
hSDM_Tinamou.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
---
title: "Overview of hSDM"
author: "Adam M. Wilson"
date: "April 7, 2015"
output:
html_document:
keep_md: yes
toc: yes
pdf_document: default
---
```{r setup, echo=FALSE, message=FALSE, results='hide', purl=FALSE}
## This chunk automatically generates a text .R version of this script when running within knitr. You do not need to run this...
library(knitr)
#input = knitr::current_input() # filename of input document
input="SDM_Tinamou/hSDM_Tinamou.Rmd"
output = paste(tools::file_path_sans_ext(input), 'R', sep = '.')
#knitr::purl(input,output,documentation=2,quiet=T)
repo="adammwilson/SpatialAnalysisTutorials/blob/master"
knitr::opts_chunk$set(cache=T)
#Sys.setenv(PATH="/Library/Frameworks/GDAL.framework/Programs/:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Applications/:${PATH}")
## set light theme for ggplot
library(ggplot2)
theme_set(theme_light()+
theme(text=element_text(size = 28)))
```
# Example application
## Starting R on Omega (_in an interactive job_)
First `ssh` to omega
```{}
```
Today we're going to work in an _interactive_ session on a compute node rather than the _head_ node. To start one, first `source .bashrc` and then request an _interactive_ job with X forwarding. Then start `R`:
```{}
source .bashrc
qsub -q fas_devel -I -X -l nodes=1:ppn=2
R
```
For more information on what `qsub` means, check out the [Yale HPC FAQ](https://hpc.research.yale.edu/hpc_user_wiki/index.php/Torque_Userguide#qsub_-I).
You should see something like this:
```{}
[aw524@login-0-0 ~]$ qsub -q fas_devel -I -X -l walltime=02:00:00,nodes=1:ppn=2
qsub: waiting for job 4599636.rocks.omega.hpc.yale.internal to start
qsub: job 4599636.rocks.omega.hpc.yale.internal ready
[aw524@compute-79-4 ~]$
```
## Load libraries
And load some packages (either from your own private library or from mine).
```{r loadLibraries,results='hide',message=FALSE}
lpath="/lustre/scratch/client/fas/geodata/aw524/R/"
#library(package = "lattice",lib.loc=lpath)
library(rgdal)
.libPaths(new=lpath)
packages=c("hSDM","dismo","maptools","sp",
"maps","coda","rgdal","rgeos",
"doParallel","rMOL","reshape",
"ggplot2","knitr","rasterVis")
l=lapply(packages, library,
character.only=T,quietly=T)
## set some raster and processing options
rasterOptions(chunksize=1000,maxmemory=1000)
ncores=2 # number of processor cores you would like to use
registerDoParallel(ncores)
```
If you don't have the packages above, install them in the package manager or by running `install.packages("doParallel")`.
## Locate environmental data
First set the path to the data directory. You can write a script to run on multiple machines by testing the `sysname` (or username, etc.).
```{r}
if(Sys.info()[["sysname"]]=="Darwin") datadir="~/work/env/"
if(Sys.info()[["sysname"]]=="Linux") datadir="/lustre/scratch/client/fas/geodata/aw524/data"
```
And create an output directory `outputdir` to hold the outputs. It's a good idea to define these as variables so it's easy to change them later if you move to a different machine.
```{r}
outputdir="~/scratch/data/tmp"
## check that the directory exists, and if it doesn't then create it.
if(!file.exists(outputdir)) dir.create(outputdir,recursive=T)
```
__________
# Example Species:
## Set species name and extract data from MOL:
```{r,message=FALSE,results='hide'}
#species="Lepidocolaptes_lacrymiger"
species="Tinamus_solitarius"
```
## Query eBird data contained in MOL
_Presences_: all observations of our species
_Trials_: all unique observation locations for any species limited to bounding box of expert range where observer indicated recording all observed species (`all_species_reported='t'`)
> The best method for selecting data to use for _non-detections_ is very case and dataset specific.
Metadata for eBird Reference Dataset ((Version 5.0. Cornell Lab of Ornithology) [is available here](http://ebird.org/ebird/data/download)
## Extract data from MOL
```{r}
dsp=MOLget(species,type=c("ebird","range"))
points_all=dsp[["ebird"]]
range=dsp[["range"]]
```
Check out the data structure:
```{r headSpd,results='asis'}
kable(head(points_all[,-1]), format = "markdown")
```
Explore observer effort: sampling duration, distance travelled, and area surveyed.
```{r spdDurationPlot,fig.width=10,fig.height=7}
ggplot(points_all@data,aes(
x=effort_distance_km))+
xlab("Sampling Distance (km)")+
geom_histogram(binwidth = .1)+scale_x_log10()+
geom_vline(xintercept=c(1,5),col="red")
```
Also note that there are many records with missing spatial certainty values.
```{r spNA}
table("Distance/Area"=!is.na(points_all$effort_distance_km)|
!is.na(points_all$effort_area_ha))
```
> For this exercise, we'll simply remove points with large or unknown spatial uncertainty. Incorporating this spatial uncertainty into distribution models is an active area of research.
Filter the data below thresholds specified above using `filter` from the [dplyr library](http://cran.r-project.org/package=dplyr).
```{r filterSpd}
cdis=5 # Distance in km
points=points_all[
(points_all$effort_distance_km<=cdis&
!is.na(points_all$effort_distance_km)),]
```
## Load eBird sampling dataset
```{r,results='hide'}
## link to global sampling raster
gsampling=raster(file.path(datadir,"eBirdSampling_filtered.tif"))
names(gsampling)="trials"
## crop to species range to create modelling domain
sampling=crop(gsampling,dsp[["range"]],file.path(outputdir,"sampling.grd"),overwrite=T)
## assign projection
projection(sampling)="+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
```
## Create a combined presence-nondetection point dataset
```{r}
presences=rasterize(as(points,"SpatialPoints"),sampling,fun='count',background=0)
fitdata=cbind.data.frame(presences=values(presences),trials=values(sampling),coordinates(presences))
## filter to locations with at least one trial
fitdata=fitdata[fitdata$trials>0,]
```
Convert to a spatialDataFrame to faciliate linking with georeferenced environmental data.
```{r spSpd}
coordinates(fitdata)=c("x","y")
projection(fitdata)="+proj=longlat +datum=WGS84 +ellps=WGS84"
fitdata@data[,c("lon","lat")]=coordinates(fitdata)
```
### Load coastline from maptools package for plotting.
```{r loadCoast}
coast <- map_data("world",
xlim=c(bbox(range)[1,1]-1,bbox(range)[1,2]+1),
ylim=c(bbox(range)[2,1]-1,bbox(range)[2,2]+1))
ggcoast=geom_path(data=coast,
aes(x=long,y=lat,group = group),lwd=.1)
```
## Available Species Data
```{r spdPlot, message=FALSE}
ggplot(fitdata@data,aes(y=lat,x=lon))+
ggcoast+
geom_path(data=fortify(range),
aes(y=lat,x=long,group=group),
col="green")+
geom_point(data=fitdata@data[fitdata$presences==0,],
aes(x=lon,y=lat),pch=1,
col="black",cex=.8,lwd=2,alpha=.3)+
geom_point(data=fitdata@data[fitdata$presences>=1,],
aes(x=lon,y=lat),pch=3,
col="red",cex=2,lwd=3,alpha=1)+
ylab("Latitude")+xlab("Longitude")+
coord_equal()+
xlim(c(min(fitdata$lon),max(fitdata$lon)))+
ylim(c(min(fitdata$lat),max(fitdata$lat)))
```
______________________
## Environmental Data
We also have the environmental data available globally. Like last week, we'll focus on the following variables:
```{r}
## list of environmental rasters to use (names are used to re-name rasters):
fenv=c(
cld="cloud/meanannual.tif",
cld_intra="cloud/intra.tif",
elev="elevation_mn_GMTED2010_mn.tif",
forest="tree_mn_percentage_GFC2013.tif")
```
* **forest**: (1000s %, Hansen, Amatulli&Jetz)
* **elev**: Elevation (m, Amatulli&Jetz)
* **cld**: Mean Annual Cloud Frequency (1000s %, Wilson&Jetz)
* **cld_intra**: Cloud Seasonality (1000s %, Wilson&Jetz)
> If you want to explore using other variables, you can use `list.files(datadir,recursive=T)` to see all the available files.
To facilitate modelling, let's crop the global rasters to a smaller domain. We can use the extent of the expert range and the `crop()` function in raster package.
```{r,results='hide'}
## crop to species domain and copy to project folder
env=foreach(i=1:length(fenv))%do%{
fo=file.path(outputdir,paste0(names(fenv)[i],"_clipped.grd"))
crop(raster(file.path(datadir,fenv[i])),range,file=fo,overwrite=T)
}
```
Read the environmental data in as a raster stack:
```{r,ImportRaster, results='hide'}
env=stack(list.files(path = outputdir, pattern="*_clipped.grd$" , full.names = TRUE ))
env
## rename layers for convenience
names(env)=names(fenv)
## mask by elevation to set ocean to 0
env=mask(env,env[["elev"]],maskvalue=0)
```
Set the Variable selection is tricky business and we're not going to dwell on it here... We'll use all the following variables.
```{r}
vars=c("cld","cld_intra","elev","forest")
```
Scaling and centering the environmental variables to zero mean and variance of 1, using the ```scale``` function is typically a good idea.
```{r scaledata}
senv=scale(env[[vars]])
```
### Visualize the environmental data
```{r plotEnv,fig.width=11,warning=FALSE,message=FALSE}
## set plotting limits using expert range above
gx=xlim(extent(env)@xmin,extent(env)@xmax)
gy=ylim(extent(env)@ymin,extent(env)@ymax)
## Environmental data
gplot(senv)+
geom_raster(aes(fill=value)) +
facet_wrap(~variable,nrow=2) +
scale_fill_gradientn(
colours=c('darkblue','blue','white','red','darkred'),
na.value="transparent")+
ylab("")+xlab("")+labs(fill = "Std\nValue")+
geom_path(data=fortify(range),aes(y=lat,x=long,group=group),fill="green",col="black")+
ggcoast+gx+gy+coord_equal()
```
### Covariate correlation
Scatterplot matrix of the available environmental data.
```{r envCor}
splom(senv,varname.cex=2)
```
### _Annotate_ points with environmental data
```{r gridSpd}
fitdata=raster::extract(senv,fitdata,sp=T)
## Due to oppotunistic observations of the species, a few grid cells
## have more observations than trials. Let's remove them:
table(fitdata$presences>fitdata$trials)
fitdata=fitdata[fitdata$presences<=fitdata$trials,]
## omit rows with missing data (primarily ocean pixels)
fitdata2=na.omit(fitdata@data)
kable(head(fitdata2), format = "markdown")
```
Then transform the full gridded dataset into a `data.frame` with associated environmental data for predicting across space.
```{r spdReformat}
pdata=cbind.data.frame(
coordinates(senv),
values(senv))
colnames(pdata)[1:2]=c("lon","lat")
## omit rows with missing data (primarily ocean pixels)
pdata=na.omit(pdata)
kable(head(pdata), format = "markdown")
```
This table is similar to the data available from the "Annotate" function in MOL, with the exception that it contains the point data aggregated to the resolution of the environmental data.
## Model Comparison
Let's compare two models, one using interpolated precipitation and the other using satellite-derived cloud data.
```{r modelNames}
# Set number of chains to fit.
mods=data.frame(
model=c("m1","m2"),
formula=c("~cld+elev",
"~cld+cld_intra+elev*I(elev^2)+forest"),
name=c( "Cloud + Elevation",
"Full Model"))
kable(mods, format = "markdown")
```
Specify model run-lengths. _Real_ model runs generally require far more iterations (e.g. 10^3-10^5 samples) to achieve convergence and aquire a suitable sample. For now, we'll do a very short run:
```{r modelSetup}
burnin=200
mcmc=100
thin=1
```
## Fit the models
Both site-occupancy or ZIB models (with `hSDM.siteocc()` or `hSDM.ZIB()` functions respectively) can be used to model the presence-absence of a species taking into account imperfect detection.
The site-occupancy model can be used in all cases but can be less convenient and slower to fit when the repeated visits at each site are made under the same observation conditions. While this is likely not true in this situation (the observations occurred in different years, etc.), we'll use the simpler model today. For more information about the differences, see the hSDM Vignette Section 4.3.
### Example: `hSDM.ZIB`
The model integrates two processes, an ecological process associated to the presence or absence of the species due to habitat suitability and an observation process that takes into account the fact that
the probability of detection of the species is less than one.
If the species has been observed at least once during multiple visits, we can assert that the habitat at this site is suitable. And the fact that the species can be unobserved at this site is only due to imperfect detection.
**Ecological process:**
<img src="assets/M1.png" alt="Drawing" style="width: 300px;"/>
**Observation process:**
<img src="assets/M2.png" alt="Drawing" style="width: 300px;"/>
In this example we'll assume a spatially constant p(observation|presence), but it's also possible to put in covariates for this parameter.
## Run the model
```{r runmodel}
results=foreach(m=1:nrow(mods),.packages="hSDM") %dopar% {
## if foreach/doParallel are not installed, you can use this line instead
# for(m in 1:nrow(mods)) {
tres=hSDM.ZIB(
suitability=as.character(mods$formula[m]), #Formula for suitability
presences=fitdata2$presences, # Number of Observed Presences
observability="~1", # Formula for p(observation|presence)
trials=fitdata2$trials, # Number of Trials
data=fitdata2, # Covariates for fitting model
suitability.pred=pdata, # Covariates for prediction
mugamma=0, Vgamma=1.0E6, # Priors on Gamma
gamma.start=0, # Gamma initial Value
burnin=burnin, mcmc=mcmc, thin=thin, # MCMC parameters
beta.start=0, # Initial values for betas
mubeta=0, Vbeta=1.0E6, # Priors on Beta
save.p=0, # Don't save full posterior on p
verbose=1, # Report progress
seed=round(runif(1,0,1e6))) # Random seed
tres$model=mods$formula[m] # Add model formula to result
tres$modelname=mods$name[m] # Add model name to result
return(tres)
}
```
## Summarize posterior parameters
The model returns full posterior distributions for all model parameters. To summarize them you need to choose your summary metric (e.g. mean/median/quantiles).
```{r SummarizePosteriors}
params=foreach(r1=results,.combine=rbind.data.frame,.packages="coda")%do% {
data.frame(model=r1$model,
modelname=r1$modelname,
parameter=colnames(r1$mcmc),
mean=summary(r1$mcmc)$statistics[,"Mean"],
sd=summary(r1$mcmc)$statistics[,"SD"],
median=summary(r1$mcmc)$quantiles[,"50%"],
HPDinterval(mcmc(as.matrix(r1$mcmc))),
RejectionRate=rejectionRate(r1$mcmc))}
```
```{r PlotPosteriors, fig.height=8}
## plot it
ggplot(params[!grepl("Deviance*",rownames(params)),],
aes(x=mean,y=parameter,colour=modelname))+
geom_errorbarh(aes(xmin=lower,xmax=upper,height=.1),lwd=1.5)+
geom_point(size = 3)+xlab("Standardized Coefficient")+
ylab("Parameter")+
theme(legend.position="bottom")
```
### Detection probability
The model uses repeat obserations within cells to estimate the probabiliy observation given that the species was present.
```{r pDetect}
pDetect <- params[params$parameter=="gamma.(Intercept)",
c("modelname","mean")]
pDetect$delta.est <- inv.logit(pDetect$mean)
colnames(pDetect)[2]="gamma.hat"
kable(pDetect,row.names=F, format = "markdown")
```
> How does this change if you add environmental covariates to the observability regression?
## Predictions for each cell
```{r pPred}
pred=foreach(r1=results,.combine=stack,.packages="raster")%dopar% {
tr=rasterFromXYZ(cbind(x=pdata$lon,
y=pdata$lat,
pred=r1$prob.p.pred))
names(tr)=r1$modelname
return(tr)
}
```
Compare the model predictions
```{r plotmodel,warning=FALSE,message=FALSE}
predscale=scale_fill_gradientn(values=c(0,.5,1),colours=c('white','darkgreen','green'),na.value="transparent")
gplot(pred,maxpixels=1e5)+geom_raster(aes(fill=value)) +
facet_wrap(~ variable) +
predscale+
coord_equal()+
geom_path(data=fortify(range),
aes(y=lat,x=long,group=group),
fill="red",col="red")+
geom_point(data=fitdata@data[fitdata$presences==0,],
aes(x=lon,y=lat,fill=1),pch=1,col="black",cex=.8,lwd=2,alpha=.3)+
geom_point(data=fitdata@data[fitdata$presences>=1,],
aes(x=lon,y=lat,fill=1),pch=3,col="red",cex=2,lwd=3,alpha=1)+
ggcoast+gx+gy+ylab("Latitude")+xlab("Longitude")+
labs(col = "p(presence)")+
coord_equal()
```
## Model selection
Model selection is an extremely important component of any modeling excercise. See [Hooten and Hobbs (2015)](http://www.esajournals.org/doi/abs/10.1890/14-0661.1) for a recent review of various methods.
One metric for Bayesian model evaluation is the Deviance Information Criterion (DIC), which assess model fit penalized by model complexity (similar to the AIC). For more information, see [Gelman, Andrew, Jessica Hwang, and Aki Vehtari. 2013. _Understanding Predictive Information Criteria for Bayesian Models_](http://www.stat.columbia.edu/~gelman/research/unpublished/waic_understand.pdf).
Calculate DIC for the two models
```{r DIC}
eval=cbind.data.frame(Model=mods$model,
Deviance=params$mean[
grepl("Deviance",rownames(params))],
Pd=(params$sd[
grepl("Deviance",rownames(params))]^2)/2
# Gelman BDA pg 182
# http://www.stat.columbia.edu/~gelman/book/
)
# add the DIC
eval$DIC=eval$Deviance+eval$Pd
# print the table
kable(eval,row.names=F, format = "markdown")
```
## Additional Models in hSDM
`*.icar`
The `*.icar` functions in `hSDM` add _spatial effects_ to the model as well, accounting for spatial autocorrelation of species occurrence.
`hSDM.binomial` & `hSDM.binomial.iCAR`
Simple and spatial binomial model (perfect detection).
`hSDM.ZIB` & `hSDM.ZIB.iCAR` & `hSDM.ZIB.iCAR.alteration`
Zero-inflated Binomial (example we used today).
`hSDM.ZIP` & `hSDM.ZIP.iCAR` & `hSDM.ZIP.iCAR.alteration`
Zero-inflated Poisson (Abundance data with imperfect detection).
`hSDM.siteocc` & `hSDM.siteocc.iCAR`
Incorporates temporally varying environment to account for changing observation conditions.
`hSDM.poisson` & `hSDM.poisson.iCAR`
Simple and spatial poisson model for species abundance (perfect detection).
`hSDM.Nmixture` & `hSDM.Nmixture.iCAR`
Poisson model for abundance with imperfect detection.
## Looking forward
* Incorporate multiple scales of observations (e.g. points & polygons)
* Account directly for spatial uncertainties in point observations
* Time-varying covariates with `hSDM.siteocc` or similar