# Function for Fitting Single-Species Spatially-Varying Coefficient Occupancy Models Using Polya-Gamma Latent Variables

`svcPGOcc.Rd`

The function `svcPGOcc`

fits single-species spatially-varying coefficient occupancy models using Polya-Gamma latent variables. Models are fit using Nearest Neighbor Gaussian Processes.

## Usage

```
svcPGOcc(occ.formula, det.formula, data, inits, priors,
tuning, svc.cols = 1, cov.model = "exponential", NNGP = TRUE,
n.neighbors = 15, search.type = "cb", n.batch,
batch.length, accept.rate = 0.43,
n.omp.threads = 1, verbose = TRUE, n.report = 100,
n.burn = round(.10 * n.batch * batch.length),
n.thin = 1, n.chains = 1, k.fold, k.fold.threads = 1,
k.fold.seed = 100, k.fold.only = FALSE, ...)
```

## Arguments

- occ.formula
a symbolic description of the model to be fit for the occurrence portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts 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 are allowed using lme4 syntax (Bates et al. 2015).

- data
a list containing data necessary for model fitting. Valid tags are

`y`

,`occ.covs`

,`det.covs`

, and`coords`

.`y`

is the detection-nondetection data 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.`occ.covs`

is a matrix or data frame containing the variables used in the occupancy portion of the model, with \(J\) rows for each column (variable).`det.covs`

is a list of variables included in the detection portion of the model. Each list element is a different detection covariate, which can be site-level or observational-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 replicates at a given site.`coords`

is a \(J \times 2\) matrix of the observation coordinates. Note that`spOccupancy`

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

`z`

,`beta`

,`alpha`

,`sigma.sq`

,`phi`

,`w`

,`nu`

,`sigma.sq.psi`

,`sigma.sq.p`

.`nu`

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

,`sigma.sq.p`

is only specified if there are random effects in`det.formula`

, and`sigma.sq.psi`

is only specified if there are random effects in`occ.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.normal`

,`alpha.normal`

,`phi.unif`

,`sigma.sq.ig`

,`sigma.sq.unif`

,`nu.unif`

,`sigma.sq.psi.ig`

, and`sigma.sq.p.ig`

. Occurrence (`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 2.73. The spatial variance parameter,`sigma.sq`

, is assumed to follow an inverse-Gamma distribution or a uniform distribution (default is inverse-Gamma). The spatial decay`phi`

and smoothness`nu`

parameters are assumed to follow Uniform distributions. The hyperparameters of the inverse-Gamma for`sigma.sq`

are passed as a list with two elements corresponding to the shape and scale parameters, respetively, with each element comprised of a vector equal to the number of spatially-varying coefficients to be estimated or of length one if priors are the same for all coefficients. The hyperparameters of any uniform priors are also passed as a list of length two with the first and second elements corresponding to the lower and upper support, respectively, which can be passed as a vector equal to the total number of spatially-varying coefficients to be estimated or of length one if priors are the same for all coefficients.`sigma.sq.psi`

and`sigma.sq.p`

are the random effect variances for any occurrence 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 the 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.- 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 occupancy model analogous to`spPGOcc`

(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"`

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

`phi`

,`nu`

, and`sigma.sq`

. The value portion of each tag defines the initial variance of the Adaptive sampler. See Roberts and Rosenthal (2009) for details.- 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. Only`NNGP = TRUE`

is currently supported for spatially-varying coefficient models.- 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 or k-fold cross-validation.- 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.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.

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

- n.burn
the number of samples out of the total

`n.batch * batch.length`

samples in each chain to discard as burn-in. 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 MCMC chains to run in sequence.

- k.fold
specifies the number of

*k*folds for cross-validation. If not specified as an argument, then cross-validation is not performed and`k.fold.threads`

and`k.fold.seed`

are ignored. In*k*-fold cross-validation, the data specified in`data`

is randomly partitioned into*k*equal sized subsamples. Of the*k*subsamples,*k*- 1 subsamples are used to fit the model and the remaining*k*samples are used for prediction. The cross-validation process is repeated*k*times (the folds). As a scoring rule, we use the model deviance as described in Hooten and Hobbs (2015). Cross-validation is performed after the full model is fit using all the data. Cross-validation results are reported in the`k.fold.deviance`

object in the return list.- k.fold.threads
number of threads to use for cross-validation. If

`k.fold.threads > 1`

parallel processing is accomplished using the foreach and doParallel packages. Ignored if`k.fold`

is not specified.- k.fold.seed
seed used to split data set into

`k.fold`

parts for k-fold cross-validation. Ignored if`k.fold`

is not specified.- k.fold.only
a logical value indicating whether to only perform cross-validation (

`TRUE`

) or perform cross-validation after fitting the full model (`FALSE`

). Default value is`FALSE`

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

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
.

Finley, A. O., and Banerjee, S. (2020). Bayesian spatially varying coefficient
models in the spBayes R package. *Environmental Modelling and Software*,
125, 104608.

Polson, N.G., J.G. Scott, and J. Windle. (2013) Bayesian Inference for
Logistic Models Using Polya-Gamma Latent Variables.
*Journal of the American Statistical Association*, 108:1339-1349.

Roberts, G.O. and Rosenthal J.S. (2009) Examples of adaptive MCMC.
*Journal of Computational and Graphical Statistics*, 18(2):349-367.

## Author

Jeffrey W. Doser doserjef@msu.edu,

Andrew O. Finley finleya@msu.edu

## Value

An object of class `svcPGOcc`

that is a list comprised of:

- beta.samples
a

`coda`

object of posterior samples for the occurrence regression coefficients.- alpha.samples
a

`coda`

object of posterior samples for the detection regression coefficients.- z.samples
a

`coda`

object of posterior samples for the latent occurrence values- psi.samples
a

`coda`

object of posterior samples for the latent occurrence probability values- theta.samples
a

`coda`

object of posterior samples for spatial covariance parameters.- w.samples
a three-dimensional array of posterior samples for the latent spatial random effects for all spatially-varying coefficients. Dimensions correspond to MCMC sample, coefficient, and sites.

- sigma.sq.psi.samples
a

`coda`

object of posterior samples for variances of random intercepts included in the occupancy portion of the model. Only included if random intercepts are specified in`occ.formula`

.- sigma.sq.p.samples
a

`coda`

object of posterior samples for variances of random intercpets included in the detection portion of the model. Only included if random intercepts are specified in`det.formula`

.- beta.star.samples
a

`coda`

object of posterior samples for the occurrence random effects. Only included if random intercepts are specified in`occ.formula`

.- alpha.star.samples
a

`coda`

object of posterior samples for the detection random effects. Only included if random intercepts are specified in`det.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()`

.- k.fold.deviance
soring rule (deviance) from k-fold cross-validation. Only included if

`k.fold`

is specified in function call.

The return object will include additional objects used for
subsequent prediction and/or model fit evaluation. Note that detection
probability values are not included in the model object, but can be
extracted using `fitted()`

.

## Examples

```
set.seed(400)
# Simulate Data -----------------------------------------------------------
J.x <- 8
J.y <- 8
J <- J.x * J.y
n.rep <- sample(2:4, J, replace = TRUE)
beta <- c(0.5, 2)
p.occ <- length(beta)
alpha <- c(0, 1)
p.det <- length(alpha)
phi <- c(3 / .6, 3 / .8)
sigma.sq <- c(1.2, 0.7)
svc.cols <- c(1, 2)
dat <- simOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha,
sigma.sq = sigma.sq, phi = phi, sp = TRUE, cov.model = 'exponential',
svc.cols = svc.cols)
# Detection-nondetection data
y <- dat$y
# Occupancy covariates
X <- dat$X
# Detection covarites
X.p <- dat$X.p
# Spatial coordinates
coords <- dat$coords
# Package all data into a list
occ.covs <- X[, -1, drop = FALSE]
colnames(occ.covs) <- c('occ.cov')
det.covs <- list(det.cov.1 = X.p[, , 2])
data.list <- list(y = y,
occ.covs = occ.covs,
det.covs = det.covs,
coords = coords)
# Number of batches
n.batch <- 10
# Batch length
batch.length <- 25
n.iter <- n.batch * batch.length
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 2.72),
alpha.normal = list(mean = 0, var = 2.72),
sigma.sq.ig = list(a = 2, b = 1),
phi.unif = list(a = 3/1, b = 3/.1))
# Initial values
inits.list <- list(alpha = 0, beta = 0,
phi = 3 / .5,
sigma.sq = 2,
w = matrix(0, nrow = length(svc.cols), ncol = nrow(X)),
z = apply(y, 1, max, na.rm = TRUE))
# Tuning
tuning.list <- list(phi = 1)
out <- svcPGOcc(occ.formula = ~ occ.cov,
det.formula = ~ det.cov.1,
data = data.list,
inits = inits.list,
n.batch = n.batch,
batch.length = batch.length,
accept.rate = 0.43,
priors = prior.list,
cov.model = 'exponential',
svc.cols = c(1, 2),
tuning = tuning.list,
n.omp.threads = 1,
verbose = TRUE,
NNGP = TRUE,
n.neighbors = 5,
search.type = 'cb',
n.report = 10,
n.burn = 50,
n.thin = 1)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> NNGP Occupancy model with Polya-Gamma latent
#> variable fit with 64 sites.
#>
#> Samples per chain: 250 (10 batches of length 25)
#> Burn-in: 50
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 200
#>
#> Number of spatially-varying coefficients: 2
#> Using the exponential spatial correlation model.
#>
#> 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: 10 of 10, 100.00%
summary(out)
#>
#> Call:
#> svcPGOcc(occ.formula = ~occ.cov, det.formula = ~det.cov.1, 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, search.type = "cb", n.batch = n.batch, batch.length = batch.length,
#> accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10,
#> n.burn = 50, n.thin = 1)
#>
#> Samples per Chain: 250
#> Burn-in: 50
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 200
#> Run Time (min): 0.0055
#>
#> Occurrence (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.2615 0.4253 -1.1120 -0.2473 0.5345 NA 41
#> occ.cov 3.1321 0.8543 1.5775 3.1197 4.7794 NA 23
#>
#> Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.0420 0.2965 -0.568 -0.0574 0.5615 NA 137
#> det.cov.1 1.1061 0.2822 0.624 1.0838 1.6466 NA 145
#>
#> Spatial Covariance:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> sigma.sq-(Intercept) 0.5525 0.4252 0.1412 0.4015 1.9109 NA 9
#> sigma.sq-occ.cov 0.5388 0.4124 0.1535 0.4310 2.1640 NA 23
#> phi-(Intercept) 19.9677 6.1152 9.2044 20.2228 29.5252 NA 28
#> phi-occ.cov 15.1666 8.3853 4.0342 14.8777 29.2399 NA 6
```