Skip to contents

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

Usage

# S3 method for sfMsDS
predict(object, X.0, coords.0, n.omp.threads = 1, 
        verbose = TRUE, n.report = 100, ignore.RE = FALSE, 
        type = 'abundance', include.sp = TRUE, ...)

Arguments

object

an object of class sfMsDS

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 abundance (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 sfMsDS. Columns should correspond to the order of how covariates were specified in the corresponding formula argument of sfMsDS. Column names must match the names of the variables used to fit the model (for the intercept, use '(Intercept)').

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, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect.

type

a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates.

include.sp

a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to TRUE. If set to FALSE, predictions are given using the covariates and any unstructured random effects in the model. If FALSE, the coords argument is not required.

...

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 effect 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

Value

A list object of class predict.sfMsDS. When type = 'abundance', the list consists of:

mu.0.samples

a three-dimensional array of posterior predictive samples for the expected abundance values, or expected abundance values per unit area (i.e., density) values when an offset was used when fitting the model with sfMsDS().

N.0.samples

a three-dimensional array of posterior predictive samples for the latent abundance values. These will be in the same units as mu.0.samples.

w.0.samples

a three-dimensional array of posterior predictive samples for the spatial latent factors.

When type = 'detection', the list consists of:

sigma.0.samples

a three-dimensional array of posterior predictive samples for sigma (the parameter controlling detection probability).

The return object will include additional objects used for standard extractor functions.

Examples

set.seed(210)
J.x <- 10
J.y <- 10 
J <- J.x * J.y
# Number of distance bins from which to simulate data. 
n.bins <- 5
# Length of each bin. This should be of length n.bins
bin.width <- c(.10, .10, .20, .3, .1)
# Number of species
n.sp <- 5
# Community-level abundance coefficients
beta.mean <- c(-1, 0.2, 0.3, -0.2)
p.abund <- length(beta.mean)
tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4)
# Detection coefficients
alpha.mean <- c(-1.0, -0.3)
p.det <- length(alpha.mean)
tau.sq.alpha <- c(0.1, 0.2)
# Detection decay function
det.func <- 'halfnormal'
mu.RE <- list()
p.RE <- list()
# Draw species-level effects from community means.
beta <- matrix(NA, nrow = n.sp, ncol = p.abund)
alpha <- matrix(NA, nrow = n.sp, ncol = p.det)
for (i in 1:p.abund) {
  beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i]))
}
for (i in 1:p.det) {
  alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i]))
}
sp <- TRUE 
family <- 'Poisson'
kappa <- runif(n.sp, 0.3, 3) 
offset <- pi * .8^2
transect <- 'line'
factor.model <- TRUE
n.factors <- 3
phi <- runif(n.factors, 3 / 1, 3 / .2)
cov.model <- 'exponential'

dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width,
               n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, 
               mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model,
               sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, 
               offset = offset, transect = transect, factor.model = factor.model, 
               n.factors = n.factors)
#> overdispersion parameter (kappa) is ignored when family == 'Poisson'
#> sigma.sq is specified but will be set to 1 for spatial latent factor model

# 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, ]
# Prediction covariates
X.0 <- dat$X[pred.indx, ]
# Detection covariates
X.p <- dat$X.p[-pred.indx, , drop = FALSE]
X.p.0 <- dat$X.p[pred.indx, , drop = FALSE]
coords <- as.matrix(dat$coords[-pred.indx, ])
coords.0 <- as.matrix(dat$coords[pred.indx, ])
dist.breaks <- dat$dist.breaks


covs <- cbind(X, X.p)
colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 
                    'int.det', 'det.cov.1')

data.list <- list(y = y, 
                  covs = covs,
                  dist.breaks = dist.breaks, 
                  coords = coords,
                  offset = offset)

# Priors
prior.list <- list(beta.comm.normal = list(mean = 0, var = 10),
                   alpha.comm.normal = list(mean = 0, var = 10), 
                   kappa.unif = list(0, 100), 
                   phi.unif = list(3 / 1, 3 / .1),
                   tau.sq.beta.ig = list(a = 0.1, b = 0.1),
                   tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) 
# Starting values
inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0,
                   alpha = 0, kappa = 1, phi = 3 / .5)

tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, 
               kappa = 0.8, lambda = 1, w = 1, phi = 0.8) 

n.batch <- 4 
batch.length <- 25
n.burn <- 0
n.thin <- 1
n.chains <- 1

out <- sfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3,
              det.formula = ~ det.cov.1,
              data = data.list, 
              n.batch = n.batch, 
              batch.length = batch.length, 
              inits = inits.list, 
              family = 'Poisson',
              det.func = 'halfnormal', 
              transect = transect, 
              tuning = tuning,
              cov.model = 'exponential', 
              NNGP = TRUE,
              n.neighbors = 5,
              n.factors = n.factors,
              priors = prior.list, 
              accept.rate = 0.43, 
              n.omp.threads = 1, 
              verbose = TRUE, 
              n.report = 10,
              n.burn = n.burn,
              n.thin = n.thin,
              n.chains = n.chains) 
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> N is not specified in initial values.
#> Setting initial values based on observed data
#> tau.sq.beta is not specified in initial values.
#> Setting initial values to random values between 0.05 and 1
#> tau.sq.alpha is not specified in initial values.
#> Setting to initial values to random values between 0.05 and 1
#> lambda is not specified in initial values.
#> Setting initial values of the lower triangle to 0
#> 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 Multi-species Poisson HDS model with 75 sites and 5 species.
#> 
#> Samples per Chain: 100 (4 batches of length 25)
#> Burn-in: 0 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 100 
#> 
#> 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: 4 of 4, 100.00%
summary(out, level = 'community')
#> 
#> Call:
#> sfMsDS(abund.formula = ~abund.cov.1 + abund.cov.2 + abund.cov.3, 
#>     det.formula = ~det.cov.1, data = data.list, inits = inits.list, 
#>     priors = prior.list, tuning = tuning, cov.model = "exponential", 
#>     NNGP = TRUE, n.neighbors = 5, n.factors = n.factors, n.batch = n.batch, 
#>     batch.length = batch.length, accept.rate = 0.43, family = "Poisson", 
#>     transect = transect, det.func = "halfnormal", n.omp.threads = 1, 
#>     verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, 
#>     n.chains = n.chains)
#> 
#> Samples per Chain: 100
#> Burn-in: 0
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 100
#> Run Time (min): 0.0067
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Abundance Means (log scale): 
#>                Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept) -0.2855 0.4218 -1.2206 -0.2423 0.6480   NA 100
#> abund.cov.1  0.1124 0.4418 -0.7060  0.1487 1.1221   NA 173
#> abund.cov.2  0.1701 0.2979 -0.3246  0.1457 0.9215   NA 100
#> abund.cov.3 -0.0404 0.2245 -0.5171 -0.0567 0.4645   NA  73
#> 
#> Abundance Variances (log scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.6022 0.6654 0.0454 0.4125 2.3402   NA  52
#> abund.cov.1 0.8238 1.5485 0.0346 0.4246 3.8673   NA  51
#> abund.cov.2 0.5320 0.6073 0.0585 0.3570 2.2043   NA  12
#> abund.cov.3 0.2394 0.3434 0.0246 0.1102 1.2356   NA  58
#> 
#> Detection Means (log scale): 
#>                Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept) -0.5748 0.4354 -1.1663 -0.6929 0.3284   NA   5
#> det.cov.1   -0.3521 0.5088 -0.9557 -0.3554 0.2118   NA  54
#> Detection Variances (log scale): 
#>               Mean     SD   2.5%    50% 97.5% Rhat ESS
#> (Intercept) 0.2189 0.3564 0.0305 0.1061 1.118   NA  68
#> det.cov.1   0.7044 3.7634 0.0311 0.1504 1.288   NA  64
#> 
#> ----------------------------------------
#> 	Spatial Covariance
#> ----------------------------------------
#>          Mean     SD    2.5%     50%   97.5% Rhat ESS
#> phi-1 23.5566 5.2495 10.0521 24.9076 29.2463   NA  12
#> phi-2  5.6056 2.7725  3.2371  4.9171 16.9231   NA   8
#> phi-3  5.1609 1.1197  3.1452  5.1485  7.3688   NA  10

# Predict at new locations ------------------------------------------------
colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3')
out.pred <- predict(out, X.0, coords.0)
#> ----------------------------------------
#> 	Prediction description
#> ----------------------------------------
#> Spatial Factor NNGP Multi-species N-mixture model with 75 observations.
#> 
#> Number of covariates 4 (including intercept if specified).
#> 
#> Using the exponential spatial correlation model.
#> 
#> Using 5 nearest neighbors.
#> Using 3 latent spatial factors.
#> 
#> Number of MCMC samples 100.
#> 
#> Predicting at 25 non-sampled locations.
#> 
#> Source compiled with OpenMP support and model fit using 1 threads.
#> -------------------------------------------------
#> 		Predicting
#> -------------------------------------------------
#> Location: 25 of 25, 100.00%
#> Generating latent abundance state
str(out.pred)
#> List of 5
#>  $ N.0.samples : num [1:100, 1:5, 1:25] 3 1 1 0 0 0 1 0 0 0 ...
#>  $ w.0.samples : num [1:100, 1:3, 1:25] -0.132 -0.157 -0.874 0.096 -1.451 ...
#>  $ mu.0.samples: num [1:100, 1:5, 1:25] 0.75 0.681 0.268 0.655 0.139 ...
#>  $ call        : language predict.sfMsNMix(object = object, X.0 = X.0, coords.0 = coords.0, n.omp.threads = n.omp.threads,      verbose = v| __truncated__ ...
#>  $ object.class: chr "sfMsDS"
#>  - attr(*, "class")= chr "predict.sfMsDS"