Skip to contents

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, k.fold.only = FALSE, ...)

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 0 and N (the number of species in the community). When set to 0, the model assumes there are no residual species correlations, which is equivalent to the msPGOcc() function but without imperfect detection.

n.samples

the number of posterior samples to collect in each chain.

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

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

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.0203
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Means (logit scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.3161 0.2990 -0.2492 0.3303 0.8826   NA 185
#> occ.cov.1   0.1097 0.2673 -0.3793 0.1067 0.6877   NA 187
#> occ.cov.2   1.7034 0.6255  0.2694 1.7425 2.8209   NA 299
#> 
#> Variances (logit scale): 
#>               Mean     SD   2.5%    50%   97.5% Rhat ESS
#> (Intercept) 0.6001 0.5805 0.0979 0.4389  1.9093   NA 163
#> occ.cov.1   0.5991 0.4430 0.1360 0.4792  1.9918   NA  78
#> occ.cov.2   4.9037 3.3481 1.2587 3.9035 13.0276   NA  65
#> 
#> Random Effect Variances (logit scale): 
#>            Mean     SD   2.5%    50%  97.5% Rhat ESS
#> occ.re.1 1.7323 0.7207 0.6705 1.6954 3.5681   NA  13
#> 
#> ----------------------------------------
#> 	Species Level
#> ----------------------------------------
#> Estimates (logit scale): 
#>                     Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept)-sp1  -0.1755 0.4206 -0.9923 -0.1919  0.6918   NA 113
#> (Intercept)-sp2   0.3667 0.3872 -0.3586  0.3545  1.1838   NA  96
#> (Intercept)-sp3   1.1370 0.4531  0.2850  1.1192  2.1049   NA  77
#> (Intercept)-sp4   0.4163 0.4267 -0.4785  0.4506  1.1658   NA 116
#> (Intercept)-sp5   0.1586 0.4113 -0.6187  0.1490  0.9673   NA 157
#> (Intercept)-sp6  -0.5071 0.4321 -1.3977 -0.4881  0.2520   NA 104
#> (Intercept)-sp7   1.0635 0.4734  0.2631  1.0308  2.0651   NA  84
#> (Intercept)-sp8   0.2273 0.3799 -0.5753  0.2220  0.8451   NA 104
#> (Intercept)-sp9   0.7425 0.4606 -0.1568  0.7114  1.7208   NA  81
#> (Intercept)-sp10 -0.0912 0.4037 -0.8325 -0.1217  0.6949   NA  57
#> occ.cov.1-sp1    -0.1626 0.3963 -0.9005 -0.1328  0.5560   NA 128
#> occ.cov.1-sp2     0.8523 0.3630  0.1905  0.8287  1.5760   NA  83
#> occ.cov.1-sp3    -0.7070 0.4089 -1.6315 -0.7021  0.0471   NA  61
#> occ.cov.1-sp4     0.3248 0.3512 -0.3724  0.3577  0.9432   NA 142
#> occ.cov.1-sp5    -0.5500 0.3947 -1.3041 -0.5182  0.1646   NA  49
#> occ.cov.1-sp6     0.7246 0.3889  0.0042  0.7084  1.5094   NA  87
#> occ.cov.1-sp7     0.0819 0.3992 -0.7598  0.1021  0.8511   NA 130
#> occ.cov.1-sp8    -0.1009 0.3197 -0.8245 -0.0896  0.4735   NA 137
#> occ.cov.1-sp9     0.9570 0.4590  0.1882  0.9031  1.8824   NA  75
#> occ.cov.1-sp10   -0.4402 0.3107 -1.0629 -0.4279  0.1284   NA  36
#> occ.cov.2-sp1     4.1656 0.9141  2.5437  4.1686  6.1624   NA  29
#> occ.cov.2-sp2    -2.2658 0.5254 -3.3647 -2.2468 -1.2895   NA  55
#> occ.cov.2-sp3     1.7478 0.4320  1.0017  1.7256  2.7025   NA 103
#> occ.cov.2-sp4     2.2888 0.5230  1.3769  2.2654  3.6058   NA  67
#> occ.cov.2-sp5     3.1528 0.7578  1.9387  3.0252  4.9152   NA  44
#> occ.cov.2-sp6     3.5154 0.7825  2.2726  3.4191  5.2648   NA  38
#> occ.cov.2-sp7     3.6677 0.8443  2.2541  3.6067  5.3814   NA  41
#> occ.cov.2-sp8     0.4787 0.3636 -0.1065  0.4512  1.1949   NA 108
#> occ.cov.2-sp9     1.8347 0.4491  1.1343  1.7958  2.8990   NA  70
#> occ.cov.2-sp10    1.2651 0.4134  0.5103  1.2441  2.1357   NA  75