Function for fitting a joint species distribution model with species correlations. This model does not explicitly account for imperfect detection (see lfMsPGOcc()). We use Polya-gamma latent variables and a factor modeling approach.

## Usage

lfJSDM(formula, data, inits, priors, n.factors,
n.samples, n.omp.threads = 1, verbose = TRUE, n.report = 100,
n.burn = round(.10 * n.samples), 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 lfJSDM 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, sigma.sq.psi, lambda. 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, and sigma.sq.psi.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 set to 2.72. 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 all parameters are assigned the same prior. If not specified, prior shape and scale parameters are set to 0.1. The factor model fits n.factors independent latent factors. The priors for the factor loadings matrix lambda are fixed following standard approaches to ensure parameter identifiability. 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.

n.factors

the number of factors to use in the latent 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.samples

the number of posterior samples to collect in each chain.

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 hypterthreaded 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 MCMC progress.

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.

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.

...

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

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.

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.

## Author

Jeffrey W. Doser doserjef@msu.edu,
Andrew O. Finley finleya@msu.edu

## Value

An object of class lfJSDM 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.

lambda.samples

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

psi.samples

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

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

w.samples

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

beta.star.samples

a coda object of posterior samples for the occurrence random effects. Only included if random intercepts are specified in occ.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(400)
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, 0.6, 1.5)
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, 1.2, 1.7)
# 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
# Include a single random effect
psi.RE <- list(levels = c(20),
sigma.sq.psi = c(2))
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

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 = FALSE,
factor.model = TRUE, n.factors = 4)

X <- dat$X y <- dat$y
X.re <- dat$X.re coords <- dat$coords
occ.covs <- cbind(X, 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))
inits.list <- list(beta.comm = 0, beta = 0, tau.sq.beta = 1)
out <- lfJSDM(formula = ~ occ.cov.1 + occ.cov.2 + (1 | occ.re.1),
data = data.list,
inits = inits.list,
priors = prior.list,
n.factors = 4,
n.samples = 1000,
n.report = 500,
n.burn = 500,
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
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> Latent Factor JSDM with Polya-Gamma latent
#> variable fit with 100 sites and 10 species.
#>
#> Samples per Chain: 1000
#> Burn-in: 500
#> Thinning Rate: 2
#> Number of Chains: 1
#> Total Posterior Samples: 250
#>
#> Using 4 latent factors.
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> ----------------------------------------
#> 	Chain 1
#> ----------------------------------------
#> Sampling ...
#> Sampled: 500 of 1000, 50.00%
#> -------------------------------------------------
#> Sampled: 1000 of 1000, 100.00%
summary(out)
#>
#> Call:
#> lfJSDM(formula = ~occ.cov.1 + occ.cov.2 + (1 | occ.re.1), data = data.list,
#>     inits = inits.list, priors = prior.list, n.factors = 4, n.samples = 1000,
#>     n.report = 500, n.burn = 500, n.thin = 2, n.chains = 1)
#>
#> Samples per Chain: 1000
#> Burn-in: 500
#> Thinning Rate: 2
#> Number of Chains: 1
#> Total Posterior Samples: 250
#> Run Time (min): 0.0206
#>
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Means (logit scale):
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.2724 0.2394 -0.1641 0.2716 0.8054   NA 127
#> occ.cov.1   0.2148 0.3497 -0.5030 0.2070 0.8936   NA 205
#> occ.cov.2   1.5809 0.7447  0.0548 1.6553 2.9714   NA 169
#>
#> Variances (logit scale):
#>               Mean     SD   2.5%    50%   97.5% Rhat ESS
#> (Intercept) 0.3763 0.3347 0.0417 0.2842  1.2626   NA  72
#> occ.cov.1   1.0141 0.7740 0.2257 0.7707  2.8537   NA 144
#> occ.cov.2   5.3584 3.8375 1.8127 4.3293 15.6016   NA 250
#>
#> Random Effect Variances (logit scale):
#>            Mean     SD   2.5%    50%  97.5% Rhat ESS
#> occ.re.1 2.1969 0.7025 1.1686 2.0995 4.2135   NA  20
#>
#> ----------------------------------------
#> 	Species Level
#> ----------------------------------------
#> Estimates (logit scale):
#>                     Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept)-sp1   0.2245 0.3703 -0.5668  0.2421  0.9700   NA  91
#> (Intercept)-sp2   0.1441 0.3854 -0.5637  0.1491  0.8887   NA  95
#> (Intercept)-sp3   0.9519 0.4472  0.1211  0.9155  1.8103   NA  50
#> (Intercept)-sp4  -0.1496 0.4226 -1.0144 -0.1382  0.6802   NA  87
#> (Intercept)-sp5   0.3379 0.3858 -0.3660  0.3034  1.1300   NA 104
#> (Intercept)-sp6  -0.0616 0.4768 -1.0605 -0.0250  0.6943   NA  82
#> (Intercept)-sp7   0.6537 0.4802 -0.1400  0.6192  1.7074   NA  93
#> (Intercept)-sp8   0.4011 0.3994 -0.2884  0.3413  1.1803   NA  83
#> (Intercept)-sp9  -0.1467 0.4184 -0.9828 -0.0858  0.6434   NA  80
#> (Intercept)-sp10  0.4687 0.3933 -0.2140  0.4464  1.2968   NA  85
#> occ.cov.1-sp1     0.0826 0.3620 -0.7011  0.0921  0.8039   NA 141
#> occ.cov.1-sp2     1.2366 0.5066  0.3023  1.1838  2.4908   NA  60
#> occ.cov.1-sp3    -0.7558 0.3559 -1.5839 -0.7477 -0.1355   NA  75
#> occ.cov.1-sp4     0.1764 0.3592 -0.5371  0.1582  0.8676   NA  75
#> occ.cov.1-sp5    -0.5352 0.4195 -1.4474 -0.4966  0.1840   NA  87
#> occ.cov.1-sp6     1.4547 0.4660  0.7134  1.3997  2.3729   NA  68
#> occ.cov.1-sp7    -0.4127 0.3872 -1.3304 -0.4100  0.3173   NA 104
#> occ.cov.1-sp8     0.5952 0.3661 -0.1605  0.5624  1.4141   NA  99
#> occ.cov.1-sp9     0.7524 0.3772  0.0812  0.7188  1.5081   NA  98
#> occ.cov.1-sp10   -0.4711 0.3515 -1.1688 -0.4607  0.2043   NA 114
#> occ.cov.2-sp1     3.2918 0.6728  2.2313  3.2428  5.0424   NA  61
#> occ.cov.2-sp2    -3.0966 0.6274 -4.4655 -3.0750 -1.9906   NA  62
#> occ.cov.2-sp3     1.1210 0.4278  0.3733  1.0755  2.0520   NA  59
#> occ.cov.2-sp4     2.5264 0.6203  1.4455  2.4820  4.1727   NA  30
#> occ.cov.2-sp5     3.4599 0.7748  2.1588  3.3838  5.0523   NA  28
#> occ.cov.2-sp6     3.6090 0.7492  2.3032  3.5214  5.1638   NA  39
#> occ.cov.2-sp7     3.1462 0.6215  2.0755  3.1297  4.3179   NA  59
#> occ.cov.2-sp8     0.5905 0.3726 -0.0855  0.5603  1.2961   NA 101
#> occ.cov.2-sp9     1.9097 0.4917  1.0892  1.8497  2.9444   NA  72
#> occ.cov.2-sp10    1.2888 0.4317  0.4691  1.2864  2.1488   NA  46