# Function for Fitting Univariate Spatial Abundance GLMs

`spAbund.Rd`

The function `spAbund`

fits univariate spatial abundance GLMs.

## Usage

```
spAbund(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, save.fitted = TRUE, ...)
```

## Arguments

- formula
a symbolic description of the model to be fit for 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`

,`covs`

,`z`

,`coords`

, and`offset`

.`y`

is a vector, matrix, or data frame of the observed count values. If a vector, the values represent the observed counts at each site. If multiple replicate observations are obtained at the sites (e.g., sub-samples, repeated sampling over multiple seasons),`y`

can be specified as a 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.`covs`

is either a data frame or list containing the variables used in the model. When only fitting a model with site-level data,`covs`

can be specified as a data frame, with each row corresponding to site and each column corresponding to a variable. When multiple abundance values are available at a site,`covs`

is specified as a list, where each list element is a different covariate, which can be site-level or observation-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 replicate observations at a given site.`coords`

is a \(J \times 2\) matrix of the observation coordinates. Note that`spAbundance`

assumes coordinates are specified in a projected coordinate system. For zero-inflated Gaussian models, the tag`z`

is used to specify the binary component of the zero-inflated model and should have the same length as`y`

.`offset`

is an offset to use in the abundance model (e.g., an area offset). This can be either a single value, a vector with an offset for each site (e.g., if survey area differed in size), or a site x replicate matrix if more than one count is available at a given site.- inits
a list with each tag corresponding to a parameter name. Valid tags are

`beta`

,`sigma.sq`

,`phi`

,`w`

,`nu`

,`kappa`

,`sigma.sq.mu`

,`tau.sq`

.`nu`

is only specified if`cov.model = "matern"`

,`sigma.sq.mu`

is only specified if there are random effects in`formula`

, and`kappa`

is only specified when`family = 'NB'`

.`tau.sq`

is only specified when`family = 'Gaussian'`

or`family = 'zi-Gaussian'`

. The value portion of each tag is the parameter's initial value. See`priors`

description for definition of each parameter name. Additionally, the tag`fix`

can be set to`TRUE`

to fix the starting values across all chains. If`fix`

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`

,`phi.unif`

,`sigma.sq.ig`

,`nu.unif`

,`kappa.unif`

,`sigma.sq.mu.ig`

,`tau.sq.ig`

. Abundance (`beta`

) 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 are set to 100. The spatial variance parameter,`sigma.sq`

, is assumed to follow an inverse-Gamma distribution. The spatial decay`phi`

, spatial smoothness`nu`

, and negative binomial dispersion`kappa`

parameters are assumed to follow Uniform distributions. The hyperparameters of the inverse-Gamma for`sigma.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`

are the random effect variances for any random effects, 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 effects or of length one if priors are the same for all random effect variances.`tau.sq`

is the residual variance for Gaussian (or zero-inflated Gaussian) models, and it is assigned an inverse-Gamma prior. The hyperparameters of the inverse-Gamma are passed as a vector of length two, with the first and second element corresponding to the shape and scale parameters, respectively.- 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 single numeric value representing the initial variance of the adaptive sampler for

`beta`

,`alpha`

,`beta.star`

(the abundance random effect values),`kappa`

,`phi`

, and`nu`

. See Roberts and Rosenthal (2009) for details. Note that only`phi`

and`nu`

are the only parameters that require tuning for a Gaussian or zero-inflated Gaussian model.- 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 full GP 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),`'Poisson'`

,`'Gaussian'`

, and`'zi-Gaussian'`

.- 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.

- save.fitted
logical value indicating whether or not fitted values and likelihood values should be saved in the resulting model object. If

`save.fitted = FALSE`

, the components`y.rep.samples`

,`mu.samples`

, and`like.samples`

will not be included in the model object, and subsequent functions for calculating WAIC, fitted values, and posterior predictive checks will not work, although they all can be calculated manually if desired. Setting`save.fitted = FALSE`

can be useful when working with very large data sets to minimize the amount of RAM needed when fitting and storing the model object in memory.- ...
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.

## Author

Jeffrey W. Doser doserjef@msu.edu,

Andrew O. Finley finleya@msu.edu

## Value

An object of class `spAbund`

that is a list comprised of:

- beta.samples
a

`coda`

object of posterior samples for the abundance regression coefficients.- kappa.samples
a

`coda`

object of posterior samples for the abundance dispersion parameter. Only included when`family = 'NB'`

.- tau.sq.samples
a

`coda`

object of posterior samples for the Gaussian residual variance parameter. Only included when`family = 'Gaussian'`

or`family = 'zi-Gaussian'`

.- y.rep.samples
a two or three-dimensional object of posterior samples for the abundance replicate (fitted) values with dimensions corresponding to MCMC samples, site, and replicate.

- mu.samples
a two or -three-dimensional array of posterior samples for the expected abundance samples with dimensions corresponding to MCMC samples, site, and replicate.

- 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 effects included in the model. Only included if random effects are specified in`formula`

.- beta.star.samples
a

`coda`

object of posterior samples for the abundance random effects. Only included if random effects are specified in`formula`

.- like.samples
a

`coda`

object of posterior samples for the likelihood value associated with each site. Used for calculating WAIC.- 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.

## Examples

```
set.seed(888)
J.x <- 8
J.y <- 8
J <- J.x * J.y
n.rep <- sample(3, J, replace = TRUE)
beta <- c(0, -1.5, 0.3, -0.8)
p.abund <- length(beta)
mu.RE <- list(levels = c(50, 45),
sigma.sq.mu = c(1.3, 0.5),
beta.indx = c(1, 2))
phi <- 3/.6
sigma.sq <- 2
kappa <- 0.2
sp <- TRUE
cov.model <- 'exponential'
family <- 'NB'
dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta,
kappa = kappa, mu.RE = mu.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
coords <- dat$coords
covs <- list(int = X[, , 1],
abund.cov.1 = X[, , 2],
abund.cov.2 = X[, , 3],
abund.cov.3 = X[, , 4],
abund.factor.1 = X.re[, , 1],
abund.factor.2 = X.re[, , 2])
data.list <- list(y = y, covs = covs, coords = coords)
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 100),
phi.unif = c(3 / 1, 3 / .1),
sigma.sq.ig = c(2, 1),
kappa.unif = c(0.001, 10))
# Starting values
inits.list <- list(beta = beta, kappa = kappa, sigma.sq = sigma.sq, phi = phi)
tuning <- list(phi = 0.3, kappa = 0.05, beta = 0.1, beta.star = 0.1, w = 0.1)
n.batch <- 4
batch.length <- 25
n.burn <- 20
n.thin <- 1
n.chains <- 1
out <- spAbund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 +
(1 | abund.factor.1) + (abund.cov.1 | abund.factor.2),
data = data.list,
n.batch = n.batch,
batch.length = batch.length,
inits = inits.list,
tuning = tuning,
priors = prior.list,
NNGP = TRUE,
cov.model = 'exponential',
search.type = 'cb',
n.neighbors = 5,
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 sigma.sq.mu.ig.
#> Setting prior shape to 0.1 and prior scale to 0.1
#> sigma.sq.mu is not specified in initial values.
#> Setting initial values to random values between 0.05 and 1
#> 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 NNGP Poisson Abundance model fit with 64 sites.
#>
#> Samples per Chain: 100 (4 batches of length 25)
#> Burn-in: 20
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 80
#>
#> Using the exponential spatial correlation model.
#>
#> 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%
#> Parameter Acceptance Tuning
#> beta[1] 48.0 0.10202
#> beta[2] 36.0 0.10000
#> beta[3] 40.0 0.10000
#> beta[4] 40.0 0.09802
#> phi 88.0 0.30606
#> -------------------------------------------------
#> Batch: 2 of 4, 50.00%
#> Parameter Acceptance Tuning
#> beta[1] 32.0 0.10101
#> beta[2] 24.0 0.09900
#> beta[3] 48.0 0.10101
#> beta[4] 68.0 0.09900
#> phi 92.0 0.30914
#> -------------------------------------------------
#> Batch: 3 of 4, 75.00%
#> Parameter Acceptance Tuning
#> beta[1] 28.0 0.10000
#> beta[2] 12.0 0.09802
#> beta[3] 40.0 0.10000
#> beta[4] 44.0 0.10000
#> phi 88.0 0.31224
#> -------------------------------------------------
#> Batch: 4 of 4, 100.00%
summary(out)
#>
#> Call:
#> spAbund(formula = ~abund.cov.1 + abund.cov.2 + abund.cov.3 +
#> (1 | abund.factor.1) + (abund.cov.1 | abund.factor.2), data = data.list,
#> inits = inits.list, priors = prior.list, tuning = tuning,
#> cov.model = "exponential", NNGP = TRUE, n.neighbors = 5,
#> search.type = "cb", 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: 20
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 80
#> Run Time (min): 0.0013
#>
#> Abundance (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.2628 0.2131 -0.5803 -0.2921 0.2442 NA 3
#> abund.cov.1 -2.4345 0.0970 -2.5822 -2.4204 -2.3191 NA 2
#> abund.cov.2 0.7826 0.0734 0.6765 0.7587 1.0044 NA 5
#> abund.cov.3 -0.7686 0.0778 -0.9057 -0.7703 -0.6091 NA 8
#>
#> Abundance Random Effect Variances (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-abund.factor.1 1.0911 0.3591 0.6091 0.9995 1.9510 NA 4
#> abund.cov.1-abund.factor.2 1.9088 0.4733 1.2096 1.8442 2.9181 NA 80
#>
#> Spatial Covariance:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> sigma.sq 0.5528 0.2002 0.1989 0.5352 0.9803 NA 2
#> phi 11.6883 2.5536 8.2529 11.2674 16.8277 NA 10
```