# Function for Fitting Univariate Abundance GLMMs

`abund.Rd`

Function for fitting univariate abundance generalized linear (mixed) models

## Usage

```
abund(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, 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`

, 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 a list or data frame containing the variables used in the model. 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\) (or column in a data frame), 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. 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`

,`kappa`

,`sigma.sq.mu`

, and`tau.sq`

. The value portion of each tag is the parameter's initial value.`sigma.sq.mu`

is only relevant when including random effects in the model.`kappa`

is only relevant when`family = 'NB'`

.`tau.sq`

is only relevant when`family = 'Gaussian'`

or`family = 'zi-Gaussian'`

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

,`kappa.unif`

,`sigma.sq.mu.ig`

, and`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 set to 100.`kappa`

is the negative binomial over-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`

are the random effect variances for any abundance 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 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.- 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`

,`beta.star`

(the abundance random effect values), and`kappa`

. See Roberts and Rosenthal (2009) for details. Note that no tuning is necessary for Gaussian or zero-inflated Gaussian models.- 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 hypterthreaded cores. Note,`n.omp.threads`

> 1 might not work on some systems. Currently only relevant for spatially-explicit 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 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.

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

## Author

Jeffrey W. Doser doserjef@msu.edu,

Andrew O. Finley finleya@msu.edu,

## Value

An object of class `abund`

that is a list comprised of:

- beta.samples
a

`coda`

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

`coda`

object of posterior samples for the abundance overdispersion 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 array of posterior samples for the abundance replicate (fitted) values with dimensions corresponding to MCMC samples, site, and an optional third dimension of 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 an optional third dimension of replicate.

- 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 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(1010)
J.x <- 15
J.y <- 15
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(30),
sigma.sq.mu = c(1.3))
kappa <- 0.5
sp <- FALSE
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, family = 'NB')
y <- dat$y
X <- dat$X
X.re <- dat$X.re
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])
data.list <- list(y = y, covs = covs)
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 100),
kappa.unif = c(0.001, 10))
# Starting values
inits.list <- list(beta = 0, kappa = kappa)
tuning <- list(kappa = 0.2, beta = 0.1, beta.star = 0.2)
n.batch <- 5
batch.length <- 25
n.burn <- 0
n.thin <- 1
n.chains <- 1
out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 +
(1 | abund.factor.1),
data = data.list,
n.batch = n.batch,
batch.length = batch.length,
inits = inits.list,
tuning = tuning,
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
#> ----------------------------------------
#> 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
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Poisson abundance model fit with 225 sites.
#>
#> Samples per Chain: 125 (5 batches of length 25)
#> Burn-in: 0
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 125
#>
#> 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 5, 20.00%
#> Parameter Acceptance Tuning
#> beta[1] 40.0 0.10000
#> beta[2] 28.0 0.09802
#> beta[3] 32.0 0.09802
#> beta[4] 40.0 0.10000
#> -------------------------------------------------
#> Batch: 2 of 5, 40.00%
#> Parameter Acceptance Tuning
#> beta[1] 8.0 0.09900
#> beta[2] 16.0 0.09704
#> beta[3] 44.0 0.09900
#> beta[4] 24.0 0.09900
#> -------------------------------------------------
#> Batch: 3 of 5, 60.00%
#> Parameter Acceptance Tuning
#> beta[1] 16.0 0.09802
#> beta[2] 20.0 0.09608
#> beta[3] 32.0 0.09802
#> beta[4] 24.0 0.09802
#> -------------------------------------------------
#> Batch: 4 of 5, 80.00%
#> Parameter Acceptance Tuning
#> beta[1] 24.0 0.09704
#> beta[2] 16.0 0.09512
#> beta[3] 20.0 0.09704
#> beta[4] 28.0 0.09704
#> -------------------------------------------------
#> Batch: 5 of 5, 100.00%
summary(out)
#>
#> Call:
#> abund(formula = ~abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 |
#> abund.factor.1), data = data.list, inits = inits.list, priors = prior.list,
#> tuning = tuning, 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: 125
#> Burn-in: 0
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 125
#> Run Time (min): 0.0015
#>
#> Abundance (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.5845 0.2745 0.2914 0.4465 1.0748 NA 3
#> abund.cov.1 -1.1401 0.4094 -1.5384 -1.2679 -0.2137 NA 2
#> abund.cov.2 0.2462 0.1147 0.1250 0.1991 0.5210 NA 3
#> abund.cov.3 -0.8142 0.1822 -1.0062 -0.8690 -0.2980 NA 4
#>
#> Abundance Random Effect Variances (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-abund.factor.1 1.6958 0.5268 0.869 1.6671 2.7789 NA 16
```