-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpred-full.R
61 lines (58 loc) · 3.05 KB
/
pred-full.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
# pred-full.R: script to predict occupancy probability of Grasshopper
# Sparrow across the US from 1970-2019, and extract the SVC
# effects for subsequent mapping.
# Author: Jeffrey W. Doser
rm(list = ls())
library(spOccupancy)
# Load the data used to fit the model -------------------------------------
load('data/case-study-2/GRSP-spOcc-data.rda')
# Load the model fit object -----------------------------------------------
load('results/case-study-2/full-GRSP-model-results.rda')
# Load the prediction data ------------------------------------------------
load('data/case-study-2/GRSP-pred-data.rda')
# Standardize/scale variables by those used to fit the model
J.0 <- nrow(coords.0)
n.time <- ncol(out$X)
X.0 <- array(1, dim = c(J.0, ncol(out$X), dim(out$X)[3]))
dimnames(X.0)[[3]] <- c('(Intercept)', 'grass.dev', 'crop.dev',
'scale(tmax)', 'I(scale(tmax)^2)', 'scale(grass.mean)',
'scale(crop.mean)', 'grass.dev:scale(grass.mean)',
'crop.dev:scale(crop.mean)', 'grass.dev:scale(tmax)',
'crop.dev:scale(tmax)')
# Get means and sds of values used to fit model
grass.dev.val <- abs(min(data.GRSP$occ.covs$grass.dev))
crop.dev.val <- abs(min(data.GRSP$occ.covs$crop.dev))
tmax.mean <- mean(data.GRSP$occ.covs$tmax)
tmax.sd <- sd(data.GRSP$occ.covs$tmax)
grass.mean <- mean(data.GRSP$occ.covs$grass.mean)
grass.sd <- sd(data.GRSP$occ.covs$grass.mean)
crop.mean <- mean(data.GRSP$occ.covs$crop.mean)
crop.sd <- sd(data.GRSP$occ.covs$crop.mean)
for (t in 1:n.time) {
X.0[, t, 'grass.dev'] <- occ.pred.covs$grass.dev[, t] + grass.dev.val
X.0[, t, 'crop.dev'] <- occ.pred.covs$crop.dev[, t] + crop.dev.val
X.0[, t, 'scale(tmax)'] <- (occ.pred.covs$tmax - tmax.mean) / tmax.sd
X.0[, t, 'I(scale(tmax)^2)'] <- X.0[, t, 'scale(tmax)']^2
X.0[, t, 'scale(grass.mean)'] <- (occ.pred.covs$grass.mean - grass.mean) / grass.sd
X.0[, t, 'scale(crop.mean)'] <- (occ.pred.covs$crop.mean - crop.mean) / crop.sd
X.0[, t, 'grass.dev:scale(grass.mean)'] <- X.0[, t, 'grass.dev'] * X.0[, t, 'scale(grass.mean)']
X.0[, t, 'crop.dev:scale(crop.mean)'] <- X.0[, t, 'crop.dev'] * X.0[, t, 'scale(crop.mean)']
X.0[, t, 'grass.dev:scale(tmax)'] <- X.0[, t, 'grass.dev'] * X.0[, t, 'scale(tmax)']
X.0[, t, 'crop.dev:scale(tmax)'] <- X.0[, t, 'crop.dev'] * X.0[, t, 'scale(tmax)']
}
vals <- split(1:J.0, ceiling(seq_along(1:J.0) / 500))
n.samples <- out$n.post * out$n.chains
p.svc <- length(out$svc.cols)
n.time <- ncol(X.0)
w.samples <- array(NA, dim = c(n.samples, p.svc, J.0))
psi.mean.samples <- array(NA, dim = c(n.samples, J.0))
for (j in 1:length(vals)) {
print(paste("Currently on set ", j, " out of ", length(vals), sep = ''))
curr.indx <- vals[[j]]
out.pred <- predict(out, X.0[curr.indx, , ], coords.0[curr.indx, ],
t.cols = 1:n.time, n.omp.threads = 10, verbose = FALSE)
w.samples[, , curr.indx] <- out.pred$w.0.samples
psi.mean.samples[, curr.indx] <- apply(out.pred$psi.0.samples, c(1, 2), mean)
}
save(w.samples, file = 'results/case-study-2/full-pred-results.rda')
save(psi.mean.samples, file = 'results/case-study-2/full-psi-pred-results.rda')