Skip to contents

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

Usage

# S3 method for svcMsPGOcc
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 svcMsPGOcc

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

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.

...

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.svcMsPGOcc. 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 four-dimensional array of posterior predictive samples for the spatially-varying coefficients, with dimensions corresponding to MCMC sample, spatial factor, site, and spatially varying coefficient.

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 <- 10
J.y <- 10
J <- J.x * J.y
n.rep <- sample(5, size = J, replace = TRUE)
N <- 6
# Community-level covariate effects
# Occurrence
beta.mean <- c(0.2, -0.2, 0.3, -0.1, 0.4)
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, 1.5, 0.4, 0.5, 0.3)
# Detection
alpha.mean <- c(0, 1.2, -0.5)
tau.sq.alpha <- c(1, 0.5, 1.3)
p.det <- length(alpha.mean)
# No random effects
psi.RE <- list()
p.RE <- list()
# 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]))
}
# Number of spatial factors for each SVC
n.factors <- 2
# The intercept and first two covariates have spatially-varying effects
svc.cols <- c(1, 2, 3)
p.svc <- length(svc.cols)
q.p.svc <- n.factors * p.svc
# Spatial decay parameters
phi <- runif(q.p.svc, 3 / 0.9, 3 / 0.1)
# A length N vector indicating the proportion of simulated locations
# that are within the range for a given species.
range.probs <- runif(N, 1, 1)
factor.model <- TRUE
cov.model <- 'spherical'
sp <- TRUE

dat <- simMsOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, N = N, beta = beta, alpha = alpha,
    psi.RE = psi.RE, p.RE = p.RE, phi = phi, sp = sp, svc.cols = svc.cols,
    cov.model = cov.model, n.factors = n.factors,
    factor.model = factor.model, range.probs = range.probs)

# 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, ])

# Prep data for spOccupancy -----------------------------------------------
# Occurrence covariates
occ.covs <- cbind(X)
colnames(occ.covs) <- c('int', 'occ.cov.1', 'occ.cov.2', 'occ.cov.3',
      'occ.cov.4')
# Detection covariates
det.covs <- list(det.cov.1 = X.p[, , 2],
     det.cov.2 = X.p[, , 3])
# Data list
data.list <- list(y = y, coords = coords, occ.covs = occ.covs,
                  det.covs = det.covs)
# 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))
inits.list <- list(alpha.comm = 0,
       beta.comm = 0,
       beta = 0,
       alpha = 0,
       tau.sq.beta = 1,
       tau.sq.alpha = 1,
       z = apply(y, c(1, 2), max, na.rm = TRUE))
# Tuning
tuning.list <- list(phi = 1)

# Number of batches
n.batch <- 2
# Batch length
batch.length <- 25
n.burn <- 0
n.thin <- 1
n.samples <- n.batch * batch.length

out <- svcMsPGOcc(occ.formula = ~ occ.cov.1 + occ.cov.2 + occ.cov.3 +
                                  occ.cov.4,
                  det.formula = ~ det.cov.1 + det.cov.2,
                  data = data.list,
                  inits = inits.list,
                  n.batch = n.batch,
      n.factors = n.factors,
                  batch.length = batch.length,
      std.by.sp = TRUE,
                  accept.rate = 0.43,
                  priors = prior.list,
      svc.cols = svc.cols,
                  cov.model = "spherical",
                  tuning = tuning.list,
                  n.omp.threads = 1,
                  verbose = TRUE,
                  NNGP = TRUE,
                  n.neighbors = 5,
                  search.type = 'cb',
                  n.report = 10,
                  n.burn = n.burn,
      n.thin = n.thin,
      n.chains = 1)
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> phi is not specified in initial values.
#> Setting initial value to random values from the prior distribution
#> lambda is not specified in initial values.
#> Setting initial values of the lower triangle to random values from a standard normal
#> w is not specified in initial values.
#> Setting initial value to 0
#> ----------------------------------------
#> 	Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> Spatial Factor NNGP Multispecies Occupancy Model with Polya-Gamma latent
#> variable fit with 75 sites and 6 species.
#> 
#> Samples per chain: 50 (2 batches of length 25)
#> Burn-in: 0 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 50 
#> 
#> Number of spatially-varying coefficients: 3 
#> Using the spherical spatial correlation model.
#> 
#> Using 2 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: 2 of 2, 100.00%

summary(out)
#> 
#> Call:
#> svcMsPGOcc(occ.formula = ~occ.cov.1 + occ.cov.2 + occ.cov.3 + 
#>     occ.cov.4, det.formula = ~det.cov.1 + det.cov.2, data = data.list, 
#>     inits = inits.list, priors = prior.list, tuning = tuning.list, 
#>     svc.cols = svc.cols, cov.model = "spherical", NNGP = TRUE, 
#>     n.neighbors = 5, search.type = "cb", std.by.sp = TRUE, n.factors = n.factors, 
#>     n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, 
#>     n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, 
#>     n.thin = n.thin, n.chains = 1)
#> 
#> Samples per Chain: 50
#> Burn-in: 0
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 50
#> Run Time (min): 0.0033
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Occurrence Means (logit scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.3865 0.5685 -1.1749 0.4292 1.2276   NA  50
#> occ.cov.1   0.0081 0.5905 -1.1959 0.0432 1.1084   NA 173
#> occ.cov.2   0.7362 0.3440 -0.1158 0.7717 1.3399   NA  10
#> occ.cov.3   0.0562 0.2659 -0.4958 0.0354 0.4986   NA  11
#> occ.cov.4   0.8627 0.3776  0.2992 0.8223 1.5092   NA  33
#> 
#> Occurrence Variances (logit scale): 
#>               Mean     SD   2.5%    50%   97.5% Rhat ESS
#> (Intercept) 1.3177 1.3943 0.1741 0.7425  4.1792   NA  18
#> occ.cov.1   3.1192 3.4246 0.1013 2.2301 13.7831   NA  26
#> occ.cov.2   0.3586 0.6218 0.0511 0.2246  1.0391   NA  50
#> occ.cov.3   0.2961 0.2932 0.0393 0.2037  1.1387   NA  19
#> occ.cov.4   0.6849 0.5199 0.2031 0.5123  1.9494   NA  23
#> 
#> Detection Means (logit scale): 
#>                Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept) -0.3338 0.5077 -1.4572 -0.2468 0.4008   NA  32
#> det.cov.1    1.1351 0.4890  0.2226  1.0978 2.1360   NA  23
#> det.cov.2   -0.3246 0.5099 -1.1707 -0.3391 0.5401   NA  50
#> 
#> Detection Variances (logit scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.9784 1.1061 0.1420 0.7099 3.4060   NA  50
#> det.cov.1   1.4190 1.7630 0.0566 0.6807 5.4301   NA  13
#> det.cov.2   2.1039 2.1854 0.2831 1.3321 7.6769   NA  50
#> 
#> ----------------------------------------
#> 	Species Level
#> ----------------------------------------
#> Occurrence (logit scale): 
#>                    Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept)-sp1  0.1204 0.4108 -0.8129  0.1333  0.8772   NA  12
#> (Intercept)-sp2  0.5152 0.3386 -0.1270  0.5078  1.0796   NA  23
#> (Intercept)-sp3  1.2984 0.4233  0.7146  1.2412  2.1715   NA  17
#> (Intercept)-sp4  0.5981 0.5428 -0.2577  0.5200  1.5575   NA   5
#> (Intercept)-sp5  0.7718 0.4711 -0.0883  0.8728  1.4816   NA   9
#> (Intercept)-sp6 -0.7744 0.4436 -1.6482 -0.7071 -0.1762   NA   9
#> occ.cov.1-sp1    0.2998 0.4075 -0.3943  0.2956  1.2428   NA  13
#> occ.cov.1-sp2    2.2276 0.8809  0.4118  2.2859  3.4414   NA   6
#> occ.cov.1-sp3    0.8633 0.5466  0.0243  0.9007  1.8449   NA  12
#> occ.cov.1-sp4   -1.1789 0.7235 -2.5228 -1.2085 -0.0175   NA   5
#> occ.cov.1-sp5   -0.2925 0.5078 -1.1791 -0.3295  0.5847   NA  13
#> occ.cov.1-sp6   -1.0185 0.5218 -1.9747 -0.9902 -0.0561   NA   9
#> occ.cov.2-sp1    0.6744 0.2852  0.1269  0.7041  1.1580   NA  25
#> occ.cov.2-sp2    0.8395 0.3460  0.0787  0.8597  1.3587   NA  30
#> occ.cov.2-sp3    0.4144 0.4998 -0.5444  0.4081  1.3089   NA   6
#> occ.cov.2-sp4    0.5985 0.4612 -0.5446  0.6236  1.3293   NA  19
#> occ.cov.2-sp5    1.1814 0.4116  0.4731  1.2444  1.9073   NA   9
#> occ.cov.2-sp6    0.5817 0.4090 -0.1759  0.6052  1.3318   NA  16
#> occ.cov.3-sp1    0.1635 0.2813 -0.3860  0.1352  0.7058   NA  21
#> occ.cov.3-sp2   -0.2556 0.2746 -0.7935 -0.1994  0.2446   NA  27
#> occ.cov.3-sp3   -0.3107 0.3698 -0.9376 -0.2785  0.3672   NA  19
#> occ.cov.3-sp4    0.6446 0.4050  0.0485  0.6618  1.5711   NA   7
#> occ.cov.3-sp5   -0.0053 0.2979 -0.5681 -0.0031  0.6136   NA  16
#> occ.cov.3-sp6    0.1729 0.2715 -0.3626  0.1452  0.8022   NA  14
#> occ.cov.4-sp1    1.6203 0.4602  0.8826  1.6058  2.5588   NA  20
#> occ.cov.4-sp2    0.9906 0.5377  0.1522  0.8947  1.9813   NA  11
#> occ.cov.4-sp3   -0.1165 0.4216 -0.9665 -0.1469  0.7349   NA  11
#> occ.cov.4-sp4    1.0866 0.4027  0.4983  1.0434  1.9043   NA  19
#> occ.cov.4-sp5    0.7303 0.4934 -0.2793  0.7929  1.5352   NA   9
#> occ.cov.4-sp6    0.8864 0.2991  0.4276  0.8976  1.4710   NA  27
#> 
#> Detection (logit scale): 
#>                    Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept)-sp1  0.8278 0.3256  0.1824  0.8555  1.4689   NA  16
#> (Intercept)-sp2  0.0943 0.2817 -0.4700  0.1230  0.5269   NA  14
#> (Intercept)-sp3  0.3342 0.2001 -0.0304  0.3151  0.7154   NA  50
#> (Intercept)-sp4 -1.0454 0.2936 -1.5948 -1.0048 -0.5161   NA  17
#> (Intercept)-sp5 -0.8061 0.2454 -1.1824 -0.8404 -0.3858   NA  10
#> (Intercept)-sp6 -0.6116 0.2909 -1.1882 -0.6198 -0.1343   NA  22
#> det.cov.1-sp1    1.0036 0.3055  0.5914  0.9636  1.5820   NA   8
#> det.cov.1-sp2    2.4081 0.7330  1.2315  2.2326  3.8627   NA   6
#> det.cov.1-sp3    0.4143 0.1909  0.0993  0.4279  0.8450   NA  33
#> det.cov.1-sp4    1.6223 0.4313  0.8609  1.6472  2.2960   NA   7
#> det.cov.1-sp5    1.0948 0.2096  0.6819  1.0998  1.4972   NA  25
#> det.cov.1-sp6    1.2477 0.3431  0.5479  1.3057  1.8199   NA  21
#> det.cov.2-sp1   -0.6427 0.2398 -1.0699 -0.6205 -0.2813   NA  38
#> det.cov.2-sp2   -1.8715 0.5901 -3.1050 -1.7665 -1.0658   NA   9
#> det.cov.2-sp3   -0.9136 0.2023 -1.3211 -0.8808 -0.5774   NA  50
#> det.cov.2-sp4   -0.2174 0.2609 -0.7359 -0.1965  0.2730   NA  28
#> det.cov.2-sp5    0.6222 0.2505  0.1696  0.5835  1.0950   NA  29
#> det.cov.2-sp6    1.0674 0.4100  0.4339  1.0182  1.9062   NA  12
#> 
#> ----------------------------------------
#> 	Spatial Covariance
#> ----------------------------------------
#>                      Mean     SD    2.5%     50%   97.5% Rhat ESS
#> phi-1-(Intercept) 16.6851 5.3523  8.1941 16.1706 24.5452   NA  10
#> phi-2-(Intercept) 18.6767 4.5936 10.0707 18.5800 25.3736   NA  25
#> phi-1-occ.cov.1   15.6544 8.4278  3.7932 16.6590 27.7249   NA   4
#> phi-2-occ.cov.1   11.7298 4.8515  5.2252 11.7010 21.1147   NA  16
#> phi-1-occ.cov.2   10.4756 3.6043  5.9287  9.8572 18.3046   NA  13
#> phi-2-occ.cov.2   14.6710 4.3363  8.5785 13.8155 23.0049   NA  30
# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0, verbose = FALSE)

# Get SVC samples for each species at prediction locations
svc.samples <- getSVCSamples(out, out.pred)