# Function for Fitting Single-Species Integrated Spatial Occupancy Models Using Polya-Gamma Latent Variables

`spIntPGOcc.Rd`

The function `spIntPGOcc`

fits single-species integrated spatial occupancy models using Polya-Gamma latent variables. Models can be fit using either a full Gaussian process or a Nearest Neighbor Gaussian Process for large data sets. Data integration is done using a joint likelihood framework, assuming distinct detection models for each data source that are each conditional on a single latent occupancy process.

## Usage

```
spIntPGOcc(occ.formula, det.formula, data, inits, priors,
tuning, 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, k.fold.data, ...)
```

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

- det.formula
a list of symbolic descriptions of the models to be fit for the detection portion of the model using R's model syntax for each data set. Each element in the list is a formula for the detection model of a given data set. Only right-hand side of formula is specified. See example below.

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

`y`

,`occ.covs`

,`det.covs`

,`sites`

and`coords`

.`y`

is a list of matrices or data frames for each data set used in the integrated model. Each element of the list has first dimension equal to the number of sites with that data source 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 occurrence portion of the model, with the number of rows being the number of sites with at least one data source for each column (variable).`det.covs`

is a list of variables included in the detection portion of the model for each data source.`det.covs`

should have the same number of elements as`y`

, where each element is itself a list. Each element of the list for a given data source is a different detection covariate, which can be site-level or observational-level. Site-level covariates are specified as a vector with length equal to the number of observed sites of that data source, while observation-level covariates are specified as a matrix or data frame with the number of rows equal to the number of observed sites of that data source and number of columns equal to the maximum number of replicates at a given site.`coords`

is a matrix of the observation site 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`

, and`nu`

. The value portion of all tags except`alpha`

is the parameter's initial value. The tag`alpha`

is a list comprised of the initial values for the detection parameters for each data source. Each element of the list should be a vector of initial values for all detection parameters in the given data source or a single value for each data source to assign all parameters for a given data source the same 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`

, and`nu.unif`

. Occurrence (`beta`

) and detection (`alpha`

) regression coefficients are assumed to follow a normal distribution. For`beta`

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. For the detection coefficients`alpha`

, the mean and variance hyperparameters are themselves passed in as lists, with each element of the list corresponding to the specific hyperparameters for the detection parameters in a given data source. If not specified, prior means are set to 0 and prior variances set to 2.73 for normal priors. The spatial variance parameter,`sigma.sq`

, is assumed to follow an inverse-Gamma distribution or a uniform distribution (default is inverse-Gamma).`sigma.sq`

can also be fixed at its initial value by setting the prior value to`"fixed"`

. The spatial decay`phi`

and smoothness`nu`

parameters are assumed to follow Uniform distributions. The hyperparameters of the inverse-Gamma are passed as a vector of length two, with the first and second elements corresponding to the*shape*and*scale*, respectively. The hyperparameters of the Uniform are also passed as a vector of length two with the first and second elements corresponding to the lower and upper support, respectively.- tuning
a list with each tag corresponding to a parameter name. Valid tags are

`phi`

and`nu`

. The value portion of each tag defines the initial variance of the Adaptive sampler. 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. If`FALSE`

, a full Gaussian process is used. See Datta et al. (2016) and Finley et al. (2019) for more information.- 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 to run for each chain 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.

- 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.batch * batch.length`

samples 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 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.data
an integer specifying the specific data set to hold out values from. If not specified, data from all data set locations will be incorporated into the k-fold cross-validation.

- ...
currently no additional arguments

## Note

Some of the underlying code used for generating random numbers from the Polya-Gamma distribution is taken from the pgdraw package written by Daniel F. Schmidt and Enes Makalic. Their code implements Algorithm 6 in PhD thesis of Jesse Bennett Windle (2013) https://repositories.lib.utexas.edu/handle/2152/21842.

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

Finley, A. O., Datta, A., and Banerjee, S. (2020). spNNGP R package
for nearest neighbor Gaussian process models. *arXiv* preprint arXiv:2001.09111.

Hooten, M. B., and Hobbs, N. T. (2015). A guide to Bayesian model
selection for ecologists. *Ecological Monographs*, 85(1), 3-28.

Hooten, M. B., and Hefley, T. J. (2019). Bringing Bayesian models to life.
*CRC Press*.

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

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 for all data sources.- 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 covariance parameters.- w.samples
a

`coda`

object of posterior samples for latent spatial random effects.- 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
scoring rule (deviance) from k-fold cross-validation. A separate deviance value is returned for each data source. Only included if

`k.fold`

is specified in function call. Only a single value is returned if`k.fold.data`

is specified.

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

.

## Examples

```
set.seed(400)
# Simulate Data -----------------------------------------------------------
# Number of locations in each direction. This is the total region of interest
# where some sites may or may not have a data source.
J.x <- 8
J.y <- 8
J.all <- J.x * J.y
# Number of data sources.
n.data <- 4
# Sites for each data source.
J.obs <- sample(ceiling(0.2 * J.all):ceiling(0.5 * J.all), n.data, replace = TRUE)
# Replicates for each data source.
n.rep <- list()
for (i in 1:n.data) {
n.rep[[i]] <- sample(1:4, size = J.obs[i], replace = TRUE)
}
# Occupancy covariates
beta <- c(0.5, 0.5)
p.occ <- length(beta)
# Detection covariates
alpha <- list()
alpha[[1]] <- runif(2, 0, 1)
alpha[[2]] <- runif(3, 0, 1)
alpha[[3]] <- runif(2, -1, 1)
alpha[[4]] <- runif(4, -1, 1)
p.det.long <- sapply(alpha, length)
p.det <- sum(p.det.long)
sigma.sq <- 2
phi <- 3 / .5
sp <- TRUE
# Simulate occupancy data from multiple data sources.
dat <- simIntOcc(n.data = n.data, J.x = J.x, J.y = J.y, J.obs = J.obs,
n.rep = n.rep, beta = beta, alpha = alpha, sp = sp,
sigma.sq = sigma.sq, phi = phi, cov.model = 'exponential')
y <- dat$y
X <- dat$X.obs
X.p <- dat$X.p
sites <- dat$sites
X.0 <- dat$X.pred
psi.0 <- dat$psi.pred
coords <- as.matrix(dat$coords.obs)
coords.0 <- as.matrix(dat$coords.pred)
# Package all data into a list
occ.covs <- X[, 2, drop = FALSE]
colnames(occ.covs) <- c('occ.cov')
det.covs <- list()
# Add covariates one by one
det.covs[[1]] <- list(det.cov.1.1 = X.p[[1]][, , 2])
det.covs[[2]] <- list(det.cov.2.1 = X.p[[2]][, , 2],
det.cov.2.2 = X.p[[2]][, , 3])
det.covs[[3]] <- list(det.cov.3.1 = X.p[[3]][, , 2])
det.covs[[4]] <- list(det.cov.4.1 = X.p[[4]][, , 2],
det.cov.4.2 = X.p[[4]][, , 3],
det.cov.4.3 = X.p[[4]][, , 4])
data.list <- list(y = y,
occ.covs = occ.covs,
det.covs = det.covs,
sites = sites,
coords = coords)
J <- length(dat$z.obs)
# Initial values
inits.list <- list(alpha = list(0, 0, 0, 0),
beta = 0,
phi = 3 / .5,
sigma.sq = 2,
w = rep(0, J),
z = rep(1, J))
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 2.72),
alpha.normal = list(mean = list(0, 0, 0, 0),
var = list(2.72, 2.72, 2.72, 2.72)),
phi.unif = c(3/1, 3/.1),
sigma.sq.ig = c(2, 2))
# Tuning
tuning.list <- list(phi = 0.3)
# Number of batches
n.batch <- 10
# Batch length
batch.length <- 25
out <- spIntPGOcc(occ.formula = ~ occ.cov,
det.formula = list(f.1 = ~ det.cov.1.1,
f.2 = ~ det.cov.2.1 + det.cov.2.2,
f.3 = ~ det.cov.3.1,
f.4 = ~ det.cov.4.1 + det.cov.4.2 + det.cov.4.3),
data = data.list,
inits = inits.list,
n.batch = n.batch,
batch.length = batch.length,
accept.rate = 0.43,
priors = prior.list,
cov.model = "exponential",
tuning = tuning.list,
n.omp.threads = 1,
verbose = TRUE,
NNGP = FALSE,
n.report = 10,
n.burn = 50,
n.thin = 1)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Spatial Integrated Occupancy Model with Polya-Gamma latent
#> variable fit with 54 sites.
#>
#> Integrating 4 occupancy data sets.
#>
#> Samples per chain: 250 (10 batches of length 25)
#> Burn-in: 50
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 200
#>
#> Using the exponential spatial correlation model.
#>
#>
#> 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:
#> spIntPGOcc(occ.formula = ~occ.cov, det.formula = list(f.1 = ~det.cov.1.1,
#> f.2 = ~det.cov.2.1 + det.cov.2.2, f.3 = ~det.cov.3.1, f.4 = ~det.cov.4.1 +
#> det.cov.4.2 + det.cov.4.3), data = data.list, inits = inits.list,
#> priors = prior.list, tuning = tuning.list, cov.model = "exponential",
#> NNGP = FALSE, 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.0056
#>
#> Occurrence (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.7411 0.4588 -0.0963 0.7520 1.7538 NA 60
#> occ.cov 0.1697 0.3548 -0.4907 0.1676 0.9319 NA 92
#>
#> Data source 1 Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.3974 0.6617 -0.6767 0.3581 1.5855 NA 79
#> det.cov.1.1 1.2573 0.6768 0.1930 1.1961 2.5362 NA 94
#>
#> Data source 2 Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 1.2309 0.3734 0.6006 1.2235 2.0354 NA 72
#> det.cov.2.1 0.0860 0.3802 -0.6096 0.0676 0.7860 NA 156
#> det.cov.2.2 0.5402 0.3392 -0.0715 0.5170 1.2598 NA 149
#>
#> Data source 3 Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.2819 0.3051 -0.8860 -0.2629 0.3441 NA 154
#> det.cov.3.1 0.4909 0.2851 -0.1025 0.4966 1.0670 NA 261
#>
#> Data source 4 Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.1144 0.3781 -0.5744 0.1093 0.9064 NA 152
#> det.cov.4.1 0.4481 0.3317 -0.2548 0.4168 1.1268 NA 154
#> det.cov.4.2 -0.6200 0.4310 -1.4494 -0.6251 0.1225 NA 146
#> det.cov.4.3 -0.2681 0.3536 -0.9096 -0.2942 0.4853 NA 200
#>
#> Spatial Covariance:
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> sigma.sq 1.6581 0.8860 0.5817 1.4683 3.6564 NA 17
#> phi 10.0482 4.8377 4.1783 8.5052 22.1713 NA 4
```