# Function for Fitting Single-Species Hierarchical Distance Sampling Models

`DS.Rd`

Function for fitting single-sepcies hierarchical distance sampling models

## Usage

```
DS(abund.formula, det.formula, data, inits, priors, tuning,
n.batch, batch.length, accept.rate = 0.43, family = 'Poisson',
transect = 'line', det.func = 'halfnormal',
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`

,`covs`

,`dist.breaks`

, and`offset`

.`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 number of distance bins.`covs`

is a matrix or data frame containing the variables used in the the abundance and/or the detection portion of the model, with \(J\) rows for each column (variable).`dist.breaks`

is a vector of distances that denote the breakpoints of the distance bands.`dist.breaks`

should have length equal to the number of columns in`y`

plus one.`offset`

is an offset that can be used to scale estimates from abundance per transect to density per some desired unit of measure. This can be either a single value or a vector with an offset value for each site (e.g., if transects differ in length)- inits
a list with each tag corresponding to a parameter name. Valid tags are

`N`

,`beta`

,`alpha`

,`kappa`

,`sigma.sq.mu`

, and`sigma.sq.p`

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

and`sigma.sq.p`

are only relevant when including random effects in the abundance and detection portion of the distance sampling model, respectively.`kappa`

is only relevant when`family = 'NB'`

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

,`alpha.normal`

,`kappa.unif`

,`sigma.sq.mu.ig`

, and`sigma.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.`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`

and`sigma.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 single numeric value representing the initial variance of the adaptive sampler for

`beta`

,`alpha`

,`beta.star`

(the abundance random effect values),`alpha.star`

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

. 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 number of MCMC samples in each 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'`

.- transect
the type of transect. Currently supports line transects (

`'line'`

) or circular transects (i.e., point counts;`'point'`

).- det.func
the detection model used to describe how detection probability varies with distance. In other software, this is often referred to as the key function. Currently supports two functions: half normal (

`'halfnormal'`

) and negative exponential (`'negexp'`

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

Royle, J. A., Dawson, D. K., & Bates, S. (2004). Modeling abundance effects in distance sampling. Ecology, 85(6), 1591-1597.

## Author

Jeffrey W. Doser doserjef@msu.edu,

## Value

An object of class `DS`

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 when`family = 'NB'`

.- N.samples
a

`coda`

object of posterior samples for the latent abundance values. Note that these values always represent transect-level abundance, even when an offset is supplied.- mu.samples
a

`coda`

object of posterior samples for the latent expected abundance values. When an offset is supplied in the`data`

object, these correspond to expected abundance per unit area (i.e., density).- 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 in`abund.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 in`det.formula`

.- beta.star.samples
a

`coda`

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

.- alpha.star.samples
a

`coda`

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

.- y.rep.samples
a three-dimensional array of fitted values. Array dimensions correspond to MCMC samples, sites, and distance band.

- pi.samples
a three-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, sites, and distance band.

- 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(123)
J.x <- 10
J.y <- 10
J <- J.x * J.y
# Number of distance bins from which to simulate data.
n.bins <- 5
# Length of each bin. This should be of length n.bins
bin.width <- c(.10, .10, .20, .3, .1)
# Abundance coefficients
beta <- c(1.0, 0.2, 0.3, -0.2)
p.abund <- length(beta)
# Detection coefficients
alpha <- c(-1.0, -0.3)
p.det <- length(alpha)
# Detection decay function
det.func <- 'halfnormal'
mu.RE <- list()
p.RE <- list()
sp <- FALSE
family <- 'NB'
kappa <- 0.1
offset <- 1.8
transect <- 'point'
dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width,
beta = beta, alpha = alpha, det.func = det.func, kappa = kappa,
mu.RE = mu.RE, p.RE = p.RE, sp = sp,
sigma.sq = sigma.sq, phi = phi, nu = nu, family = family,
offset = offset, transect = transect)
y <- dat$y
X <- dat$X
X.re <- dat$X.re
X.p <- dat$X.p
X.p.re <- dat$X.p.re
dist.breaks <- dat$dist.breaks
covs <- cbind(X, X.p)
colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3',
'int.det', 'det.cov.1')
data.list <- list(y = y,
covs = covs,
dist.breaks = dist.breaks,
offset = offset)
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 10),
alpha.normal = list(mean = 0,
var = 10),
kappa.unif = c(0, 100))
# Starting values
inits.list <- list(alpha = 0,
beta = 0,
kappa = 1)
# Tuning values
tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1,
kappa = 0.2)
out <- DS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3,
det.formula = ~ det.cov.1,
data = data.list,
n.batch = 10,
batch.length = 25,
inits = inits.list,
family = 'NB',
det.func = 'halfnormal',
transect = 'point',
tuning = tuning,
priors = prior.list,
accept.rate = 0.43,
n.omp.threads = 1,
verbose = TRUE,
n.report = 100,
n.burn = 100,
n.thin = 1,
n.chains = 1)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> N is not specified in initial values.
#> Setting initial values based on observed data
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Negative Binomial HDS model with 100 sites.
#>
#> Samples per Chain: 250 (10 batches of length 25)
#> Burn-in: 100
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 150
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> Adaptive Metropolis with target acceptance rate: 43.0
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Batch: 10 of 10, 100.00%
summary(out)
#>
#> Call:
#> DS(abund.formula = ~abund.cov.1 + abund.cov.2 + abund.cov.3,
#> det.formula = ~det.cov.1, data = data.list, inits = inits.list,
#> priors = prior.list, tuning = tuning, n.batch = 10, batch.length = 25,
#> accept.rate = 0.43, family = "NB", transect = "point", det.func = "halfnormal",
#> n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100,
#> n.thin = 1, n.chains = 1)
#>
#> Samples per Chain: 250
#> Burn-in: 100
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 150
#> Run Time (min): 0.0027
#>
#> Abundance (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.5023 0.2104 0.1102 0.4926 0.9260 NA 9
#> abund.cov.1 0.3290 0.3257 -0.3182 0.4428 0.8054 NA 4
#> abund.cov.2 0.0581 0.2902 -0.5397 0.0895 0.6158 NA 6
#> abund.cov.3 0.0827 0.3370 -0.4069 -0.0058 0.7764 NA 3
#>
#> Detection (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.9379 0.1004 -1.1295 -0.9805 -0.7530 NA 5
#> det.cov.1 -0.2507 0.1540 -0.5626 -0.2147 -0.0574 NA 2
#>
#> NB overdispersion:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> kappa 0.1551 0.0358 0.104 0.1529 0.2384 NA 16
```