Skip to contents

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

Usage

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

Arguments

object

an object of class NMix

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

ignore.RE

logical value that specifies whether or not to remove random abundance (or detection if type = 'detection') effects from the subsequent predictions. If TRUE, random effects will be included. If FALSE, random effects will be set to 0 and predictions will only be generated from the fixed effects.

type

a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict latent abundance and expected abundance 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,

Value

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

mu.0.samples

a coda object 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 NMix()

N.0.samples

a coda object 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 coda object of posterior predictive samples for the detection probability values.

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

Examples

set.seed(100)
# Simulate Data -----------------------------------------------------------
J.x <- 10
J.y <- 10
J <- J.x * J.y
n.rep <- sample(2:4, J, replace = TRUE)
beta <- c(0.5, 2)
p.abund <- length(beta)
alpha <- c(0, 1)
p.det <- length(alpha)
dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha,
               sp = FALSE)
# 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, ]
# Prediction covariates
X.0 <- dat$X[pred.indx, ]
# Detection covariates
X.p <- dat$X.p[-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 = X.p[, , 2])
data.list <- list(y = y, 
                  abund.covs = abund.covs,
                  det.covs = det.covs)
# Priors
prior.list <- list(beta.normal = list(mean = rep(0, p.abund),
                                      var = rep(100, p.abund)),
                   alpha.normal = list(mean = rep(0, p.det),
                                       var = rep(2.72, p.det)), 
                   kappa.unif = c(0.001, 10))
# Initial values
inits.list <- list(alpha = rep(0, p.det),
                   beta = rep(0, p.abund),
                   kappa = 0.5,
                   N = apply(y, 1, max, na.rm = TRUE))

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

out <- NMix(abund.formula = ~ abund.cov, 
            det.formula = ~ det.cov,
            data = data.list, 
            inits = inits.list,
            n.batch = n.batch, 
            batch.length = batch.length,
            family = 'Poisson',
            priors = prior.list,
            n.omp.threads = 1,
            verbose = TRUE,
            n.report = 1, 
            n.burn = n.burn, 
            n.thin = n.thin, 
            n.chains = n.chains)
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> Poisson N-mixture model with 75 sites.
#> 
#> Samples per Chain: 250 (10 batches of length 25)
#> Burn-in: 0 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 250 
#> 
#> 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 10, 10.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		4.0		0.98020
#> 	beta[2]		4.0		0.98020
#> 	alpha[1]	16.0		0.98020
#> 	alpha[2]	8.0		0.98020
#> -------------------------------------------------
#> Batch: 2 of 10, 20.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		4.0		0.97045
#> 	beta[2]		4.0		0.97045
#> 	alpha[1]	12.0		0.97045
#> 	alpha[2]	8.0		0.97045
#> -------------------------------------------------
#> Batch: 3 of 10, 30.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		0.0		0.96079
#> 	beta[2]		4.0		0.96079
#> 	alpha[1]	8.0		0.96079
#> 	alpha[2]	0.0		0.96079
#> -------------------------------------------------
#> Batch: 4 of 10, 40.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		12.0		0.95123
#> 	beta[2]		4.0		0.95123
#> 	alpha[1]	4.0		0.95123
#> 	alpha[2]	4.0		0.95123
#> -------------------------------------------------
#> Batch: 5 of 10, 50.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		4.0		0.94176
#> 	beta[2]		0.0		0.94176
#> 	alpha[1]	8.0		0.94176
#> 	alpha[2]	16.0		0.94176
#> -------------------------------------------------
#> Batch: 6 of 10, 60.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		0.0		0.93239
#> 	beta[2]		8.0		0.93239
#> 	alpha[1]	12.0		0.93239
#> 	alpha[2]	0.0		0.93239
#> -------------------------------------------------
#> Batch: 7 of 10, 70.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		4.0		0.92312
#> 	beta[2]		4.0		0.92312
#> 	alpha[1]	12.0		0.92312
#> 	alpha[2]	12.0		0.92312
#> -------------------------------------------------
#> Batch: 8 of 10, 80.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		0.0		0.91393
#> 	beta[2]		4.0		0.91393
#> 	alpha[1]	0.0		0.91393
#> 	alpha[2]	12.0		0.91393
#> -------------------------------------------------
#> Batch: 9 of 10, 90.00%
#> 	Parameter	Acceptance	Tuning
#> 	beta[1]		0.0		0.90484
#> 	beta[2]		8.0		0.90484
#> 	alpha[1]	12.0		0.90484
#> 	alpha[2]	20.0		0.90484
#> -------------------------------------------------
#> Batch: 10 of 10, 100.00%

summary(out)
#> 
#> Call:
#> NMix(abund.formula = ~abund.cov, det.formula = ~det.cov, data = data.list, 
#>     inits = inits.list, priors = prior.list, n.batch = n.batch, 
#>     batch.length = batch.length, family = "Poisson", n.omp.threads = 1, 
#>     verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, 
#>     n.chains = n.chains)
#> 
#> Samples per Chain: 250
#> Burn-in: 0
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 250
#> Run Time (min): 5e-04
#> 
#> Abundance (log scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.4492 0.1836 0.3437 0.4481 0.4944   NA  22
#> abund.cov   1.9840 0.1595 1.7105 2.0131 2.0413   NA  17
#> 
#> Detection (logit scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1770 0.2357 -0.0986 0.0920 0.7924   NA   4
#> det.cov     1.1073 0.2053  0.8381 1.0911 1.5472   NA  16

# Predict at new locations ------------------------------------------------
colnames(X.0) <- c('intercept', 'abund.cov')
out.pred <- predict(out, X.0)
mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975))
plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', 
     ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants)))
segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ])
lines(dat$mu[pred.indx], dat$mu[pred.indx])