
Function for Fitting Single-Species Occupancy Models Using Polya-Gamma Latent Variables
PGOcc.RdFunction for fitting single-species occupancy models using Polya-Gamma latent variables.
Usage
PGOcc(occ.formula, det.formula, data, inits, priors, 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
- 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. Random intercepts are allowed using lme4 syntax (Bates et al. 2015).
- 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.yis a matrix or data frame with first dimension equal to the number of sites (\(J\)) and second dimension equal to the maximum number of replicates at a given site.occ.covsis a matrix or data frame containing the variables used in the occurrence portion of the model, with \(J\) rows for each column (variable).det.covsis a list of variables included in the detection portion of the model. Each list element is a different detection covariate, which can be site-level or observational-level. Site-level covariates are specified as a vector of length \(J\) while observation-level covariates are specified as a matrix or data frame with the number of rows equal to \(J\) and number of columns equal to the maximum number of replicates at a given site.- inits
a list with each tag corresponding to a parameter name. Valid tags are
z,beta,alpha,sigma.sq.psi, andsigma.sq.p. The value portion of each tag is the parameter's initial value.sigma.sq.psiandsigma.sq.pare only relevant when including random effects in the occurrence and detection portion of the occupancy model, respectively. Seepriorsdescription for definition of each parameter name. Additionally, the tagfixcan be set toTRUEto fix the starting values across all chains. Iffixis 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,sigma.sq.psi.ig, andsigma.sq.p.ig. Occupancy (beta) and detection (alpha) 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.sigma.sq.psiandsigma.sq.pare 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.- 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 within-chains. This will have no impact on model run time for non-spatial models. The package must be compiled for OpenMP support. For most Intel-based machines, we recommend setting
n.omp.threadsup to the number of hypterthreaded cores. Note,n.omp.threads> 1 might not work on some systems. Currently only relevant for spatial models.- 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.samplesto 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.burnsamples are discarded. Default value is set to 1.- n.chains
the number of chains to run.
- 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.threadsandk.fold.seedare ignored. In k-fold cross-validation, the data specified indatais 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 thek.fold.devianceobject in the return list.- k.fold.threads
number of threads to use for cross-validation. If
k.fold.threads > 1parallel processing is accomplished using the foreach and doParallel packages. Ignored ifk.foldis not specified.- k.fold.seed
seed used to split data set into
k.foldparts for k-fold cross-validation. Ignored ifk.foldis 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 isFALSE.- ...
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.
MacKenzie, D. I., J. D. Nichols, G. B. Lachman, S. Droege, J. Andrew Royle, and C. A. Langtimm. 2002. Estimating Site Occupancy Rates When Detection Probabilities Are Less Than One. Ecology 83: 2248-2255.
Author
Jeffrey W. Doser doserjef@msu.edu,
Andrew O. Finley finleya@msu.edu
Value
An object of class PGOcc that is a list comprised of:
- beta.samples
a
codaobject of posterior samples for the occupancy regression coefficients.- alpha.samples
a
codaobject of posterior samples for the detection regression coefficients.- z.samples
a
codaobject of posterior samples for the latent occupancy values- psi.samples
a
codaobject of posterior samples for the latent occupancy probability values- sigma.sq.psi.samples
a
codaobject of posterior samples for variances of random intercepts included in the occupancy portion of the model. Only included if random intercepts are specified inocc.formula.- sigma.sq.p.samples
a
codaobject 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
codaobject of posterior samples for the occurrence random effects. Only included if random intercepts are specified inocc.formula.- alpha.star.samples
a
codaobject of posterior samples for the detection random effects. Only included if random intercepts are specified indet.formula.- like.samples
a
codaobject of posterior samples for the likelihood value associated with each site. 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
execution time reported using
proc.time().- k.fold.deviance
scoring rule (deviance) from k-fold cross-validation. Only included if
k.foldis 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 <- sample(2:4, J, replace = TRUE)
beta <- c(0.5, -0.15)
p.occ <- length(beta)
alpha <- c(0.7, 0.4)
p.det <- length(alpha)
dat <- simOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha,
sp = FALSE)
occ.covs <- dat$X[, 2, drop = FALSE]
colnames(occ.covs) <- c('occ.cov')
det.covs <- list(det.cov = dat$X.p[, , 2])
# Data bundle
data.list <- list(y = dat$y,
occ.covs = occ.covs,
det.covs = det.covs)
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 2.72),
alpha.normal = list(mean = 0, var = 2.72))
# Initial values
inits.list <- list(alpha = 0, beta = 0,
z = apply(data.list$y, 1, max, na.rm = TRUE))
n.samples <- 5000
n.report <- 1000
# Note that this is just a test case and more iterations/chains may need to
# be run to ensure convergence.
out <- PGOcc(occ.formula = ~ occ.cov,
det.formula = ~ det.cov,
data = data.list,
inits = inits.list,
n.samples = n.samples,
priors = prior.list,
n.omp.threads = 1,
verbose = TRUE,
n.report = n.report,
n.burn = 1000,
n.thin = 1,
n.chains = 1)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Occupancy model with Polya-Gamma latent
#> variable fit with 100 sites.
#>
#> Samples per Chain: 5000
#> Burn-in: 1000
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 4000
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Sampled: 1000 of 5000, 20.00%
#> -------------------------------------------------
#> Sampled: 2000 of 5000, 40.00%
#> -------------------------------------------------
#> Sampled: 3000 of 5000, 60.00%
#> -------------------------------------------------
#> Sampled: 4000 of 5000, 80.00%
#> -------------------------------------------------
#> Sampled: 5000 of 5000, 100.00%
summary(out)
#>
#> Call:
#> PGOcc(occ.formula = ~occ.cov, det.formula = ~det.cov, data = data.list,
#> inits = inits.list, priors = prior.list, n.samples = n.samples,
#> n.omp.threads = 1, verbose = TRUE, n.report = n.report, n.burn = 1000,
#> n.thin = 1, n.chains = 1)
#>
#> Samples per Chain: 5000
#> Burn-in: 1000
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 4000
#> Run Time (min): 0.0056
#>
#> Occurrence (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.6177 0.2523 0.1414 0.6056 1.1385 NA 1951
#> occ.cov -0.2627 0.2292 -0.7259 -0.2607 0.1822 NA 2530
#>
#> Detection (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 0.8147 0.1908 0.4451 0.8201 1.1940 NA 1747
#> det.cov 0.6357 0.1733 0.3099 0.6332 0.9792 NA 2162