Function for Fitting Single-Species Spatial N-Mixture Models
spNMix.Rd
The function spNMix
fits single-species spatial N-mixture models. Spatial models are fit using Nearest Neighbor Gaussian Processes.
Usage
spNMix(abund.formula, det.formula, data, inits, priors, tuning,
cov.model = 'exponential', NNGP = TRUE,
n.neighbors = 15, search.type = 'cb',
n.batch, batch.length, accept.rate = 0.43, family = 'Poisson',
n.omp.threads = 1, verbose = TRUE, n.report = 100,
n.burn = round(.10 * n.batch * batch.length), n.thin = 1,
n.chains = 1, ...)
Arguments
- abund.formula
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and random slopes are allowed using lme4 syntax (Bates et al. 2015).
- det.formula
a symbolic description of the model to be fit for the detection portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and random slopes are allowed using lme4 syntax (Bates et al. 2015).
- data
a list containing data necessary for model fitting. Valid tags are
y
,abund.covs
,det.covs
,offset
, andcoords
.y
is the count data matrix or data frame with first dimension equal to the number of sites (\(J\)) and second dimension equal to the maximum number of replicates at a given site.abund.covs
is a matrix or data frame containing the variables used in the abundance portion of the model, with \(J\) rows for each column (variable).det.covs
is a list of variables included in the detection portion of the model. Each list element is a different detection covariate, which can be site-level or observational-level. Site-level covariates are specified as a vector of length \(J\) while observation-level covariates are specified as a matrix or data frame with the number of rows equal to \(J\) and number of columns equal to the maximum number of replicates at a given site.coords
is a \(J \times 2\) matrix of the observation coordinates. Note thatspAbundance
assumes coordinates are specified in a projected coordinate system.offset
is an offset to use in the abundance model (e.g., an area offset). This can be either a single value or a vector with an offset for each site (e.g., if survey area differed in size).- inits
a list with each tag corresponding to a parameter name. Valid tags are
N
,beta
,alpha
,sigma.sq
,phi
,w
,nu
,kappa
,sigma.sq.mu
,sigma.sq.p
.nu
is only specified ifcov.model = "matern"
,sigma.sq.p
is only specified if there are random effects indet.formula
,sigma.sq.mu
is only specified if there are random effects inabund.formula
, andkappa
is only specified whenfamily = 'NB'
. The value portion of each tag is the parameter's initial value. Seepriors
description for definition of each parameter name. Additionally, the tagfix
can be set toTRUE
to fix the starting values across all chains. Iffix
is not specified (the default), starting values are varied randomly across chains.- priors
a list with each tag corresponding to a parameter name. Valid tags are
beta.normal
,alpha.normal
,phi.unif
,sigma.sq.ig
,nu.unif
,kappa.unif
,sigma.sq.mu.ig
, andsigma.sq.p.ig
. Abundance (beta
) and detection (alpha
) regression coefficients are assumed to follow a normal distribution. The hyperparameters of the normal distribution are passed as a list of length two with the first and second elements corresponding to the mean and variance of the normal distribution, which are each specified as vectors of length equal to the number of coefficients to be estimated or of length one if priors are the same for all coefficients. If not specified, prior means are set to 0 and prior variances for abundance coefficients are set to 100 and for detection coefficients set to 2.72. The spatial variance parameter,sigma.sq
, is assumed to follow an inverse-Gamma distribution. The spatial decayphi
, spatial smoothnessnu
, and negative binomial dispersionkappa
parameters are assumed to follow Uniform distributions. The hyperparameters of the inverse-Gamma forsigma.sq
are passed as a vector of length two, with the first and second elements corresponding to the shape and scale, respectively. The hyperparameters of the Uniform are also passed as a vector of length two with the first and second elements corresponding to the lower and upper support, respectively.sigma.sq.mu
andsigma.sq.p
are the random effect variances for any abundance or detection random effects, respectively, and are assumed to follow an inverse-Gamma distribution. The hyperparameters of the inverse-Gamma distribution are passed as a list of length two with the first and second elements corresponding to the shape and scale parameters, respectively, which are each specified as vectors of length equal to the number of random intercepts/slopes or of length one if priors are the same for all random effect variances.- cov.model
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the observations. Supported covariance model key words are:
"exponential"
,"matern"
,"spherical"
, and"gaussian"
.- tuning
a list with each tag corresponding to a parameter name, whose value corresponds to the initial tuning variance of the adaptive sampler for
beta
,alpha
,beta.star
(the abundance random effect values),alpha.star
(the detection random effect values),kappa
,phi
,nu
, andw
. See Roberts and Rosenthal (2009) for details.- NNGP
if
TRUE
, model is fit with an NNGP. See Datta et al. (2016) and Finley et al. (2019) for more information. Currently only NNGP is supported, functionality for a Gaussian Process may be addded in future package development.- n.neighbors
number of neighbors used in the NNGP. Only used if
NNGP = TRUE
. Datta et al. (2016) showed that 15 neighbors is usually sufficient, but that as few as 5 neighbors can be adequate for certain data sets, which can lead to even greater decreases in run time. We recommend starting with 15 neighbors (the default) and if additional gains in computation time are desired, subsequently compare the results with a smaller number of neighbors using WAIC.- search.type
a quoted keyword that specifies the type of nearest neighbor search algorithm. Supported method key words are:
"cb"
and"brute"
. The"cb"
should generally be much faster. If locations do not have identical coordinate values on the axis used for the nearest neighbor ordering then"cb"
and"brute"
should produce identical neighbor sets. However, if there are identical coordinate values on the axis used for nearest neighbor ordering, then"cb"
and"brute"
might produce different, but equally valid, neighbor sets, e.g., if data are on a grid.- n.batch
the number of MCMC batches in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details.
- batch.length
the length of each MCMC batch in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details.
- accept.rate
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details.
- family
the distribution to use for the latent abundance process. Currently supports
'NB'
(negative binomial) and'Poisson'
.- 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
, messages about data preparation, model specification, and progress of the sampler are printed to the screen. Otherwise, no messages are printed.- n.report
the interval to report Metropolis sampler acceptance and MCMC progress.
- n.burn
the number of samples out of the total
n.batch * batch.length
samples in each chain to discard as burn-in. By default, the first 10% of samples is discarded.- n.thin
the thinning interval for collection of MCMC samples. The thinning occurs after the
n.burn
samples are discarded. Default value is set to 1.- n.chains
the number of MCMC chains to run in sequence.
- ...
currently no additional arguments
References
Bates, Douglas, Martin Maechler, Ben Bolker, Steve Walker (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01 .
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091 .
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924 .
Roberts, G.O. and Rosenthal J.S. (2009) Examples of adaptive MCMC. Journal of Computational and Graphical Statistics, 18(2):349-367.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Author
Jeffrey W. Doser doserjef@msu.edu,
Andrew O. Finley finleya@msu.edu
Value
An object of class spNMix
that is a list comprised of:
- beta.samples
a
coda
object of posterior samples for the abundance regression coefficients.- alpha.samples
a
coda
object of posterior samples for the detection regression coefficients.- kappa.samples
a
coda
object of posterior samples for the abundance dispersion parameter. Only included whenfamily = 'NB'
.- N.samples
a
coda
object of posterior samples for the latent abundance values- mu.samples
a
coda
object of posterior samples for the latent expected abundance values- theta.samples
a
coda
object of posterior samples for spatial covariance parameters.- w.samples
a
coda
object of posterior samples for latent spatial random effects.- sigma.sq.mu.samples
a
coda
object of posterior samples for variances of random intercepts/slopes included in the abundance portion of the model. Only included if random effects are specified inabund.formula
.- sigma.sq.p.samples
a
coda
object of posterior samples for variances of random effects included in the detection portion of the model. Only included if random effects are specified indet.formula
.- beta.star.samples
a
coda
object of posterior samples for the abundance random effects. Only included if random effects are specified inabund.formula
.- alpha.star.samples
a
coda
object of posterior samples for the detection random effects. Only included if random effects are specified indet.formula
.- rhat
a list of Gelman-Rubin diagnostic values for some of the model parameters.
- ESS
a list of effective sample sizes for some of the model parameters.
- run.time
execution time reported using
proc.time()
.
The return object will include additional objects used for
subsequent prediction and/or model fit evaluation. Note that detection
probability values are not included in the model object, but can be
extracted using fitted()
.
Examples
set.seed(350)
# Simulate Data -----------------------------------------------------------
J.x <- 15
J.y <- 15
J <- J.x * J.y
n.rep <- sample(3, J, replace = TRUE)
beta <- c(0.5, 1.5)
p.abund <- length(beta)
alpha <- c(0.5, 1.2, -0.5)
p.det <- length(alpha)
mu.RE <- list()
p.RE <- list()
phi <- runif(1, 3 / 1, 3 / .1)
sigma.sq <- runif(1, 0.2, 1.5)
kappa <- 0.5
sp <- TRUE
cov.model <- 'exponential'
dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha,
kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp,
phi = phi, sigma.sq = sigma.sq, cov.model = cov.model,
family = 'NB')
y <- dat$y
X <- dat$X
X.re <- dat$X.re
X.p <- dat$X.p
X.p.re <- dat$X.p.re
coords <- dat$coords
abund.covs <- X
colnames(abund.covs) <- c('int', 'abund.cov.1')
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)
# 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, 10))
# Starting values
inits.list <- list(alpha = alpha,
beta = beta,
kappa = kappa,
w = rep(0, J),
phi = 3 / 0.5,
sigma.sq = 1,
N = apply(y, 1, max, na.rm = TRUE))
# Tuning values
tuning.list <- list(phi = 0.5, kappa = 0.5, beta = 0.1, alpha = 0.1, w = 0.1)
n.batch <- 4
batch.length <- 25
n.burn <- 0
n.thin <- 1
n.chains <- 1
out <- spNMix(abund.formula = ~ abund.cov.1,
det.formula = ~ det.cov.1 + det.cov.2,
data = data.list,
n.batch = n.batch,
batch.length = batch.length,
inits = inits.list,
priors = prior.list,
NNGP = TRUE,
cov.model = 'spherical',
n.neighbors = 10,
accept.rate = 0.43,
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
#> ----------------------------------------
#> No prior specified for phi.unif.
#> Setting uniform bounds based on the range of observed spatial coordinates.
#> No prior specified for sigma.sq.
#> Using an inverse-Gamma prior with the shape parameter set to 2 and scale parameter to 1.
#> ----------------------------------------
#> Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Spatial NNGP Poisson N-mixture model with 225 sites.
#>
#> 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 spherical spatial correlation model.
#>
#> Using 10 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%
#> Parameter Acceptance Tuning
#> beta[1] 0.0 0.98020
#> beta[2] 0.0 0.98020
#> alpha[1] 4.0 0.98020
#> alpha[2] 0.0 0.98020
#> alpha[3] 0.0 0.98020
#> phi 92.0 1.02020
#> -------------------------------------------------
#> Batch: 2 of 4, 50.00%
#> Parameter Acceptance Tuning
#> beta[1] 8.0 0.97045
#> beta[2] 0.0 0.97045
#> alpha[1] 8.0 0.97045
#> alpha[2] 12.0 0.97045
#> alpha[3] 4.0 0.97045
#> phi 88.0 1.03045
#> -------------------------------------------------
#> Batch: 3 of 4, 75.00%
#> Parameter Acceptance Tuning
#> beta[1] 4.0 0.96079
#> beta[2] 4.0 0.96079
#> alpha[1] 8.0 0.96079
#> alpha[2] 0.0 0.96079
#> alpha[3] 0.0 0.96079
#> phi 56.0 1.04081
#> -------------------------------------------------
#> Batch: 4 of 4, 100.00%
summary(out)
#>
#> Call:
#> spNMix(abund.formula = ~abund.cov.1, det.formula = ~det.cov.1 +
#> det.cov.2, data = data.list, inits = inits.list, priors = prior.list,
#> cov.model = "spherical", NNGP = TRUE, n.neighbors = 10, n.batch = n.batch,
#> batch.length = batch.length, accept.rate = 0.43, n.omp.threads = 1,
#> verbose = TRUE, n.report = 1, 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.0051
#>
#> Abundance (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.4364 0.0205 0.4070 0.4234 0.4852 NA 9
#> abund.cov.1 2.0975 0.0001 2.0973 2.0976 2.0976 NA 5
#>
#> Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.6255 0.0300 0.5735 0.6439 0.6536 NA 8
#> det.cov.1 1.1822 0.0374 1.1335 1.2012 1.2148 NA 1
#> det.cov.2 -0.5316 0.0109 -0.5376 -0.5325 -0.5000 NA 5
#>
#> Spatial Covariance:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> sigma.sq 2.8886 0.8806 0.5888 3.1385 3.9898 NA 4
#> phi 22.7268 8.2548 11.5361 22.5277 38.0600 NA 13