Skip to contents

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

Usage

# S3 method for msNMix
predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)

Arguments

object

an object of class msNMix

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

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.

...

currently no additional arguments

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.

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.msNMix. 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 msNMix().

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.

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'

dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha,
                 sp = FALSE, family = 'Poisson')
# 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, , ]
# Prediction values
X.0 <- dat$X[pred.indx, ]
mu.0 <- dat$psi[, 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)

# Occupancy 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))
# Initial values
inits.list <- list(alpha.comm = 0, 
                   beta.comm = 0, 
                   beta = 0, 
                   alpha = 0,
                   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)
n.batch <- 4
batch.length <- 25
accept.rate <- 0.43

out <- msNMix(abund.formula = ~ abund.cov, 
              det.formula = ~ det.cov.1 + det.cov.2, 
              data = data.list, 
              inits = inits.list, 
              family = 'Poisson', 
              n.batch = n.batch,
              batch.length = batch.length, 
              accept.rate = 0.43,
              tuning = tuning,
              priors = prior.list, 
              n.omp.threads = 1, 
              verbose = TRUE, 
              n.report = 1)
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> 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 
#> 
#> 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]		24.0		0.29406
#> 	1	alpha[1]	40.0		0.29406
#> 	2	beta[1]		60.0		0.30606
#> 	2	alpha[1]	60.0		0.30000
#> 	3	beta[1]		52.0		0.30606
#> 	3	alpha[1]	40.0		0.30000
#> 	4	beta[1]		20.0		0.30000
#> 	4	alpha[1]	32.0		0.30000
#> 	5	beta[1]		32.0		0.30000
#> 	5	alpha[1]	48.0		0.30606
#> 	6	beta[1]		56.0		0.30606
#> 	6	alpha[1]	40.0		0.30000
#> -------------------------------------------------
#> Batch: 2 of 4, 50.00%
#> 	Species	Parameter	Acceptance	Tuning
#> 	1	beta[1]		24.0		0.29113
#> 	1	alpha[1]	24.0		0.29113
#> 	2	beta[1]		60.0		0.30914
#> 	2	alpha[1]	44.0		0.30302
#> 	3	beta[1]		28.0		0.30302
#> 	3	alpha[1]	64.0		0.30302
#> 	4	beta[1]		24.0		0.29701
#> 	4	alpha[1]	36.0		0.29701
#> 	5	beta[1]		40.0		0.29701
#> 	5	alpha[1]	60.0		0.30914
#> 	6	beta[1]		84.0		0.30914
#> 	6	alpha[1]	52.0		0.30302
#> -------------------------------------------------
#> Batch: 3 of 4, 75.00%
#> 	Species	Parameter	Acceptance	Tuning
#> 	1	beta[1]		12.0		0.28824
#> 	1	alpha[1]	16.0		0.28824
#> 	2	beta[1]		76.0		0.31224
#> 	2	alpha[1]	40.0		0.30000
#> 	3	beta[1]		52.0		0.30606
#> 	3	alpha[1]	44.0		0.30606
#> 	4	beta[1]		28.0		0.29406
#> 	4	alpha[1]	48.0		0.30000
#> 	5	beta[1]		44.0		0.30000
#> 	5	alpha[1]	44.0		0.31224
#> 	6	beta[1]		68.0		0.31224
#> 	6	alpha[1]	44.0		0.30606
#> -------------------------------------------------
#> Batch: 4 of 4, 100.00%

summary(out, level = 'community')
#> 
#> Call:
#> msNMix(abund.formula = ~abund.cov, det.formula = ~det.cov.1 + 
#>     det.cov.2, data = data.list, inits = inits.list, priors = prior.list, 
#>     tuning = tuning, 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.0012
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Abundance Means (log scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1350 0.3088 -0.4834 0.1822 0.6216   NA  90
#> abund.cov   0.1849 0.3403 -0.5857 0.1786 0.7207   NA  90
#> 
#> Abundance Variances (log scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.9601 0.8597 0.2506 0.6363 3.3516   NA  52
#> abund.cov   0.6480 0.7062 0.1427 0.4600 2.5748   NA  90
#> 
#> Detection Means (logit scale): 
#>                Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept)  0.2665 0.2169 -0.0825  0.2589 0.7344   NA  22
#> det.cov.1    0.2520 0.1923 -0.1218  0.2673 0.6039   NA  90
#> det.cov.2   -0.3596 0.5721 -1.5208 -0.3545 0.6489   NA 104
#> 
#> Detection Variances (logit scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1782 0.2656 0.0229 0.1069 0.8593   NA  26
#> det.cov.1   0.2899 0.3142 0.0661 0.1700 1.1720   NA  26
#> det.cov.2   1.7383 1.2605 0.5286 1.3760 5.2812   NA  64

# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0)
str(out.pred)
#> List of 3
#>  $ mu.0.samples: num [1:90, 1:6, 1:16] 5.06 5.98 5.03 5.03 5.03 ...
#>  $ N.0.samples : int [1:90, 1:6, 1:16] 4 8 2 6 4 10 6 4 2 3 ...
#>  $ call        : language predict.msNMix(object = out, X.0 = X.0)
#>  - attr(*, "class")= chr "predict.msNMix"