forked from doserjef/Doser_et_al_2024_JABES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-univariate-interaction.R
70 lines (63 loc) · 2.65 KB
/
main-univariate-interaction.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
# main-univariate-interaction.R: script to fit univariate models with
# an interaction between maximum temperature
# and grassland
# Author: Jeffrey W. Doser
rm(list = ls())
library(spOccupancy)
# Get info from command line ----------------------------------------------
# This code is to extract the current species name from the command line
# to easily run the script for different species
args <- commandArgs(trailingOnly = TRUE)
# Current species
curr.sp <- args[1]
# Alternatively, if not running the script from the command line:
# curr.sp <- 'LCSP'
if(length(args) == 0) base::stop('Need to tell spOccupancy the current species')
print(curr.sp)
# Read in data ------------------------------------------------------------
load('data/full-data-spOccupancy.rda')
sp.names <- dimnames(data.list$y)[[1]]
sp.indx <- which(sp.names == curr.sp)
site.indx <- which(data.list$range.ind[sp.indx, ] == 1)
data.list$y <- data.list$y[sp.indx, site.indx, ]
data.list$occ.covs <- data.list$occ.covs[site.indx, ]
data.list$det.covs$day <- data.list$det.covs$day[site.indx]
data.list$det.covs$obs <- data.list$det.covs$obs[site.indx]
data.list$det.covs$stop <- data.list$det.covs$stop[site.indx, ]
data.list$coords <- data.list$coords[site.indx, ]
# Run the model -----------------------------------------------------------
# Change as desired
n.batch <- 8000
n.burn <- 100000
n.thin <- 100
n.chains <- 3
# Set priors and initial values.
tuning.list <- list(phi = 0.3, sigma.sq = 0.5)
dist.bbs <- dist(data.list$coords)
min.dist <- quantile(dist.bbs, 0.05)
max.dist <- max(dist.bbs)
mean.dist <- mean(dist.bbs)
inits <- list(phi = 3 / mean.dist, sigma.sq = 1, sigma.sq.p = 1)
priors <- list(phi.unif = c(a = 3 / max.dist, b = 3 / min.dist))
out <- spPGOcc(occ.formula = ~ scale(grass) * scale(tmax),
det.formula = ~ scale(day) + I(scale(day)^2) +
factor(stop),
data = data.list,
inits = inits,
priors = priors,
n.batch = n.batch,
batch.length = 25,
accept.rate = 0.43,
cov.model = "exponential",
tuning = tuning.list,
n.omp.threads = 1, # Change as desired
verbose = TRUE,
NNGP = TRUE,
n.neighbors = 10,
n.report = 10,
n.burn = n.burn,
n.thin = n.thin,
n.chains = n.chains)
# Save to hard drive ------------------------------------------------------
save(out, file = paste('results/univariate-interaction/', curr.sp, '-univariate-interaction-results.rda',
sep = ''))