# Function for Fitting Spatially-Varying Coefficient Multivariate Abundance GLMMs

`svcMsAbund.Rd`

The function `svcMsAbund`

fits multivariate spatially-varying coefficient GLMs with species correlations (i.e., a spatially-explicit abundace-based joint species distribution model). We use a spatial factor modeling approach. Models are implemented using a Nearest Neighbor Gaussian Process.

## Usage

```
svcMsAbund(formula, data, inits, priors, tuning,
svc.cols = 1, cov.model = 'exponential', NNGP = TRUE,
n.neighbors = 15, search.type = 'cb', n.factors,
n.batch, batch.length, accept.rate = 0.43, family = 'Gaussian',
n.omp.threads = 1, verbose = TRUE, n.report = 100,
n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1,
...)
```

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

,`coords`

, and`z`

.`y`

is a matrix with sites corresponding to species and columns corresponding to sites.`covs`

is a list, matrix, or data frame of covariates used in the model, where each column (or list element) represents a different covariate.`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`

.- inits
a list with each tag corresponding to a parameter name. Valid tags are

`beta.comm`

,`beta`

,`tau.sq.beta`

,`sigma.sq.mu`

,`phi`

,`lambda`

,`nu`

, and`tau.sq`

.`nu`

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

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

,`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. If desired, the species-specific regression coefficients (`beta`

) can also be estimated indepdendently by specifying the tag`independent.betas = TRUE`

. If specified, this will not estimate species-specific coefficients as random effects from a common-community-level distribution, and rather the values of`beta.comm`

and`tau.sq.beta`

will be fixed at the specified initial values. This is equivalent to specifying a Gaussian, independent prior for each of the species-specific effects. The spatial factor model fits`n.factors`

independent spatial processes. The spatial decay`phi`

and smoothness`nu`

parameters for each latent factor and spatially-varying coefficient 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 equal to the number of spatial factors times the number of spatially-varying coefficients corresponding to the lower and upper support, respectively, or as a single value if the same value is assigned for all factors and spatially-varying coefficients. 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 for each spatially-varying coefficient 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.`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

`phi`

and`nu`

. See Roberts and Rosenthal (2009) for details.- svc.cols
a vector indicating the variables whose effects will be estimated as spatially-varying coefficients.

`svc.cols`

can be an integer vector with values indicating the order of covariates specified in the model formula (with 1 being the intercept if specified), or it can be specified as a character vector with names corresponding to variable names in`occ.covs`

(for the intercept, use`'(Intercept)'`

).`svc.cols`

default argument of 1 results in a spatial factor model analogous to`sfMsAbund`

(assuming an intercept is included in the model).- 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 for each spatially-varying coefficient. 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. Default is 0.43. See Roberts and Rosenthal (2009) for details.

- family
the distribution to use for abundance. Currently, spatially-varying coefficient models are available for

`family = 'Gaussian'`

and`family = '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. 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.

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

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.- tau.sq.samples
a

`coda`

object of posterior samples for the Gaussian residual variance parameter.- 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 for each spatially-varying coefficient.- 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 four-dimensional array of posterior samples for the latent spatial random effects for each spatial factor within each spatially-varying coefficient. Dimensions correspond to MCMC sample, factor, site, and spatially-varying coefficient.

- 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(332)
J.x <- 10
J.y <- 10
J <- J.x * J.y
n.rep <- rep(1, J)
n.sp <- 6
# Community-level covariate effects
beta.mean <- c(0, 0.25, 0.6)
p.abund <- length(beta.mean)
tau.sq.beta <- c(0.2, 1.2, 0.4)
# Random effects
mu.RE <- list()
# 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
svc.cols <- c(1, 2)
n.factors <- 2
q.p.svc <- length(svc.cols) * n.factors
factor.model <- TRUE
phi <- runif(q.p.svc, 3/1, 3 / .4)
tau.sq <- runif(n.sp, 0.1, 5)
cov.model <- 'exponential'
family <- 'Gaussian'
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, tau.sq = tau.sq, family = family,
factor.model = factor.model, phi = phi,
cov.model = cov.model, n.factors = n.factors,
svc.cols = svc.cols)
y <- dat$y
X <- dat$X
coords <- dat$coords
covs <- data.frame(abund.cov.1 = X[, 2],
abund.cov.2 = X[, 3])
data.list <- list(y = y, covs = covs, coords = coords)
prior.list <- list(beta.comm.normal = list(mean = 0, var = 100),
tau.sq.ig = list(a = 2, b = 2),
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,
tau.sq = 1,
tau.sq.beta = 1,
phi = 3 / 0.5)
tuning.list <- list(phi = 0.5)
n.batch <- 5
batch.length <- 25
n.burn <- 0
n.thin <- 1
n.chains <- 1
out <- svcMsAbund(formula = ~ abund.cov.1 + abund.cov.2,
data = data.list,
n.batch = n.batch,
inits = inits.list,
priors = prior.list,
tuning = tuning.list,
NNGP = TRUE,
svc.cols = c(1, 2),
family = 'Gaussian',
cov.model = 'exponential',
n.neighbors = 5,
n.factors = n.factors,
batch.length = batch.length,
n.omp.threads = 1,
verbose = TRUE,
n.report = 20,
n.burn = n.burn,
n.thin = n.thin,
n.chains = n.chains)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> lambda is not specified in initial values.
#> Setting initial values of the lower triangle to random values from a standard normal
#> 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 Gaussian Model
#> with 100 sites and 6 species.
#>
#> Samples per chain: 125 (5 batches of length 25)
#> Burn-in: 0
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 125
#>
#> Number of spatially-varying coefficients: 2
#> Using the exponential spatial correlation model.
#>
#> Using 2 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: 5 of 5, 100.00%
summary(out)
#>
#> Call:
#> svcMsAbund(formula = ~abund.cov.1 + abund.cov.2, data = data.list,
#> inits = inits.list, priors = prior.list, tuning = tuning.list,
#> svc.cols = c(1, 2), cov.model = "exponential", NNGP = TRUE,
#> n.neighbors = 5, n.factors = n.factors, n.batch = n.batch,
#> batch.length = batch.length, family = "Gaussian", n.omp.threads = 1,
#> verbose = TRUE, n.report = 20, 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.0052
#>
#> ----------------------------------------
#> Community Level
#> ----------------------------------------
#> Abundance Means:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.0542 0.1917 -0.4600 -0.0573 0.3211 NA 125
#> abund.cov.1 1.0396 0.7121 -0.5156 1.0743 2.4272 NA 178
#> abund.cov.2 0.7998 0.3149 0.0721 0.8076 1.4543 NA 125
#>
#> Abundance Variances:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.2153 0.1534 0.0515 0.1680 0.6474 NA 125
#> abund.cov.1 3.4555 2.1769 0.8063 2.9347 8.9049 NA 84
#> abund.cov.2 0.8790 0.7101 0.1894 0.6292 2.6752 NA 125
#>
#> ----------------------------------------
#> Species Level
#> ----------------------------------------
#> Abundance:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 -0.2738 0.2220 -0.7279 -0.2698 0.1581 NA 44
#> (Intercept)-sp2 -0.1709 0.1839 -0.5230 -0.1298 0.1139 NA 13
#> (Intercept)-sp3 -0.0559 0.1727 -0.3895 -0.0350 0.2285 NA 13
#> (Intercept)-sp4 -0.2098 0.1726 -0.5356 -0.2027 0.1271 NA 125
#> (Intercept)-sp5 0.5543 0.1344 0.3022 0.5678 0.7906 NA 45
#> (Intercept)-sp6 -0.1313 0.1800 -0.4784 -0.1268 0.2268 NA 33
#> abund.cov.1-sp1 1.5979 0.3086 1.0609 1.6102 2.1489 NA 17
#> abund.cov.1-sp2 3.5170 0.3154 2.8446 3.5041 4.0708 NA 4
#> abund.cov.1-sp3 -0.0710 0.3751 -0.8439 -0.0092 0.5400 NA 5
#> abund.cov.1-sp4 1.0396 0.3031 0.4402 1.0019 1.6204 NA 9
#> abund.cov.1-sp5 -0.8378 0.2672 -1.4312 -0.8019 -0.3783 NA 6
#> abund.cov.1-sp6 0.8199 0.2415 0.4612 0.7867 1.3247 NA 26
#> abund.cov.2-sp1 0.6850 0.1964 0.3121 0.6789 1.0219 NA 64
#> abund.cov.2-sp2 1.7650 0.1424 1.5059 1.7672 2.0264 NA 21
#> abund.cov.2-sp3 0.1106 0.1590 -0.1943 0.1126 0.3979 NA 34
#> abund.cov.2-sp4 0.3916 0.1640 0.0848 0.4093 0.7106 NA 85
#> abund.cov.2-sp5 1.6976 0.1126 1.4576 1.6919 1.8929 NA 60
#> abund.cov.2-sp6 0.1440 0.1328 -0.1169 0.1622 0.4162 NA 83
#>
#> ----------------------------------------
#> Spatial Covariance
#> ----------------------------------------
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> phi-1-(Intercept) 8.6541 4.0412 3.2935 8.1981 16.4235 NA 5
#> phi-2-(Intercept) 5.2898 1.6956 3.1108 5.2662 9.3440 NA 4
#> phi-1-abund.cov.1 6.1902 1.4954 4.0207 5.8134 8.8614 NA 12
#> phi-2-abund.cov.1 10.5151 5.7228 3.7901 8.3002 23.5328 NA 3
#>
#> ----------------------------------------
#> Residual variance
#> ----------------------------------------
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> sp1 3.5358 0.7356 2.2994 3.4528 5.1541 NA 46
#> sp2 0.8118 0.3225 0.4413 0.7347 1.4865 NA 27
#> sp3 1.1220 0.3979 0.5474 1.0883 1.9401 NA 11
#> sp4 2.4152 0.4358 1.7619 2.3393 3.4294 NA 55
#> sp5 1.1259 0.2518 0.7408 1.1059 1.6417 NA 13
#> sp6 1.9704 0.3284 1.4644 1.9496 2.6776 NA 95
```