# Function for Fitting Spatial Factor Multivariate Abundance GLMMs

`sfMsAbund.Rd`

The function `sfMsAbund`

fits multivariate spatial abundance GLMMs with species correlations (i.e., a spatially-explicit abundace-based joint species distribution model). We use a spatial factor modeling approach. Currently, models are implemented using a Nearest Neighbor Gaussian Process. Future development may allow for running the models using full Gaussian Processes.

## Usage

```
sfMsAbund(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',
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 two or three-dimensional array of observed count data. The first dimension of the array is equal to the number of species and the second dimension is equal to the number of sites. If specified as a three-dimensional array, the third dimension corresponds to replicate observations at each site (e.g., sub-samples, repeated sampling over multiple seasons).`covs`

is a list containing the variables used in the model. If a data frame, each row of`covs`

is a site and each column is a variable. If a list, 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 model and should have the same dimensions 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.comm`

,`beta`

,`tau.sq.beta`

,`sigma.sq.mu`

,`kappa`

,`phi`

,`lambda`

,`nu`

, and`tau.sq`

.`nu`

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

,`kappa`

is only specified if`family = 'NB'`

,`tau.sq`

is only specified for Gaussian and zero-inflated Gaussian models, and`sigma.sq.mu`

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

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

,`tau.sq.beta.ig`

,`sigma.sq.mu`

,`kappa.unif`

,`phi.unif`

,`nu.unif`

, and`tau.sq.ig`

. Community-level (`beta.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 to 100. Community-level variance parameters (`tau.sq.beta`

) 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 priors are the same for all parameters. If not specified, prior shape and scale parameters are set to 0.1. 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).`sigma.sq.mu`

are the random effect variances 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 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.`tau.sq`

is the species-specific 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 list of length two, with the first and second element corresponding to the shape and scale parameters, respectively, which are each specified as vectors of length equal to the number of species or a single value if priors are the same for all species.- 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`

,`lambda`

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

and`nu`

are tuned for Gaussian or zero-inflated Gaussian models.- 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. If`FALSE`

, a full Gaussian process is used. See Datta et al. (2016) and Finley et al. (2019) for more information. For spatial factor models, only`NNGP = TRUE`

is currently supported.- 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 spatial 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 the number of species in the modeled 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 to run for the adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details.

- accept.rate
target acceptance rate for adaptive MCMC. Defaul is 0.43. See Roberts and Rosenthal (2009) for details.

- family
the distribution to use for the abundance. Currently supports

`'NB'`

(negative binomial),`'Poisson'`

(Poisson),`'Gaussian'`

(Gaussian), and`'zi-Gaussian'`

(zero-inflated 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. Note this is specified in terms of batches and not overall samples for spatial models.

- 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

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.

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 .

Christensen, W. F., and Amemiya, Y. (2002). Latent variable analysis
of multivariate spatial data. *Journal of the American Statistical Association*,
97(457), 302-317.

## Author

Jeffrey W. Doser doserjef@msu.edu,

Andrew O. Finley finleya@msu.edu

## Value

An object of class `sfMsAbund`

that is a list comprised of:

- beta.comm.samples
a

`coda`

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

`coda`

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

`coda`

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

`coda`

object of posterior samples for the species level abundance dispersion parameters. 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'`

.- theta.samples
a

`coda`

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

`coda`

object of posterior samples for the latent spatial factor loadings.- y.rep.samples
a three or four-dimensional array of posterior samples for the fitted (replicate) values for each species with dimensions corresponding to MCMC sample, species, site, and replicate.

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

- w.samples
a three-dimensional array of posterior samples for the latent effects for each latent factor. Array dimensions correspond to MCMC sample, latent factor, 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`

.- beta.star.samples
a

`coda`

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

.- like.samples
a three-dimensional array of posterior samples for the likelihood value associated with each site and species. 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
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(408)
J.x <- 8
J.y <- 8
J <- J.x * J.y
n.rep <- sample(3, size = J, replace = TRUE)
n.sp <- 6
# Community-level covariate effects
beta.mean <- c(-2, 0.5)
p.abund <- length(beta.mean)
tau.sq.beta <- c(0.2, 1.2)
# Random effects (two random intercepts)
mu.RE <- list(levels = c(10, 15),
sigma.sq.mu = c(0.43, 0.5))
# Draw species-level effects from community means.
beta <- matrix(NA, nrow = n.sp, ncol = p.abund)
for (i in 1:p.abund) {
beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i]))
}
sp <- TRUE
n.factors <- 2
factor.model <- TRUE
phi <- runif(n.factors, 3/1, 3 / .1)
kappa <- runif(n.sp, 0.1, 1)
dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta,
mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB',
factor.model = factor.model, phi = phi,
cov.model = 'exponential', n.factors = n.factors)
y <- dat$y
X <- dat$X
X.re <- dat$X.re
coords <- dat$coords
# Package all data into a list
covs <- list(int = X[, , 1],
abund.cov.1 = X[, , 2],
abund.factor.1 = X.re[, , 1],
abund.factor.2 = X.re[, , 2])
data.list <- list(y = y, covs = covs, coords = coords)
prior.list <- list(beta.comm.normal = list(mean = 0, var = 100),
kappa.unif = list(a = 0, b = 10),
phi.unif = list(a = 3 / 1, b = 3 / .1),
tau.sq.beta.ig = list(a = .1, b = .1))
inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5,
tau.sq.beta = 1, phi = 3 / 0.5)
# Small
n.batch <- 2
batch.length <- 25
n.burn <- 20
n.thin <- 1
n.chains <- 1
out <- sfMsAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) +
(1 | abund.factor.2),
data = data.list,
n.batch = n.batch,
inits = inits.list,
priors = prior.list,
NNGP = TRUE,
cov.model = 'exponential',
n.neighbors = 5,
n.factors = n.factors,
batch.length = batch.length,
n.omp.threads = 3,
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
#> 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 NNGP Multi-species Poisson Abundance
#> model fit with 64 sites and 6 species.
#>
#> Samples per Chain: 50
#> Burn-in: 20
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 30
#>
#> Using the exponential spatial correlation model.
#>
#> Using 2 latent spatial factors.
#> Using 5 nearest neighbors.
#>
#> Source compiled with OpenMP support and model fit using 3 thread(s).
#>
#> Adaptive Metropolis with target acceptance rate: 43.0
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Batch: 1 of 2, 50.00%
#> Number Parameter Acceptance Tuning
#> 1 beta[1] 8.0 0.98020
#> 2 beta[1] 12.0 0.98020
#> 3 beta[1] 16.0 0.98020
#> 4 beta[1] 28.0 0.98020
#> 5 beta[1] 4.0 0.98020
#> 6 beta[1] 16.0 0.98020
#> 1 phi 52.0 1.02020
#> 2 phi 52.0 1.00000
#> -------------------------------------------------
#> Batch: 2 of 2, 100.00%
summary(out)
#>
#> Call:
#> sfMsAbund(formula = ~abund.cov.1 + (1 | abund.factor.1) + (1 |
#> abund.factor.2), data = data.list, inits = inits.list, priors = prior.list,
#> cov.model = "exponential", NNGP = TRUE, n.neighbors = 5,
#> n.factors = n.factors, n.batch = n.batch, batch.length = batch.length,
#> n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn,
#> n.thin = n.thin, n.chains = n.chains)
#>
#> Samples per Chain: 50
#> Burn-in: 20
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 30
#> Run Time (min): 0.002
#>
#> ----------------------------------------
#> Community Level
#> ----------------------------------------
#> Abundance Means (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -1.7528 0.2378 -2.3031 -1.7170 -1.3813 NA 30
#> abund.cov.1 -0.0418 0.4636 -0.9572 0.0266 0.7454 NA 30
#>
#> Abundance Variances (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.3666 0.3850 0.1143 0.2285 1.3069 NA 30
#> abund.cov.1 1.0228 0.8838 0.2231 0.6803 3.2556 NA 30
#>
#> Abundance Random Effect Variances (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-abund.factor.1 1.8336 0.4021 1.2779 1.8598 2.6484 NA 15
#> (Intercept)-abund.factor.2 0.8821 0.1625 0.6261 0.8977 1.1187 NA 15
#>
#> ----------------------------------------
#> Species Level
#> ----------------------------------------
#> Abundance (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 -2.1287 0.0564 -2.2121 -2.1490 -2.0374 NA 15
#> (Intercept)-sp2 -1.6456 0.1579 -2.0426 -1.7185 -1.4016 NA 5
#> (Intercept)-sp3 -1.3096 0.1398 -1.6076 -1.2115 -1.1974 NA 4
#> (Intercept)-sp4 -2.4255 0.2249 -3.0024 -2.4218 -2.0768 NA 9
#> (Intercept)-sp5 -1.4365 0.0035 -1.4410 -1.4358 -1.4358 NA 30
#> (Intercept)-sp6 -1.6156 0.1421 -1.7774 -1.6895 -1.3327 NA 4
#> abund.cov.1-sp1 -0.9582 0.1660 -1.2081 -0.8834 -0.7866 NA 5
#> abund.cov.1-sp2 0.4971 0.0439 0.4322 0.4956 0.5453 NA 3
#> abund.cov.1-sp3 -0.1752 0.2293 -0.3695 -0.3695 0.1858 NA 2
#> abund.cov.1-sp4 -0.5948 0.1582 -0.7631 -0.5522 -0.1690 NA 6
#> abund.cov.1-sp5 0.8422 0.0000 0.8422 0.8422 0.8422 NA 0
#> abund.cov.1-sp6 0.6625 0.1337 0.5611 0.5826 0.8480 NA 2
#>
#> ----------------------------------------
#> Spatial Covariance
#> ----------------------------------------
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> phi-1 12.5747 5.9406 5.3640 10.3262 26.3982 NA 3
#> phi-2 7.8485 1.6030 5.5773 7.8450 10.9598 NA 30
```