# Function for Fitting Spatial Factor Multi-Species Hierarchical Distance Sampling Models

`sfMsDS.Rd`

Function for fitting spatial factor multi-species hierarchical distance sampling models.

## Usage

```
sfMsDS(abund.formula, det.formula, data, inits, priors,
tuning, cov.model = 'exponential', NNGP = TRUE,
n.neighbors = 15, search.type = 'cb', n.factors,
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`

,`coords`

,`dist.breaks`

, and`offset`

.`y`

is a three-dimensional array of observed count data with first dimension equal to the number of species, second dimension equal to the number of sites, and third dimension equal to the maximum number of replicates at a given site.`covs`

is a matrix or data frame containing the variables used in 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 third dimension of`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).`coords`

is a matrix or data frame with two columns that contain the spatial coordinates of each site. Note that`spAbundance`

assumes coordinates are specified in a projected coordinate system.- inits
a list with each tag corresponding to a parameter name. Valid tags are

`alpha.comm`

,`beta.comm`

,`beta`

,`alpha`

,`tau.sq.beta`

,`tau.sq.alpha`

,`sigma.sq.mu`

,`sigma.sq.p`

,`phi`

,`nu`

,`lambda`

,`w`

,`kappa`

, and`N`

.`sigma.sq.mu`

and`sigma.sq.p`

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

is only relevant when`family = 'NB'`

.`nu`

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

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

,`alpha.comm.normal`

,`tau.sq.beta.ig`

,`tau.sq.alpha.ig`

,`sigma.sq.mu.ig`

,`sigma.sq.p.ig`

,`kappa.unif`

,`phi.unif`

, and`nu.unif`

. Community-level abundance (`beta.comm`

) and detection (`alpha.comm`

) 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. Community-level variance parameters for abundance (`tau.sq.beta`

) and detection (`tau.sq.alpha`

) 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, which are each specified as vectors of length equal to the number of coefficients to be estimated or a single value if all parameters are assigned the same prior. If not specified, prior shape and scale parameters are set to 0.1.`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 effects or of length one if priors are the same for all random effect variances.`kappa`

is the negative binomial dispersion parameter for each species and is assumed to follow a uniform distribution. The hyperparameters of the uniform distribution are passed as a list of length two with first and second elements corresponding to the lower and upper bounds of the uniform distribution, respectively, which are each specified as vectors of length equal to the number of species or of length one if priors are the same for all species-specific dispersion parameters. The spatial factor model fits`n.factors`

independent spatial processes. The spatial decay`phi`

and smoothness`nu`

parameters for each latent factor are assumed to follow Uniform distributions. The hyperparameters of the Uniform are passed as a list with two elements, with both elements being vectors of length`n.factors`

corresponding to the lower and upper support, respectively, or as a single value if the same value is assigned for all factors. The priors for the factor loadings matrix`lambda`

are fixed following the standard spatial factor model to ensure parameter identifiability (Christensen and Amemlya 2002). The upper triangular elements of the`n.sp x n.factors`

matrix are fixed at 0 and the diagonal elements are fixed at 1. The lower triangular elements are assigned a standard normal prior (i.e., mean 0 and variance 1).- 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`

,`lambda`

(the latent factor loadings),`w`

(the latent factors),`beta.star`

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

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

. See Roberts and Rosenthal (2009) for details.- 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"`

.- 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 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.factors
the number of factors to use in the latent factor model approach. Typically, the number of factors is set to be small (e.g., 4-5) relative to the total number of species in the community, which will lead to substantial decreases in computation time. However, the value can be anywhere between 1 and N (the number of species in the community).

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

.- 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. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.

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
.

Sollmann, R., Gardner, B., Williams, K. A., Gilbert, A. T., & Veit, R. R. (2016). A hierarchical distance sampling model to estimate abundance and covariate associations of species and communities. Methods in Ecology and Evolution, 7(5), 529-537.

## Author

Jeffrey W. Doser doserjef@msu.edu,

Andrew O. Finley finleya@msu.edu

## Value

An object of class `sfMsDS`

that is a list comprised of:

- beta.comm.samples
a

`coda`

object of posterior samples for the community level abundance regression coefficients.- alpha.comm.samples
a

`coda`

object of posterior samples for the community level detection regression coefficients.- tau.sq.beta.samples
a

`coda`

object of posterior samples for the abundance community variance parameters.- tau.sq.alpha.samples
a

`coda`

object of posterior samples for the detection community variance parameters.- beta.samples
a

`coda`

object of posterior samples for the species level abundance regression coefficients.- alpha.samples
a

`coda`

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

`coda`

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

.- theta.samples
a

`coda`

object of posterior samples for the spatial correlation parameters for each spatial factor.- lambda.samples
a

`coda`

object of posterior samples for the spatial factor loadings.- w.samples
a three-dimensional array of posterior samples for the latent effects for each spatial factor. Array dimensions correspond to MCMC sample, spatial factor, then site.

- N.samples
a three-dimensional array of posterior samples for the latent abundance values for each species. Note that these values always represent transect-level abundance, even when an offset is supplied. Array dimensions correspond to MCMC sample, species, then site.

- mu.samples
a three-dimensional array of posterior samples for the latent expected abundance values for each species. When an offset is supplied in the

`data`

object, these correspond to expected abundance per unit area (i.e., density). Array dimensions correspond to MCMC sample, species, then site.- 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 four-dimensional array of fitted values. Array dimensions correspond to MCMC samples, species, sites, and distance band.

- pi.samples
a four-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, species, 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
MCMC sampler 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(210)
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)
# Number of species
n.sp <- 5
# Community-level abundance coefficients
beta.mean <- c(-1, 0.2, 0.3, -0.2)
p.abund <- length(beta.mean)
tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4)
# Detection coefficients
alpha.mean <- c(-1.0, -0.3)
p.det <- length(alpha.mean)
tau.sq.alpha <- c(0.1, 0.2)
# Detection decay function
det.func <- 'halfnormal'
mu.RE <- list()
p.RE <- list()
# Draw species-level effects from community means.
beta <- matrix(NA, nrow = n.sp, ncol = p.abund)
alpha <- matrix(NA, nrow = n.sp, ncol = p.det)
for (i in 1:p.abund) {
beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i]))
}
for (i in 1:p.det) {
alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i]))
}
sp <- TRUE
family <- 'Poisson'
kappa <- runif(n.sp, 0.3, 3)
offset <- pi * .8^2
transect <- 'line'
factor.model <- TRUE
n.factors <- 3
phi <- runif(n.factors, 3 / 1, 3 / .2)
cov.model <- 'exponential'
dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width,
n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func,
mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model,
sigma.sq = sigma.sq, phi = phi, nu = nu, family = family,
offset = offset, transect = transect, factor.model = factor.model,
n.factors = n.factors)
#> sigma.sq is specified but will be set to 1 for spatial latent factor model
y <- dat$y
X <- dat$X
X.p <- dat$X.p
coords <- dat$coords
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,
coords = coords,
offset = offset)
# Priors
prior.list <- list(beta.comm.normal = list(mean = 0, var = 10),
alpha.comm.normal = list(mean = 0, var = 10),
kappa.unif = list(0, 100),
phi.unif = list(3 / 1, 3 / .1),
tau.sq.beta.ig = list(a = 0.1, b = 0.1),
tau.sq.alpha.ig = list(a = 0.1, b = 0.1))
# Starting values
inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0,
alpha = 0, kappa = 1, phi = 3 / .5)
tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1,
kappa = 0.8, lambda = 1, w = 1, phi = 0.8)
n.batch <- 4
batch.length <- 25
n.burn <- 0
n.thin <- 1
n.chains <- 1
out <- sfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3,
det.formula = ~ det.cov.1,
data = data.list,
n.batch = n.batch,
batch.length = batch.length,
inits = inits.list,
family = 'Poisson',
det.func = 'halfnormal',
transect = transect,
tuning = tuning,
cov.model = 'exponential',
NNGP = TRUE,
n.neighbors = 5,
n.factors = n.factors,
priors = prior.list,
accept.rate = 0.43,
n.omp.threads = 1,
verbose = TRUE,
n.report = 10,
n.burn = n.burn,
n.thin = n.thin,
n.chains = n.chains)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> N is not specified in initial values.
#> Setting initial values based on observed data
#> tau.sq.beta is not specified in initial values.
#> Setting initial values to random values between 0.05 and 1
#> tau.sq.alpha is not specified in initial values.
#> Setting to initial values to random values between 0.05 and 1
#> lambda is not specified in initial values.
#> Setting initial values of the lower triangle to 0
#> 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 Factor Multi-species Poisson HDS model with 100 sites and 5 species.
#>
#> 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 exponential spatial correlation model.
#>
#> Using 3 latent spatial factors.
#> 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: 4 of 4, 100.00%
summary(out, level = 'community')
#>
#> Call:
#> sfMsDS(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, cov.model = "exponential",
#> NNGP = TRUE, n.neighbors = 5, n.factors = n.factors, n.batch = n.batch,
#> batch.length = batch.length, accept.rate = 0.43, family = "Poisson",
#> transect = transect, det.func = "halfnormal", n.omp.threads = 1,
#> verbose = TRUE, n.report = 10, 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.0093
#>
#> ----------------------------------------
#> Community Level
#> ----------------------------------------
#> Abundance Means (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.5069 0.3357 -1.2624 -0.5194 0.0935 NA 48
#> abund.cov.1 0.3225 0.3142 -0.3387 0.3024 1.0038 NA 100
#> abund.cov.2 0.1524 0.3627 -0.4611 0.1752 0.8932 NA 97
#> abund.cov.3 0.1330 0.2070 -0.2629 0.1527 0.5515 NA 100
#>
#> Abundance Variances (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.5508 0.7917 0.0630 0.3125 3.1385 NA 23
#> abund.cov.1 0.5969 0.5766 0.1122 0.4392 2.3833 NA 39
#> abund.cov.2 0.8351 0.8799 0.0469 0.6047 3.3462 NA 4
#> abund.cov.3 0.2275 0.2512 0.0461 0.1525 0.8571 NA 22
#>
#> Detection Means (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.5865 0.3530 -1.1001 -0.6778 0.1216 NA 4
#> det.cov.1 -0.4044 0.3124 -0.8524 -0.4782 0.3304 NA 36
#> Detection Variances (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.2612 0.2914 0.0363 0.1764 1.0183 NA 27
#> det.cov.1 0.3255 0.5751 0.0397 0.1737 2.0532 NA 43
#>
#> ----------------------------------------
#> Spatial Covariance
#> ----------------------------------------
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> phi-1 4.2111 1.0927 3.0658 3.9839 6.8047 NA 10
#> phi-2 12.3522 3.9712 5.7008 12.0353 21.3153 NA 12
#> phi-3 6.9817 1.4671 4.0350 6.8446 9.9680 NA 15
```