Function for prediction at new locations for multi-season single-species integrated occupancy models
predict.tIntPGOcc.Rd
The function predict
collects posterior predictive samples for a set of new locations given an object of class `tIntPGOcc`. Prediction is currently only possible for the latent occupancy state. Predictions are currently only possible for sampled primary time periods.
Usage
# S3 method for tIntPGOcc
predict(object, X.0, t.cols, ignore.RE = FALSE, type = 'occupancy', ...)
Arguments
- object
an object of class tIntPGOcc
- X.0
the design matrix of covariates at the prediction locations. This should be a three-dimensional array, with dimensions corresponding to site, primary time period, and covariate, respectively. Note that the first covariate should consist of all 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/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data intIntPGOcc
. The covariates should be organized in the same order as they were specified in the corresponding formula argument oftIntPGOcc
. Names of the third dimension (covariates) of any random effects in X.0 must match the name of the random effects used to fit the model, if specified in the corresponding formula argument oftIntPGOcc
. See example below.- t.cols
an indexing vector used to denote which primary time periods are contained in the design matrix of covariates at the prediction locations (
X.0
). The values should denote the specific primary time periods used to fit the model. The values should indicate the columns indata$y
used to fit the model for which prediction is desired. See example below.- ignore.RE
logical value that specifies whether or not to remove random unstructured occurrence (or detection if
type = 'detection'
) effects from the subsequent predictions. IfTRUE
, unstructured random effects will be included. IfFALSE
, unstructured random effects will be set to 0 and predictions will only be generated from the fixed effects and AR(1) random effects if the model was fit withar1 = TRUE
.- 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. Detection prediction is not currently supported for integrated models.
- ...
currently no additional arguments
Note
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of unstructured 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.
Occurrence predictions at sites that are only sampled for a subset of the total number of primary time periods are obtained directly when fitting the model. See the psi.samples
and z.samples
portions of the output list from the model object of class tIntPGOcc
.
Author
Jeffrey W. Doser doserjef@msu.edu
Value
A list object of class predict.tIntPGOcc
. When type = 'occupancy'
, the list consists of:
- psi.0.samples
a three-dimensional object of posterior predictive samples for the latent occupancy probability values with dimensions corresponding to posterior predictive sample, site, and primary time period.
- z.0.samples
a three-dimensional object of posterior predictive samples for the latent occupancy values with dimensions corresponding to posterior predictive sample, site, and primary time period.
The return object will include additional objects used for standard extractor functions.
Examples
set.seed(332)
# Simulate Data -----------------------------------------------------------
# Number of locations in each direction. This is the total region of interest
# where some sites may or may not have a data source.
J.x <- 15
J.y <- 15
J.all <- J.x * J.y
# Number of data sources.
n.data <- 3
# Sites for each data source.
J.obs <- sample(ceiling(0.2 * J.all):ceiling(0.4 * J.all), n.data, replace = TRUE)
# Maximum number of years for each data set
n.time.max <- c(4, 8, 10)
# Number of years each site in each data set is sampled
n.time <- list()
for (i in 1:n.data) {
n.time[[i]] <- sample(1:n.time.max[i], J.obs[i], replace = TRUE)
}
# Replicates for each data source.
n.rep <- list()
for (i in 1:n.data) {
n.rep[[i]] <- matrix(NA, J.obs[i], n.time.max[i])
for (j in 1:J.obs[i]) {
n.rep[[i]][j, sample(1:n.time.max[i], n.time[[i]][j], replace = FALSE)] <-
sample(1:4, n.time[[i]][j], replace = TRUE)
}
}
# Total number of years across all data sets
n.time.total <- 10
# List denoting the specific years each data set was sampled during.
data.seasons <- list()
for (i in 1:n.data) {
data.seasons[[i]] <- sort(sample(1:n.time.total, n.time.max[i], replace = FALSE))
}
# Occupancy covariates
beta <- c(0, 0.4, 0.3)
trend <- TRUE
# Random occupancy covariates
psi.RE <- list()
p.occ <- length(beta)
# Detection covariates
alpha <- list()
alpha[[1]] <- c(0, 0.2, -0.5)
alpha[[2]] <- c(-1, 0.5, 0.3, -0.8)
alpha[[3]] <- c(-0.5, 1)
p.RE <- list()
p.det.long <- sapply(alpha, length)
p.det <- sum(p.det.long)
# Simulate occupancy data.
dat <- simTIntOcc(n.data = n.data, J.x = J.x, J.y = J.y, J.obs = J.obs,
n.time = n.time, data.seasons = data.seasons, n.rep = n.rep,
beta = beta, alpha = alpha, trend = trend,
psi.RE = psi.RE, p.RE = p.RE)
y <- dat$y
X <- dat$X.obs
X.p <- dat$X.p
sites <- dat$sites
# Package all data into a list
occ.covs <- list(trend = X[, , 2],
occ.cov.1 = X[, , 3])
det.covs <- list()
# Add covariates one by one
det.covs[[1]] <- list(det.cov.1.1 = X.p[[1]][, , , 2],
det.cov.1.2 = X.p[[1]][, , , 3])
det.covs[[2]] <- list(det.cov.2.1 = X.p[[2]][, , , 2],
det.cov.2.2 = X.p[[2]][, , , 3],
det.cov.2.3 = X.p[[2]][, , , 4])
det.covs[[3]] <- list(det.cov.3.1 = X.p[[3]][, , , 2])
data.list <- list(y = y, occ.covs = occ.covs, det.covs = det.covs,
sites = sites, seasons = data.seasons)
# Testing
occ.formula <- ~ trend + occ.cov.1
# Note that the names are not necessary.
det.formula <- list(f.1 = ~ det.cov.1.1 + det.cov.1.2,
f.2 = ~ det.cov.2.1 + det.cov.2.2 + det.cov.2.3,
f.3 = ~ det.cov.3.1)
# NOTE: this is a short run of the model, in reality we would run the
# model for much longer.
out <- tIntPGOcc(occ.formula = occ.formula,
det.formula = det.formula,
data = data.list,
n.batch = 3,
batch.length = 25,
n.report = 1,
n.burn = 25,
n.thin = 1,
n.chains = 1)
#> ----------------------------------------
#> Preparing the data
#> ----------------------------------------
#> No prior specified for beta.normal.
#> Setting prior mean to 0 and prior variance to 2.72
#> No prior specified for alpha.normal.
#> Setting prior mean to 0 and prior variance to 2.72
#> z is not specified in initial values.
#> Setting initial values based on observed data
#> beta is not specified in initial values.
#> Setting initial values to random values from the prior distribution
#> alpha is not specified in initial values.
#> Setting initial values to random values from the prior distribution
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Integrated multi-season Occupancy Model with Polya-Gamma latent variable
#> fit with 157 sites and 10 primary time periods.
#>
#> Integrating 3 occupancy data sets.
#>
#> Samples per chain: 75 (3 batches of length 25)
#> Burn-in: 25
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 50
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Batch: 1 of 3, 33.33%
#> -------------------------------------------------
#> Batch: 2 of 3, 66.67%
#> -------------------------------------------------
#> Batch: 3 of 3, 100.00%
t.cols <- 1:n.time.total
out.pred <- predict(out, X.0 = dat$X.pred, t.cols = t.cols,
type = 'occupancy')
str(out.pred)
#> List of 5
#> $ psi.0.samples: num [1:50, 1:68, 1:10] 0.358 0.319 0.318 0.372 0.312 ...
#> $ z.0.samples : int [1:50, 1:68, 1:10] 1 1 0 1 0 1 0 1 1 0 ...
#> $ run.time : 'proc_time' Named num [1:5] 0.002 0.003 0.002 0 0
#> ..- attr(*, "names")= chr [1:5] "user.self" "sys.self" "elapsed" "user.child" ...
#> $ call : language predict.tPGOcc(object = object, X.0 = X.0, t.cols = t.cols, ignore.RE = ignore.RE, type = type)
#> $ object.class : chr "tIntPGOcc"
#> - attr(*, "class")= chr "predict.tIntPGOcc"