Function for Fitting Multi-Species Multi-Season Occupancy Models
tMsPGOcc.Rd
The function tMsPGOcc
fits multi-species multi-season occupancy models using Polya-Gamma data augmentation.
Usage
tMsPGOcc(occ.formula, det.formula, data, inits, priors, tuning,
n.batch, batch.length,
accept.rate = 0.43, n.omp.threads = 1,
verbose = TRUE, ar1 = FALSE, n.report = 100,
n.burn = round(.10 * n.batch * batch.length), n.thin = 1,
n.chains = 1, ...)
Arguments
- occ.formula
a symbolic description of the model to be fit for the occurrence portion of the model using R's model syntax. Random intercepts are allowed using lme4 syntax (Bates et al. 2015). Only right-hand side of formula is specified. See example below.
- 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
, anddet.covs
.y
is a four-dimensional array with first dimension equal to the number of species, second dimension equal to the number of sites, third dimension equal to the number of primary time periods, and fourth dimension equal to the maximum number of secondary replicates at a given site.occ.covs
is a list of variables included in the occurrence portion of the model. Each list element is a different occurrence covariate, which can be site level or site/primary time period level. Site-level covariates are specified as a vector of length \(J\) while site/primary time period level covariates are specified as a matrix with rows corresponding to sites and columns correspond to primary time periods. Similarly,det.covs
is a list of variables included in the detection portion of the model, with each list element corresponding to an individual variable. In addition to site-level and/or site/primary time period-level, detection covariates can also be observational-level. Observation-level covariates are specified as a three-dimensional array with first dimension corresponding to sites, second dimension corresponding to primary time period, and third dimension corresponding to replicate.- inits
a list with each tag corresponding to a parameter name. Valid tags are
alpha.comm
,beta.comm
,beta
,alpha
,tau.sq.beta
,tau.sq.alpha
,sigma.sq.psi
,sigma.sq.p
,z
,sigma.sq.t
, andrho
.sigma.sq.t
andrho
are only relevant whenar1 = TRUE
, andsigma.sq.psi
andsigma.sq.p
are only specified if random effects are included inocc.formula
ordet.formula
, respectively. The value portion of each tag is the parameter's initial value. Seepriors
description for definition of each parameter name. Additionally, the tagfix
can be set toTRUE
to fix the starting values across all chains. Iffix
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
,alpha.comm.normal
,tau.sq.beta.ig
,tau.sq.alpha.ig
,sigma.sq.psi
,sigma.sq.p
,sigma.sq.t.ig
, andrho.unif
. Community-level occurrence (beta.comm
) and detection (alpha.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. By default, community-level variance parameters for occupancy (tau.sq.beta
) and detection (tau.sq.alpha
) 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.sigma.sq.t
andrho
are the AR(1) variance and correlation parameters for the AR(1) zero-mean temporal random effects, respectively.sigma.sq.t
is assumed to follow an inverse-Gamma distribution, where the hyperparameters are specified as a list of length two with the first and second elements corresponding to the shape and scale parameters, respectively, which can each be specified as vector equal to the number of species in the model or a single value if the same prior is used for all species.rho
is assumed to follow a uniform distribution, where the hyperparameters are specified similarly as a list of length two with the first and second elements corresponding to the lower and upper bounds of the uniform prior, which can each be specified as vector equal to the number of species in the model or a single value if the same prior is used for all species.sigma.sq.psi
andsigma.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 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
rho
. The value portion of each tag defines the initial tuning variance of the Adaptive sampler. See Roberts and Rosenthal (2009) for details.- 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.- ar1
logical value indicating whether to include an AR(1) zero-mean temporal random effect in the model for each species. If
FALSE
, the model is fit without an AR(1) temporal autocovariance structure. IfTRUE
, a species-specific AR(1) random effect is included in the model to account for temporal autocorrelation across the primary time periods.- 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.
- ...
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.
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 .
Kery, M., & Royle, J. A. (2021). Applied hierarchical modeling in ecology: Analysis of distribution, abundance and species richness in R and BUGS: Volume 2: Dynamic and advanced models. Academic Press. Section 4.6.
Author
Jeffrey W. Doser doserjef@msu.edu,
Andrew O. Finley finleya@msu.edu
Value
An object of class tMsPGOcc
that is a list comprised of:
- beta.comm.samples
a
coda
object of posterior samples for the community level occurrence regression coefficients.- alpha.comm.samples
a
coda
object of posterior samples for the community level detection regression coefficients.- tau.sq.beta.samples
a
coda
object of posterior samples for the occurrence community variance parameters.- tau.sq.alpha.samples
a
coda
object of posterior samples for the detection community variance parameters.- beta.samples
a
coda
object of posterior samples for the species level occurrence regression coefficients.- alpha.samples
a
coda
object of posterior samples for the species level detection regression coefficients.- theta.samples
a
coda
object of posterior samples for the species level AR(1) variance (sigma.sq.t
) and correlation (rho
) parameters. Only included ifar1 = TRUE
.- eta.samples
a three-dimensional array of posterior samples for the species-specific AR(1) random effects for each primary time period. Dimensions correspond to MCMC sample, species, and primary time period.
- z.samples
a four-dimensional array of posterior samples for the latent occurrence values for each species. Dimensions corresopnd to MCMC sample, species, site, and primary time period.
- psi.samples
a four-dimensional array of posterior samples for the latent occupancy probability values for each species. Dimensions correspond to MCMC sample, species, site, and primary time period.
- 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 inocc.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 indet.formula
.- beta.star.samples
a
coda
object of posterior samples for the occurrence random effects. Only included if random intercepts are specified inocc.formula
.- alpha.star.samples
a
coda
object of posterior samples for the detection random effects. Only included if random intercepts are specified indet.formula
.- like.samples
a four-dimensional array of posterior samples for the likelihood value used for calculating WAIC. Dimensions correspond to MCMC sample, species, site, and time period.
- 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()
.
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
# Simulate Data -----------------------------------------------------------
set.seed(500)
J.x <- 8
J.y <- 8
J <- J.x * J.y
# Years sampled
n.time <- sample(3:10, J, replace = TRUE)
# n.time <- rep(10, J)
n.time.max <- max(n.time)
# Replicates
n.rep <- matrix(NA, J, max(n.time))
for (j in 1:J) {
n.rep[j, 1:n.time[j]] <- sample(2:4, n.time[j], replace = TRUE)
}
N <- 7
# Community-level covariate effects
# Occurrence
beta.mean <- c(-3, -0.2, 0.5)
trend <- FALSE
sp.only <- 0
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, 1.5, 1.4)
# Detection
alpha.mean <- c(0, 1.2, -1.5)
tau.sq.alpha <- c(1, 0.5, 2.3)
p.det <- length(alpha.mean)
# Random effects
psi.RE <- list()
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]))
}
sp <- FALSE
dat <- simTMsOcc(J.x = J.x, J.y = J.y, n.time = n.time, n.rep = n.rep, N = N,
beta = beta, alpha = alpha, sp.only = sp.only, trend = trend,
psi.RE = psi.RE, p.RE = p.RE, sp = sp)
y <- dat$y
X <- dat$X
X.p <- dat$X.p
X.re <- dat$X.re
X.p.re <- dat$X.p.re
occ.covs <- list(occ.cov.1 = X[, , 2],
occ.cov.2 = X[, , 3])
det.covs <- list(det.cov.1 = X.p[, , , 2],
det.cov.2 = X.p[, , , 3])
data.list <- list(y = y, occ.covs = occ.covs,
det.covs = det.covs)
# Priors
prior.list <- list(beta.comm.normal = list(mean = 0, var = 2.72),
alpha.comm.normal = list(mean = 0, var = 2.72),
tau.sq.beta.ig = list(a = 0.1, b = 0.1),
tau.sq.alpha.ig = list(a = 0.1, b = 0.1))
z.init <- apply(y, c(1, 2, 3), function(a) as.numeric(sum(a, na.rm = TRUE) > 0))
inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0,
alpha = 0, tau.sq.beta = 1, tau.sq.alpha = 1,
z = z.init)
# Tuning
tuning.list <- list(phi = 1)
# Number of batches
n.batch <- 5
# Batch length
batch.length <- 25
n.burn <- 25
n.thin <- 1
n.samples <- n.batch * batch.length
out <- tMsPGOcc(occ.formula = ~ occ.cov.1 + occ.cov.2,
det.formula = ~ det.cov.1 + det.cov.2,
data = data.list,
inits = inits.list,
n.batch = n.batch,
batch.length = batch.length,
accept.rate = 0.43,
priors = prior.list,
n.omp.threads = 1,
verbose = TRUE,
n.report = 1,
n.burn = n.burn,
n.thin = n.thin,
n.chains = 1)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Multi-season Multi-species Occupancy Model with Polya-Gamma latent
#> variables with 64 sites, 7 species, and 10 primary time periods.
#>
#> Samples per chain: 125 (5 batches of length 25)
#> Burn-in: 25
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 100
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> Adaptive Metropolis with target acceptance rate: 43.0
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Batch: 1 of 5, 20.00%
#> -------------------------------------------------
#> Batch: 2 of 5, 40.00%
#> -------------------------------------------------
#> Batch: 3 of 5, 60.00%
#> -------------------------------------------------
#> Batch: 4 of 5, 80.00%
#> -------------------------------------------------
#> Batch: 5 of 5, 100.00%
summary(out)
#>
#> Call:
#> tMsPGOcc(occ.formula = ~occ.cov.1 + occ.cov.2, det.formula = ~det.cov.1 +
#> det.cov.2, data = data.list, inits = inits.list, priors = prior.list,
#> n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43,
#> n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn,
#> n.thin = n.thin, n.chains = 1)
#>
#> Samples per Chain: 125
#> Burn-in: 25
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 100
#> Run Time (min): 0.0062
#>
#> ----------------------------------------
#> Community Level
#> ----------------------------------------
#> Occurrence Means (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -3.3951 0.2561 -3.8965 -3.3688 -2.9980 NA 67
#> occ.cov.1 0.2756 0.2465 -0.2455 0.2749 0.7464 NA 51
#> occ.cov.2 0.9911 0.6127 -0.0586 1.0145 2.3914 NA 100
#>
#> Occurrence Variances (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.6558 0.6816 0.0831 0.5029 2.2011 NA 17
#> occ.cov.1 0.4368 0.4756 0.0765 0.2968 1.7432 NA 58
#> occ.cov.2 2.7841 1.9843 0.6906 2.2722 7.8407 NA 67
#>
#> Detection Means (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.2977 0.2616 -0.2173 0.2969 0.6910 NA 36
#> det.cov.1 1.1457 0.3288 0.3844 1.1673 1.7245 NA 100
#> det.cov.2 -1.5292 0.8089 -2.8766 -1.5298 -0.0014 NA 147
#>
#> Detection Variances (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.2899 0.3396 0.0476 0.1997 1.4378 NA 38
#> det.cov.1 0.5948 0.6932 0.0793 0.3576 2.8432 NA 16
#> det.cov.2 6.0538 5.5965 0.9278 4.9073 21.7062 NA 13
#>
#> ----------------------------------------
#> Species Level
#> ----------------------------------------
#> Occurrence (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 -3.3381 0.2781 -3.8072 -3.3623 -2.8221 NA 13
#> (Intercept)-sp2 -3.8087 0.3364 -4.5895 -3.7667 -3.2830 NA 8
#> (Intercept)-sp3 -3.4782 0.2767 -4.0439 -3.4558 -3.0143 NA 12
#> (Intercept)-sp4 -2.7861 0.2037 -3.1506 -2.7728 -2.3653 NA 20
#> (Intercept)-sp5 -2.9008 0.2052 -3.2888 -2.9090 -2.4694 NA 19
#> (Intercept)-sp6 -3.2567 0.2770 -3.7471 -3.2758 -2.7925 NA 11
#> (Intercept)-sp7 -4.6254 0.6917 -6.0049 -4.6657 -3.4336 NA 4
#> occ.cov.1-sp1 0.1990 0.2319 -0.2268 0.2417 0.6363 NA 19
#> occ.cov.1-sp2 -0.1376 0.2792 -0.7171 -0.1364 0.3086 NA 14
#> occ.cov.1-sp3 -0.3402 0.2547 -0.7901 -0.3369 0.1288 NA 15
#> occ.cov.1-sp4 0.1534 0.1957 -0.1936 0.1549 0.5603 NA 24
#> occ.cov.1-sp5 0.6027 0.2612 0.1821 0.5843 1.0629 NA 17
#> occ.cov.1-sp6 0.5553 0.2275 0.1205 0.5497 1.0428 NA 24
#> occ.cov.1-sp7 0.9313 0.3146 0.4912 0.8504 1.6122 NA 14
#> occ.cov.2-sp1 1.1176 0.2496 0.5983 1.1282 1.5460 NA 20
#> occ.cov.2-sp2 1.7928 0.2196 1.3671 1.7863 2.3106 NA 22
#> occ.cov.2-sp3 -0.5970 0.2756 -1.1362 -0.5582 -0.1028 NA 13
#> occ.cov.2-sp4 -0.3093 0.2761 -0.8506 -0.3490 0.2245 NA 18
#> occ.cov.2-sp5 0.2672 0.2336 -0.1879 0.2839 0.6652 NA 19
#> occ.cov.2-sp6 1.9392 0.2399 1.5127 1.9487 2.4148 NA 27
#> occ.cov.2-sp7 2.9842 0.5896 2.0502 2.8994 4.1770 NA 4
#>
#> Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 0.6487 0.3810 -0.0374 0.6079 1.4427 NA 19
#> (Intercept)-sp2 -0.0461 0.2562 -0.5066 -0.0365 0.4261 NA 17
#> (Intercept)-sp3 0.1488 0.4564 -0.8523 0.2275 0.9119 NA 29
#> (Intercept)-sp4 0.2893 0.2634 -0.2456 0.3054 0.7261 NA 42
#> (Intercept)-sp5 0.3673 0.2662 -0.0924 0.3989 0.8349 NA 44
#> (Intercept)-sp6 0.6592 0.2331 0.1994 0.6374 1.0941 NA 49
#> (Intercept)-sp7 0.0337 0.2281 -0.3833 0.0455 0.4328 NA 54
#> det.cov.1-sp1 1.9411 0.5038 1.0673 1.9065 2.9361 NA 21
#> det.cov.1-sp2 0.8524 0.2982 0.4211 0.8139 1.5298 NA 48
#> det.cov.1-sp3 1.0282 0.4706 0.1435 1.0100 2.0621 NA 19
#> det.cov.1-sp4 0.7611 0.3436 0.1435 0.7569 1.5169 NA 28
#> det.cov.1-sp5 1.8650 0.4711 1.2108 1.7717 2.8979 NA 21
#> det.cov.1-sp6 1.0617 0.2706 0.5133 1.0828 1.5632 NA 43
#> det.cov.1-sp7 0.9060 0.2327 0.5224 0.8675 1.3828 NA 54
#> det.cov.2-sp1 0.0701 0.3793 -0.5709 0.0622 0.8415 NA 55
#> det.cov.2-sp2 -0.3658 0.3012 -0.8687 -0.3310 0.1460 NA 53
#> det.cov.2-sp3 -5.9659 1.9654 -9.5462 -6.1462 -2.7888 NA 4
#> det.cov.2-sp4 -3.0455 0.7435 -4.2544 -2.9998 -1.6882 NA 15
#> det.cov.2-sp5 -1.0886 0.4014 -1.9885 -1.0644 -0.4060 NA 34
#> det.cov.2-sp6 -2.0188 0.3918 -2.6893 -1.9970 -1.4653 NA 38
#> det.cov.2-sp7 -1.6556 0.3505 -2.2969 -1.6076 -1.1129 NA 30