Skip to contents

The function sfJSDM fits a spatially-explicit joint species distribution model. This model does not explicitly account for imperfect detection (see sfMsPGOcc()). We use Polya-Gamma latent variables and a spatial factor modeling approach. Currently, models are implemented using a Nearest Neighbor Gaussian Process. Future development will allow for running the models using a full Gaussian Process.

Usage

sfJSDM(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, 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, ...)

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 are allowed using lme4 syntax (Bates et al. 2015).

data

a list containing data necessary for model fitting. Valid tags are y, covs, and coords. y is a two-dimensional array with first dimension equal to the number of species and second dimension equal to the number of sites. Note how this differs from other spOccupancy functions in that y does not have any replicate surveys. This is because sfJSDM does not account for imperfect detection. covs is a matrix or data frame containing the variables used in the model, with \(J\) rows for each column (variable). coords is a matrix with \(J\) rows and 2 columns consisting of the spatial coordinates of each site in the data. 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 beta.comm, beta, tau.sq.beta, phi, lambda, sigma.sq.psi, and nu. nu is only specified if cov.model = "matern". sigma.sq.psi is only specified if random intercepts 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, phi.unif, nu.unif, and sigma.sq.psi.ig. Community-level occurrence (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 set to 2.73. 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 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.psi is the random effect variance for any random effects, and is 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.

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. We assume the initial variance of the adaptive sampler is the same for each species, although the adaptive sampler will adjust the tuning variances separately for each species. 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. 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 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.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 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 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.

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.

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.

...

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.

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.

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 .

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

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 sfJSDM that is a list comprised of:

beta.comm.samples

a coda object of posterior samples for the community level occurrence regression coefficients.

tau.sq.beta.samples

a coda object of posterior samples for the occurrence community variance parameters.

beta.samples

a coda object of posterior samples for the species level occurrence regression coefficients.

theta.samples

a coda object of posterior samples for the species level correlation parameters.

lambda.samples

a coda object of posterior samples for the latent spatial factor loadings.

psi.samples

a three-dimensional array of posterior samples for the latent occurrence probability values for each species.

w.samples

a three-dimensional array of posterior samples for the latent spatial random effects for each latent factor.

sigma.sq.psi.samples

a coda object of posterior samples for variances of random intercepts included in the occurrence portion of the model. Only included if random intercepts are specified in formula.

beta.star.samples

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

k.fold.deviance

vector of scoring rules (deviance) from k-fold cross-validation. A separate value is reported for each species. 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 estimated values are not included in the model object, but can be extracted using fitted().

Examples

set.seed(408)
J.x <- 10
J.y <- 10
J <- J.x * J.y
n.rep <- rep(1, J) 
N <- 10
# Community-level covariate effects
# Occurrence
beta.mean <- c(0.2, 1.3, 0.5)
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, .5, 1.6)
# Detection
# Fix this to be constant and really close to 1. 
alpha.mean <- c(9)
tau.sq.alpha <- c(0.05)
p.det <- length(alpha.mean)
# Random effects
psi.RE <- list()
# Include a single random effect
psi.RE <- list(levels = c(50), 
               sigma.sq.psi = c(1.5))
p.RE <- list()
# Draw species-level effects from community means.
beta <- matrix(NA, nrow = N, ncol = p.occ)
alpha <- matrix(NA, nrow = N, ncol = p.det)
for (i in 1:p.occ) {
  beta[, i] <- rnorm(N, beta.mean[i], sqrt(tau.sq.beta[i]))
}
for (i in 1:p.det) {
  alpha[, i] <- rnorm(N, alpha.mean[i], sqrt(tau.sq.alpha[i]))
}
alpha.true <- alpha
# Factor model
factor.model <- TRUE
n.factors <- 4
sigma.sq <- rep(1, n.factors)
phi <- rep(3/.4, n.factors)
sp <- TRUE
cov.model <- "exponential"

dat <- simMsOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, N = N, beta = beta, alpha = alpha,
                psi.RE = psi.RE, p.RE = p.RE, sp = sp, sigma.sq = sigma.sq, 
                phi = phi, cov.model = cov.model, factor.model = TRUE, n.factors = n.factors)
#> sigma.sq is specified but will be set to 1 for spatial latent factor model

X <- dat$X
y <- dat$y
coords <- dat$coords
X.re <- dat$X.re

occ.covs <- data.frame(X, X.re)
occ.covs$X.re <- occ.covs$X.re
colnames(occ.covs) <- c('int', 'occ.cov.1', 'occ.cov.2', 'occ.re.1')
data.list <- list(y = y[, , 1], 
                  covs = occ.covs, 
                  coords = coords)
# Priors
prior.list <- list(beta.comm.normal = list(mean = 0, var = 2.72),
                   tau.sq.beta.ig = list(a = 0.1, b = 0.1), 
                   phi.unif = list(a = 3 / 1, b = 3 / .1))
inits.list <- list(beta.comm = 0, 
                   beta = 0, 
                   tau.sq.beta = 1, 
                   phi = 3 / .5) 
tuning.list <- list(phi = 1)

out <- sfJSDM(formula = ~ occ.cov.1 + occ.cov.2 + (1 | occ.re.1), 
              data = data.list, 
              inits = inits.list, 
              priors = prior.list, 
              tuning = tuning.list, 
              cov.model = 'spherical', 
              NNGP = TRUE, 
              n.neighbors = 5,
              n.factors = 4, 
              n.batch = 10, 
              batch.length = 25, 
              n.report = 250, 
              n.burn = 50,
              n.thin = 2,
              n.chains = 1) 
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> No prior specified for sigma.sq.psi.ig.
#> Setting prior shape to 0.1 and prior scale to 0.1
#> lambda is not specified in initial values.
#> Setting initial values of the lower triangle to random values from a standard normal
#> sigma.sq.psi is not specified in initial values.
#> Setting initial values to random values between 0.5 and 10
#> ----------------------------------------
#> 	Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> Spatial Factor NNGP JSDM with Polya-Gamma latent
#> variable fit with 100 sites and 10 species.
#> 
#> Samples per chain: 250 (10 batches of length 25)
#> Burn-in: 50 
#> Thinning Rate: 2 
#> Number of Chains: 1 
#> Total Posterior Samples: 100 
#> 
#> Using the spherical spatial correlation model.
#> 
#> Using 4 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: 10 of 10, 100.00%
summary(out)
#> 
#> Call:
#> sfJSDM(formula = ~occ.cov.1 + occ.cov.2 + (1 | occ.re.1), data = data.list, 
#>     inits = inits.list, priors = prior.list, tuning = tuning.list, 
#>     cov.model = "spherical", NNGP = TRUE, n.neighbors = 5, n.factors = 4, 
#>     n.batch = 10, batch.length = 25, n.report = 250, n.burn = 50, 
#>     n.thin = 2, n.chains = 1)
#> 
#> Samples per Chain: 250
#> Burn-in: 50
#> Thinning Rate: 2
#> Number of Chains: 1
#> Total Posterior Samples: 100
#> Run Time (min): 0.0243
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Means (logit scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1165 0.3316 -0.4697 0.1501 0.7456   NA  68
#> occ.cov.1   1.1576 0.4293  0.3417 1.1529 2.0867   NA  76
#> occ.cov.2   0.2019 0.5975 -0.7779 0.1970 1.4216   NA 100
#> 
#> Variances (logit scale): 
#>               Mean     SD   2.5%    50%   97.5% Rhat ESS
#> (Intercept) 0.9613 0.5616 0.3131 0.8479  2.3570   NA  52
#> occ.cov.1   1.3586 0.8003 0.4195 1.2208  3.1934   NA  36
#> occ.cov.2   5.2723 4.1271 1.5667 4.0343 14.1685   NA 100
#> 
#> Random Effect Variances (logit scale): 
#>            Mean     SD  2.5%    50%  97.5% Rhat ESS
#> occ.re.1 2.6483 0.7184 1.729 2.4566 4.1075   NA   8
#> 
#> ----------------------------------------
#> 	Species Level
#> ----------------------------------------
#> Estimates (logit scale): 
#>                     Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept)-sp1  -1.6520 0.4722 -2.5836 -1.5580 -0.9130   NA  18
#> (Intercept)-sp2   0.1202 0.4649 -0.7632  0.1179  0.8929   NA  35
#> (Intercept)-sp3   0.1401 0.4759 -0.8155  0.1197  1.1720   NA  27
#> (Intercept)-sp4   1.0305 0.4518  0.1687  1.0248  1.8482   NA  40
#> (Intercept)-sp5   0.2197 0.4297 -0.5418  0.2210  1.1169   NA  39
#> (Intercept)-sp6   0.1852 0.4662 -0.5527  0.1521  1.2822   NA  23
#> (Intercept)-sp7  -0.0175 0.3381 -0.8041 -0.0020  0.5771   NA  53
#> (Intercept)-sp8  -0.3678 0.3780 -1.0898 -0.3941  0.3035   NA  71
#> (Intercept)-sp9  -0.0258 0.5162 -1.0599 -0.0297  0.8286   NA  15
#> (Intercept)-sp10  1.3564 0.4431  0.5910  1.3412  2.3313   NA  25
#> occ.cov.1-sp1     0.7010 0.3536  0.0722  0.7226  1.3955   NA  72
#> occ.cov.1-sp2     2.4612 0.6793  1.3894  2.3929  4.0462   NA  15
#> occ.cov.1-sp3     0.6478 0.3917 -0.1307  0.6809  1.3225   NA  59
#> occ.cov.1-sp4     2.2706 0.5127  1.4003  2.2240  3.3321   NA  30
#> occ.cov.1-sp5     0.7666 0.4008  0.1338  0.7020  1.5101   NA  41
#> occ.cov.1-sp6     2.2342 0.6028  1.1257  2.2416  3.5383   NA  43
#> occ.cov.1-sp7     1.7102 0.4231  1.0281  1.6622  2.4884   NA  35
#> occ.cov.1-sp8    -0.0729 0.3850 -0.6528 -0.0937  0.7138   NA  51
#> occ.cov.1-sp9     1.9900 0.5401  1.1221  1.9795  2.9548   NA  11
#> occ.cov.1-sp10   -0.2292 0.3555 -0.9096 -0.2424  0.4261   NA 112
#> occ.cov.2-sp1    -0.7417 0.3492 -1.3843 -0.6969 -0.0725   NA  60
#> occ.cov.2-sp2    -3.0679 0.5893 -4.2880 -3.0260 -2.0828   NA  29
#> occ.cov.2-sp3     3.5174 0.7420  2.3255  3.4676  5.1754   NA  18
#> occ.cov.2-sp4     1.1141 0.5272  0.0134  1.1009  2.1332   NA  49
#> occ.cov.2-sp5     0.6363 0.4212 -0.1172  0.6514  1.3837   NA  38
#> occ.cov.2-sp6     0.9051 0.4581 -0.0489  0.8983  1.7314   NA  34
#> occ.cov.2-sp7     2.1371 0.5415  1.1854  2.1599  3.1398   NA  25
#> occ.cov.2-sp8    -0.1040 0.3640 -0.7873 -0.0768  0.4820   NA  33
#> occ.cov.2-sp9    -2.1157 0.6871 -3.4216 -1.9868 -0.8989   NA  20
#> occ.cov.2-sp10   -0.9825 0.4277 -1.7061 -1.0056 -0.1227   NA  43
#> 
#> ----------------------------------------
#> 	Spatial Covariance
#> ----------------------------------------
#>          Mean     SD   2.5%     50%   97.5% Rhat ESS
#> phi-1  5.8271 4.6540 3.0084  4.3854 22.8799   NA  17
#> phi-2 17.4005 6.6788 6.2690 17.7233 28.0660   NA  29
#> phi-3 16.5977 7.7179 6.0512 15.3411 29.3026   NA  15
#> phi-4 16.2991 5.1683 7.8259 16.9048 25.2024   NA  33