Function for Fitting Single-Species N-mixture Models
NMix.Rd
Function for fitting single-species N-mixture models.
Usage
NMix(abund.formula, det.formula, data, inits, priors, tuning,
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 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 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
, andoffset
.y
is a matrix or data frame of the observed count values, 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.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
,kappa
,sigma.sq.mu
, andsigma.sq.p
. The value portion of each tag is the parameter's initial value.sigma.sq.mu
andsigma.sq.p
are only relevant when including random effects in the abundance and detection portion of the model, respectively.kappa
is only relevant whenfamily = 'NB'
. 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
,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 set to 100 for the abundance coefficients and 2.72 for the detection coefficients.kappa
is the negative binomial dispersion parameter and is assumed to follow a uniform distribution. The hyperparameters of the uniform distribution are passed as a vector of length two with the first and second elements corresponding to the lower and upper bounds of the uniform distribution.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 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.- tuning
a list with each tag corresponding to a parameter name, whose value defines the initial variance of the adaptive sampler. Valid tags are
beta
,alpha
,beta.star
(the abundance random effect values),alpha.star
(the detection random effect values), andkappa
. See Roberts and Rosenthal (2009) for details.- 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 hypterthreaded cores. Note,n.omp.threads
> 1 might not work on some systems. Currently only relevant for spatial models.- 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.samples
to discard as burn-in for each chain. 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 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 .
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. doi:10.1111/j.0006-341X.2004.00142.x .
Author
Jeffrey W. Doser doserjef@msu.edu,
Value
An object of class NMix
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- sigma.sq.mu.samples
a
coda
object of posterior samples for variances of random effects 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 estimated values are not included in the model object, but can be
extracted using fitted()
.
Examples
set.seed(1010)
J.x <- 10
J.y <- 10
J <- J.x * J.y
n.rep <- sample(3, J, replace = TRUE)
beta <- c(0, -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 <- 3/.6
sigma.sq <- 2
kappa <- 0.3
sp <- FALSE
cov.model <- 'exponential'
dist <- 'NB'
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.p <- dat$X.p
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)
# 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 = 0,
beta = 0,
kappa = kappa,
N = apply(y, 1, max, na.rm = TRUE))
n.batch <- 4
batch.length <- 25
n.burn <- 50
n.thin <- 1
n.chains <- 1
out <- NMix(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,
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
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Poisson N-mixture model with 100 sites.
#>
#> Samples per Chain: 100 (4 batches of length 25)
#> Burn-in: 50
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 50
#>
#> 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] 12.0 0.98020
#> beta[2] 4.0 0.98020
#> alpha[1] 24.0 0.98020
#> alpha[2] 20.0 0.98020
#> alpha[3] 24.0 0.98020
#> -------------------------------------------------
#> Batch: 2 of 4, 50.00%
#> Parameter Acceptance Tuning
#> beta[1] 12.0 0.97045
#> beta[2] 12.0 0.97045
#> alpha[1] 12.0 0.97045
#> alpha[2] 28.0 0.97045
#> alpha[3] 28.0 0.97045
#> -------------------------------------------------
#> Batch: 3 of 4, 75.00%
#> Parameter Acceptance Tuning
#> beta[1] 12.0 0.96079
#> beta[2] 4.0 0.96079
#> alpha[1] 16.0 0.96079
#> alpha[2] 16.0 0.96079
#> alpha[3] 24.0 0.96079
#> -------------------------------------------------
#> Batch: 4 of 4, 100.00%