Skip to contents

The function predict collects posterior predictive samples for a set of new locations given an object of class `sfMsPGOcc`. Prediction is possible for both the latent occupancy state as well as detection.

Usage

# S3 method for sfMsPGOcc
predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, 
        n.report = 100, ignore.RE = FALSE, type = 'occupancy', grid.index.0, ...)

Arguments

object

an object of class sfMsPGOcc

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 in sfMsPGOcc. Columns should correspond to the order of how covariates were specified in the corresponding formula argument of sfMsPGOcc. Column names of the random effects must match the name of the random effects, if specified in the corresponding formula argument of sfMsPGOcc.

coords.0

the spatial coordinates corresponding to X.0. Note that spOccupancy 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.

grid.index.0

an indexing vector used to specify how each row in X.0 corresponds to the coordinates specified in coords.0. Only relevant if the spatial random effect was estimated at a higher spatial resolution (e.g., grid cells) than point locations.

...

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.sfMsPGOcc. 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 factors.

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]))
}
n.factors <- 3
phi <- runif(n.factors, 3/1, 3/.4)
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', 
                factor.model = TRUE, n.factors = n.factors)
#> sigma.sq is specified but will be set to 1 for spatial latent factor model

# Number of batches
n.batch <- 10
# 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
lambda.inits <- matrix(0, N, n.factors)
diag(lambda.inits) <- 1
lambda.inits[lower.tri(lambda.inits)] <- rnorm(sum(lower.tri(lambda.inits)))
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,
                   lambda = lambda.inits,
                   z = apply(y, c(1, 2), max, na.rm = TRUE))
# Tuning
tuning.list <- list(phi = 1) 

out <- sfMsPGOcc(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, 
                 n.factors = 3,
                 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 = 100, 
                 n.thin = 1)
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> 	Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> Spatial Factor NNGP Multi-species Occupancy Model with Polya-Gamma latent
#> variable fit with 37 sites and 5 species.
#> 
#> Samples per chain: 250 (10 batches of length 25)
#> Burn-in: 100 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 150 
#> 
#> Using the exponential spatial correlation model.
#> 
#> Using 3 latent spatial factors.
#> 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 10, 100.00%

summary(out, level = 'both')
#> 
#> Call:
#> sfMsPGOcc(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.factors = 3, n.batch = n.batch, 
#>     batch.length = batch.length, accept.rate = 0.43, n.omp.threads = 1, 
#>     verbose = TRUE, n.report = 10, n.burn = 100, n.thin = 1)
#> 
#> Samples per Chain: 250
#> Burn-in: 100
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 150
#> Run Time (min): 0.0039
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Occurrence Means (logit scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1279 0.4809 -0.5962 0.1139 1.0244   NA 107
#> occ.cov     0.1500 0.3667 -0.5559 0.1252 0.8037   NA  33
#> 
#> Occurrence Variances (logit scale): 
#>               Mean    SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.8961 1.901 0.0475 0.3950 4.9997   NA  85
#> occ.cov     0.3125 0.353 0.0454 0.1914 1.4483   NA  32
#> 
#> Detection Means (logit scale): 
#>                Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept)  0.8142 0.3624  0.0958  0.8084 1.6055   NA  29
#> det.cov.1   -0.1869 0.3415 -0.8131 -0.2243 0.6684   NA  88
#> det.cov.2   -0.4910 0.4105 -1.2354 -0.5374 0.3462   NA  74
#> 
#> Detection Variances (logit scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.4905 0.5251 0.0568 0.3149 2.1140   NA  82
#> det.cov.1   0.7193 1.0831 0.0887 0.4034 3.4251   NA  83
#> det.cov.2   0.6256 1.0837 0.0444 0.2575 3.0426   NA  67
#> 
#> ----------------------------------------
#> 	Species Level
#> ----------------------------------------
#> Occurrence (logit scale): 
#>                    Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept)-sp1  0.6715 0.3761 -0.1709  0.6961 1.3156   NA  55
#> (Intercept)-sp2  0.5882 0.5051 -0.2428  0.5514 1.7071   NA  55
#> (Intercept)-sp3  0.1843 0.5041 -0.8952  0.1968 1.1181   NA  48
#> (Intercept)-sp4 -0.2304 0.4729 -1.2982 -0.2257 0.6187   NA  31
#> (Intercept)-sp5 -0.3415 0.4893 -1.4413 -0.2936 0.4630   NA  37
#> occ.cov-sp1     -0.0575 0.3364 -0.7133 -0.0834 0.6045   NA  39
#> occ.cov-sp2      0.1964 0.3867 -0.5235  0.2169 0.9118   NA  58
#> occ.cov-sp3      0.1423 0.3432 -0.4604  0.1385 0.7831   NA  59
#> occ.cov-sp4      0.4982 0.4274 -0.1825  0.4545 1.5038   NA  26
#> occ.cov-sp5      0.0507 0.4105 -0.8291  0.0599 0.7582   NA  45
#> 
#> Detection (logit scale): 
#>                    Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept)-sp1  1.0610 0.2554  0.5744  1.0227  1.5660   NA 146
#> (Intercept)-sp2  0.9551 0.2835  0.3985  0.9510  1.4898   NA  91
#> (Intercept)-sp3  0.0828 0.3857 -0.7113  0.0492  0.7735   NA  30
#> (Intercept)-sp4  0.8353 0.3202  0.1672  0.8423  1.4155   NA  33
#> (Intercept)-sp5  1.1804 0.3386  0.5211  1.1917  1.7924   NA  63
#> det.cov.1-sp1   -0.0049 0.2958 -0.5699 -0.0183  0.5093   NA  72
#> det.cov.1-sp2   -0.7877 0.2645 -1.2454 -0.7849 -0.3337   NA  45
#> det.cov.1-sp3    0.4804 0.2846 -0.0461  0.4732  1.0194   NA  86
#> det.cov.1-sp4   -0.5717 0.3807 -1.5004 -0.5610  0.0679   NA  44
#> det.cov.1-sp5   -0.3540 0.3291 -0.9798 -0.3291  0.1931   NA  53
#> det.cov.2-sp1   -0.4193 0.2798 -0.9672 -0.3734  0.0796   NA  80
#> det.cov.2-sp2   -0.3318 0.2617 -0.8557 -0.3387  0.1666   NA  75
#> det.cov.2-sp3   -1.1993 0.4123 -2.2382 -1.1685 -0.4733   NA  50
#> det.cov.2-sp4   -0.5172 0.3994 -1.3174 -0.4779  0.2290   NA  61
#> det.cov.2-sp5   -0.3699 0.4016 -1.1606 -0.4059  0.4463   NA  54
#> 
#> ----------------------------------------
#> 	Spatial Covariance
#> ----------------------------------------
#>          Mean     SD   2.5%     50%   97.5% Rhat ESS
#> phi-1 14.4708 7.4146 3.5139 13.0261 29.2457   NA  15
#> phi-2 19.7087 8.2989 6.8214 19.3789 29.9599   NA   5
#> phi-3 15.4539 7.0912 4.5355 16.4643 27.6845   NA  15

# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0, verbose = FALSE)