Function for prediction at new locations for multi-species spatial occupancy models
predict.spMsPGOcc.Rd
The function predict
collects posterior predictive samples for a set of new locations given an object of class `spMsPGOcc`. Prediction is possible for both the latent occupancy state as well as detection.
Usage
# S3 method for spMsPGOcc
predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE,
n.report = 100, ignore.RE = FALSE, type = 'occupancy', ...)
Arguments
- object
an object of class spMsPGOcc
- X.0
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the occupancy (or detection if
type = 'detection'
) portion of the model, the levels of the random effects at the new locations should be included as a column in the design matrix. The ordering of the levels should match the ordering used to fit the data inspMsPGOcc
. Columns should correspond to the order of how covariates were specified in the corresponding formula argument ofspMsPGOcc
. Column names of the random effects must match the name of the random effects, if specified in the corresponding formula argument ofspMsPGOcc
.- coords.0
the spatial coordinates corresponding to
X.0
. Note thatspOccupancy
assumes coordinates are specified in a projected coordinate system.- n.omp.threads
a positive integer indicating the number of threads to use for SMP parallel processing. The package must be compiled for OpenMP support. For most Intel-based machines, we recommend setting
n.omp.threads
up to the number of hyperthreaded cores. Note,n.omp.threads
> 1 might not work on some systems.- verbose
if
TRUE
, model specification and progress of the sampler is printed to the screen. Otherwise, nothing is printed to the screen.- n.report
the interval to report sampling progress.
- ignore.RE
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, unstructured random effects will be ignored and prediction will only use the fixed effects and the spatial random effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the unstructured random effects.
- type
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'occupancy' to predict latent occupancy probability and latent occupancy values (this is the default), or 'detection' to predict detection probability given new values of detection covariates.
- ...
currently no additional arguments
Note
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Author
Jeffrey W. Doser doserjef@msu.edu,
Andrew O. Finley finleya@msu.edu
Value
An list object of class predict.spMsPGOcc
. When type = 'occupancy'
, the list consists of:
- psi.0.samples
a three-dimensional array of posterior predictive samples for the latent occurrence probability values.
- z.0.samples
a three-dimensional array of posterior predictive samples for the latent occurrence values.
- w.0.samples
a three-dimensional array of posterior predictive samples for the latent spatial random effects.
- run.time
execution time reported using
proc.time()
.
When type = 'detection'
, the list consists of:
- p.0.samples
a three-dimensional array of posterior predictive samples for the detection probability values.
- run.time
execution time reported using
proc.time()
.
The return object will include additional objects used for standard extractor functions.
Examples
set.seed(400)
# Simulate Data -----------------------------------------------------------
J.x <- 7
J.y <- 7
J <- J.x * J.y
n.rep <- sample(2:4, size = J, replace = TRUE)
N <- 5
# Community-level covariate effects
# Occurrence
beta.mean <- c(0.2, -0.15)
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, 0.3)
# Detection
alpha.mean <- c(0.5, 0.2, -.2)
tau.sq.alpha <- c(0.2, 0.3, 0.8)
p.det <- length(alpha.mean)
# Draw species-level effects from community means.
beta <- matrix(NA, nrow = N, ncol = p.occ)
alpha <- matrix(NA, nrow = N, ncol = p.det)
for (i in 1:p.occ) {
beta[, i] <- rnorm(N, beta.mean[i], sqrt(tau.sq.beta[i]))
}
for (i in 1:p.det) {
alpha[, i] <- rnorm(N, alpha.mean[i], sqrt(tau.sq.alpha[i]))
}
phi <- runif(N, 3/1, 3/.4)
sigma.sq <- runif(N, 0.3, 3)
sp <- TRUE
dat <- simMsOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, N = N, beta = beta, alpha = alpha,
phi = phi, sigma.sq = sigma.sq, sp = TRUE, cov.model = 'exponential')
# Number of batches
n.batch <- 30
# Batch length
batch.length <- 25
n.samples <- n.batch * batch.length
# Split into fitting and prediction data set
pred.indx <- sample(1:J, round(J * .25), replace = FALSE)
y <- dat$y[, -pred.indx, ]
# Occupancy covariates
X <- dat$X[-pred.indx, ]
# Coordinates
coords <- as.matrix(dat$coords[-pred.indx, ])
# Detection covariates
X.p <- dat$X.p[-pred.indx, , ]
# Prediction values
X.0 <- dat$X[pred.indx, ]
coords.0 <- as.matrix(dat$coords[pred.indx, ])
psi.0 <- dat$psi[, pred.indx]
# Package all data into a list
occ.covs <- X[, 2, drop = FALSE]
colnames(occ.covs) <- c('occ.cov')
det.covs <- list(det.cov.1 = X.p[, , 2],
det.cov.2 = X.p[, , 3]
)
data.list <- list(y = y,
occ.covs = occ.covs,
det.covs = det.covs,
coords = coords)
# Priors
prior.list <- list(beta.comm.normal = list(mean = 0, var = 2.72),
alpha.comm.normal = list(mean = 0, var = 2.72),
tau.sq.beta.ig = list(a = 0.1, b = 0.1),
tau.sq.alpha.ig = list(a = 0.1, b = 0.1),
phi.unif = list(a = 3/1, b = 3/.1),
sigma.sq.ig = list(a = 2, b = 2))
# Starting values
inits.list <- list(alpha.comm = 0,
beta.comm = 0,
beta = 0,
alpha = 0,
tau.sq.beta = 1,
tau.sq.alpha = 1,
phi = 3 / .5,
sigma.sq = 2,
w = matrix(0, nrow = N, ncol = nrow(X)),
z = apply(y, c(1, 2), max, na.rm = TRUE))
# Tuning
tuning.list <- list(phi = 1)
out <- spMsPGOcc(occ.formula = ~ occ.cov,
det.formula = ~ det.cov.1 + det.cov.2,
data = data.list,
inits = inits.list,
n.batch = n.batch,
batch.length = batch.length,
accept.rate = 0.43,
priors = prior.list,
cov.model = "exponential",
tuning = tuning.list,
n.omp.threads = 1,
verbose = TRUE,
NNGP = TRUE,
n.neighbors = 5,
search.type = 'cb',
n.report = 10,
n.burn = 500,
n.thin = 1)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> NNGP Multi-species Occupancy Model with Polya-Gamma latent
#> variable fit with 37 sites and 5 species.
#>
#> Samples per chain: 750 (30 batches of length 25)
#> Burn-in: 500
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 250
#>
#> Using the exponential spatial correlation model.
#>
#> Using 5 nearest neighbors.
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> Adaptive Metropolis with target acceptance rate: 43.0
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Batch: 10 of 30, 33.33%
#> Species Parameter Acceptance Tuning
#> 1 phi 68.0 1.11628
#> 2 phi 52.0 1.11628
#> 3 phi 72.0 1.11628
#> 4 phi 76.0 1.11628
#> 5 phi 64.0 1.11628
#> -------------------------------------------------
#> Batch: 20 of 30, 66.67%
#> Species Parameter Acceptance Tuning
#> 1 phi 64.0 1.23368
#> 2 phi 72.0 1.23368
#> 3 phi 76.0 1.23368
#> 4 phi 64.0 1.23368
#> 5 phi 68.0 1.23368
#> -------------------------------------------------
#> Batch: 30 of 30, 100.00%
summary(out, level = 'both')
#>
#> Call:
#> spMsPGOcc(occ.formula = ~occ.cov, det.formula = ~det.cov.1 +
#> det.cov.2, data = data.list, inits = inits.list, priors = prior.list,
#> tuning = tuning.list, cov.model = "exponential", NNGP = TRUE,
#> n.neighbors = 5, search.type = "cb", n.batch = n.batch, batch.length = batch.length,
#> accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10,
#> n.burn = 500, n.thin = 1)
#>
#> Samples per Chain: 750
#> Burn-in: 500
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 250
#> Run Time (min): 0.0099
#>
#> ----------------------------------------
#> Community Level
#> ----------------------------------------
#> Occurrence Means (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.4622 0.4427 -1.3665 -0.5057 0.2814 NA 122
#> occ.cov 0.3886 0.4287 -0.5755 0.3795 1.1836 NA 77
#>
#> Occurrence Variances (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.6554 1.1049 0.0469 0.3317 5.5581 NA 64
#> occ.cov 0.7827 1.1321 0.0412 0.4286 3.7263 NA 88
#>
#> Detection Means (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.7224 0.4123 -0.0629 0.7256 1.5427 NA 86
#> det.cov.1 -0.1478 0.5247 -0.9811 -0.1644 0.8437 NA 169
#> det.cov.2 -0.6750 0.5438 -1.6651 -0.6442 0.4714 NA 250
#>
#> Detection Variances (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.6929 0.8467 0.0638 0.4020 3.2757 NA 36
#> det.cov.1 1.6185 3.0210 0.0945 0.8538 7.6464 NA 114
#> det.cov.2 1.6717 2.4921 0.1242 1.0001 6.9050 NA 162
#>
#> ----------------------------------------
#> Species Level
#> ----------------------------------------
#> Occurrence (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 -0.3694 0.4666 -1.3527 -0.4014 0.5434 NA 64
#> (Intercept)-sp2 0.1296 0.3753 -0.5420 0.1129 0.8152 NA 101
#> (Intercept)-sp3 -0.7903 0.5282 -2.0046 -0.7108 0.1381 NA 50
#> (Intercept)-sp4 -0.7650 0.4484 -1.7566 -0.7360 0.0782 NA 80
#> (Intercept)-sp5 -0.6734 0.4096 -1.5578 -0.6384 0.0518 NA 101
#> occ.cov-sp1 -0.0413 0.4951 -1.1310 0.0115 0.8520 NA 73
#> occ.cov-sp2 0.5945 0.4041 -0.2357 0.5854 1.4573 NA 101
#> occ.cov-sp3 -0.1653 0.5898 -1.3566 -0.1781 0.9947 NA 46
#> occ.cov-sp4 0.7727 0.5037 -0.0445 0.7268 1.9130 NA 72
#> occ.cov-sp5 0.8413 0.4658 0.0764 0.7759 1.8499 NA 81
#>
#> Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 0.8876 0.3579 0.1956 0.8738 1.6152 NA 66
#> (Intercept)-sp2 1.3182 0.3622 0.7439 1.2933 2.0585 NA 43
#> (Intercept)-sp3 0.0269 0.6466 -1.4559 0.0952 1.1080 NA 28
#> (Intercept)-sp4 0.5653 0.3957 -0.2553 0.5861 1.2771 NA 166
#> (Intercept)-sp5 0.9058 0.3408 0.2357 0.8734 1.5581 NA 74
#> det.cov.1-sp1 -0.0293 0.2950 -0.5720 -0.0299 0.5518 NA 191
#> det.cov.1-sp2 -0.5497 0.3369 -1.2268 -0.5430 0.1430 NA 81
#> det.cov.1-sp3 1.0452 0.5574 0.1501 1.0092 2.3902 NA 49
#> det.cov.1-sp4 -1.0507 0.5133 -2.0604 -1.0211 -0.2111 NA 79
#> det.cov.1-sp5 -0.2745 0.3472 -1.0018 -0.2907 0.3592 NA 131
#> det.cov.2-sp1 -0.0504 0.3465 -0.6895 -0.0429 0.6142 NA 157
#> det.cov.2-sp2 -0.7805 0.3400 -1.4132 -0.7845 -0.1564 NA 115
#> det.cov.2-sp3 -2.1822 0.8084 -3.7653 -2.1169 -0.8894 NA 35
#> det.cov.2-sp4 -0.5711 0.3753 -1.3250 -0.5649 0.2372 NA 155
#> det.cov.2-sp5 -0.3943 0.3222 -0.9939 -0.3695 0.1664 NA 157
#>
#> Spatial Covariance:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> sigma.sq-sp1 3.5431 3.3181 0.5505 2.4245 12.3978 NA 10
#> sigma.sq-sp2 1.1110 0.8678 0.2592 0.8772 3.8971 NA 17
#> sigma.sq-sp3 2.7520 4.0084 0.3399 1.2134 15.3034 NA 7
#> sigma.sq-sp4 1.6086 1.2170 0.4699 1.1758 5.1140 NA 26
#> sigma.sq-sp5 1.4824 1.1701 0.3043 1.1938 4.5265 NA 44
#> phi-sp1 10.5326 6.6557 3.2131 7.9822 24.9551 NA 24
#> phi-sp2 17.7871 7.5907 4.3769 17.7565 29.7557 NA 24
#> phi-sp3 16.9545 6.9623 3.9358 17.3553 28.4260 NA 21
#> phi-sp4 18.5204 7.7661 4.1596 19.3904 29.6975 NA 14
#> phi-sp5 15.1225 7.8533 3.6180 14.0047 28.5701 NA 21
# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0, verbose = FALSE)