Skip to contents

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

Usage

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

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 sfMsNMix. Columns should correspond to the order of how covariates were specified in the corresponding formula argument of sfMsNMix. 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.0 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,
Andrew O. Finley finleya@msu.edu

Value

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

mu.0.samples

a three-dimensional array of posterior predictive samples for the expected abundance values. Note these will be per unit area if an offset was used when fitting the model with sfMsNMix().

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:

p.0.samples

a three-dimensional array of posterior predictive samples for the detection probability values.

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

Examples

set.seed(400)
J.x <- 8
J.y <- 8
J <- J.x * J.y
n.rep<- sample(2:4, size = J, replace = TRUE)
n.sp <- 6
# Community-level covariate effects
# Abundance
beta.mean <- c(0.2, 0.5)
p.abund <- length(beta.mean)
tau.sq.beta <- c(0.6, 0.3)
# Detection
alpha.mean <- c(0.5, 0.2, -0.1)
tau.sq.alpha <- c(0.2, 0.3, 1)
p.det <- length(alpha.mean)
# 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]))
}
family <- 'Poisson'
n.factors <- 3
phi <- runif(n.factors, 3 / 1, 3 / .1)

dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, 
                 beta = beta, alpha = alpha, sp = TRUE, 
                 family = 'Poisson', factor.model = TRUE, 
                 n.factors = n.factors, phi = phi, cov.model = 'exponential')
# Split into fitting and prediction data set
pred.indx <- sample(1:J, round(J * .25), replace = FALSE)
y <- dat$y[, -pred.indx, ]
# Abundance covariates
X <- dat$X[-pred.indx, ]
# Detection covariates
X.p <- dat$X.p[-pred.indx, , ]
# Coordinates
coords <- dat$coords[-pred.indx, ]
# Prediction values
X.0 <- dat$X[pred.indx, ]
mu.0 <- dat$psi[, pred.indx]
coords.0 <- dat$coords[pred.indx, ]
# Package all data into a list
abund.covs <- X[, 2, drop = FALSE]
colnames(abund.covs) <- c('abund.cov')
det.covs <- list(det.cov.1 = X.p[, , 2], 
                 det.cov.2 = X.p[, , 3])
data.list <- list(y = y, 
                  abund.covs = abund.covs,
                  det.covs = det.covs, 
                  coords = coords)

# Initial values
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, 3 / .1))
# Initial values
inits.list <- list(alpha.comm = 0, 
                   beta.comm = 0, 
                   beta = 0, 
                   alpha = 0,
                   phi = 3 / .5,
                   tau.sq.beta = 1, 
                   tau.sq.alpha = 1, 
                   N = apply(y, c(1, 2), max, na.rm = TRUE))
# Tuning values
tuning <- list(beta = 0.3, alpha = 0.3, lambda = 0.5, w = 0.5, phi = 1.5)
n.batch <- 4
batch.length <- 25
accept.rate <- 0.43

out <- sfMsNMix(abund.formula = ~ abund.cov, 
                det.formula = ~ det.cov.1 + det.cov.2, 
                data = data.list, 
                inits = inits.list, 
                family = 'Poisson', 
                n.factors = n.factors,
                n.batch = n.batch,
                batch.length = batch.length, 
                accept.rate = 0.43,
                cov.model = 'exponential', 
                n.neighbors = 5,
                tuning = tuning,
                priors = prior.list, 
                n.omp.threads = 1, 
                verbose = TRUE, 
                n.report = 1)
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> 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 NNGP Multi-species Poisson N-Mixture model fit with 48 sites and 6 species.
#> 
#> Samples per Chain: 100 
#> Burn-in: 10 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 90 
#> 
#> 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: 1 of 4, 25.00%
#> 	Species	Parameter	Acceptance	Tuning
#> 	1	beta[1]		12.0		0.30000
#> 	1	alpha[1]	28.0		0.29406
#> 	2	beta[1]		32.0		0.30000
#> 	2	alpha[1]	48.0		0.30000
#> 	3	beta[1]		48.0		0.30606
#> 	3	alpha[1]	60.0		0.30606
#> 	4	beta[1]		36.0		0.30000
#> 	4	alpha[1]	40.0		0.29406
#> 	5	beta[1]		28.0		0.30000
#> 	5	alpha[1]	52.0		0.30000
#> 	6	beta[1]		60.0		0.30606
#> 	6	alpha[1]	56.0		0.30606
#> 	1	phi		64.0		1.53030
#> 	2	phi		80.0		1.53030
#> 	3	phi		36.0		1.50000
#> -------------------------------------------------
#> Batch: 2 of 4, 50.00%
#> 	Species	Parameter	Acceptance	Tuning
#> 	1	beta[1]		28.0		0.29701
#> 	1	alpha[1]	16.0		0.29113
#> 	2	beta[1]		40.0		0.29701
#> 	2	alpha[1]	44.0		0.30302
#> 	3	beta[1]		28.0		0.30302
#> 	3	alpha[1]	36.0		0.30302
#> 	4	beta[1]		44.0		0.30302
#> 	4	alpha[1]	32.0		0.29113
#> 	5	beta[1]		40.0		0.29701
#> 	5	alpha[1]	36.0		0.29701
#> 	6	beta[1]		60.0		0.30914
#> 	6	alpha[1]	48.0		0.30914
#> 	1	phi		52.0		1.54568
#> 	2	phi		52.0		1.54568
#> 	3	phi		52.0		1.51508
#> -------------------------------------------------
#> Batch: 3 of 4, 75.00%
#> 	Species	Parameter	Acceptance	Tuning
#> 	1	beta[1]		20.0		0.29406
#> 	1	alpha[1]	24.0		0.28824
#> 	2	beta[1]		52.0		0.30000
#> 	2	alpha[1]	40.0		0.30000
#> 	3	beta[1]		44.0		0.30606
#> 	3	alpha[1]	56.0		0.30606
#> 	4	beta[1]		24.0		0.30000
#> 	4	alpha[1]	44.0		0.29406
#> 	5	beta[1]		36.0		0.29406
#> 	5	alpha[1]	36.0		0.29406
#> 	6	beta[1]		60.0		0.31224
#> 	6	alpha[1]	48.0		0.31224
#> 	1	phi		72.0		1.56122
#> 	2	phi		44.0		1.56122
#> 	3	phi		72.0		1.53030
#> -------------------------------------------------
#> Batch: 4 of 4, 100.00%

summary(out, level = 'community')
#> 
#> Call:
#> sfMsNMix(abund.formula = ~abund.cov, det.formula = ~det.cov.1 + 
#>     det.cov.2, data = data.list, inits = inits.list, priors = prior.list, 
#>     tuning = tuning, cov.model = "exponential", n.neighbors = 5, 
#>     n.factors = n.factors, n.batch = n.batch, batch.length = batch.length, 
#>     accept.rate = 0.43, family = "Poisson", n.omp.threads = 1, 
#>     verbose = TRUE, n.report = 1)
#> 
#> Samples per Chain: 100
#> Burn-in: 10
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 90
#> Run Time (min): 0.0036
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Abundance Means (log scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1836 0.4005 -0.6214 0.1540 1.2130   NA 125
#> abund.cov   0.4287 0.2130  0.1112 0.4175 0.8164   NA  66
#> 
#> Abundance Variances (log scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.7339 0.6442 0.1905 0.5391 2.6690   NA  65
#> abund.cov   0.2229 0.1637 0.0625 0.1829 0.7194   NA  36
#> 
#> Detection Means (logit scale): 
#>                Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept)  0.4455 0.2730  0.0022  0.3862 1.0413   NA  34
#> det.cov.1    0.2447 0.2105 -0.1826  0.2507 0.7219   NA  90
#> det.cov.2   -0.2774 0.3757 -0.9682 -0.2674 0.4007   NA  90
#> Detection Variances (logit scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.2901 0.3893 0.0491 0.2029 1.1979   NA  45
#> det.cov.1   0.2383 0.2761 0.0446 0.1626 0.6691   NA  44
#> det.cov.2   0.7968 0.4991 0.2712 0.6328 2.2640   NA  90
#> 
#> ----------------------------------------
#> 	Spatial Covariance
#> ----------------------------------------
#>          Mean     SD   2.5%     50%   97.5% Rhat ESS
#> phi-1 18.0705 6.7571 5.7451 17.8964 28.8509   NA  11
#> phi-2 15.2134 6.6508 6.7109 14.0017 27.8340   NA  15
#> phi-3 15.1885 5.3719 4.8653 14.9383 24.0379   NA  16

# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0)
#> ----------------------------------------
#> 	Prediction description
#> ----------------------------------------
#> Spatial Factor NNGP Multi-species N-mixture model with 48 observations.
#> 
#> Number of covariates 2 (including intercept if specified).
#> 
#> Using the exponential spatial correlation model.
#> 
#> Using 5 nearest neighbors.
#> Using 3 latent spatial factors.
#> 
#> Number of MCMC samples 90.
#> 
#> Predicting at 16 non-sampled locations.
#> 
#> Source compiled with OpenMP support and model fit using 1 threads.
#> -------------------------------------------------
#> 		Predicting
#> -------------------------------------------------
#> Location: 16 of 16, 100.00%
#> Generating latent abundance state
str(out.pred)
#> List of 5
#>  $ N.0.samples : num [1:90, 1:6, 1:16] 2 2 0 0 0 0 5 0 4 3 ...
#>  $ w.0.samples : num [1:90, 1:3, 1:16] 1.166 0.479 -1.101 -0.595 -1.497 ...
#>  $ mu.0.samples: num [1:90, 1:6, 1:16] 4.451 2.059 0.424 0.583 0.223 ...
#>  $ call        : language predict.sfMsNMix(object = out, X.0 = X.0, coords.0 = coords.0)
#>  $ object.class: chr "sfMsNMix"
#>  - attr(*, "class")= chr "predict.sfMsNMix"