Description Usage Arguments Details Value Author(s) Examples
View source: R/nim_abundance.R
nimble.abund
is used to fit Binomial Mixture Models using Bayesian
estimation. The function returns model summary statistics, posterior draws,
and the BUGS code used to estimate the model.
1 2 3 4 5  nimble.abund(siteformula = NULL, obsformula = NULL, y = NULL,
sitevars = NULL, obsvars = NULL, mixture = c("Poisson", "ZIP", "NB"),
priors = c("Normal", "t", "Uniform", "Gamma"), dropbase = TRUE,
droplast = FALSE, niter = 10000, burnin = 1000, initmcmc = c(0.5, 1,
5, 10), chains = 4, returncode = TRUE, returnsamp = TRUE, ...)

siteformula 
A linear model formula for the latent state: log(λ_{i}). Formula must start with a tilde (~). Add 1 to remove intercept. Random effects are specified with a pipe (see lmer). If model contains an intercept, random effects will be centered at 0. Interactions are specified with * (e.g., x*y). 
obsformula 
A linear model formula for the observation process: logit(p_{i,j}). 
y 
The observed counts at site i during temporal replicate j. Must be provided as either a matrix or dataframe. Rows are sites and columns are replicates. 
sitevars 
A dataframe of sitespecific covariates. Must be a dataframe with named columns corresponding to variables. Number of rows should equal the number of sites. 
obsvars 
A named list with site and survey specific covariates. Each list element is a matrix with rows corresponding to site and columns to replicate. Variables specified in the model formula must match the variable names in the list. 
mixture 
There are three mixture options for the latent states: Poisson (default), ZeroInflated Poisson (ZIP), and Negative Binomial (NB). The ZIP mixture adds an extra hierarchial layer to the model, which is a Bernoulli random variable that divides sites between positive counts and sites with 0 counts. The NB link adds an extra term (rho) to model overdispersion. 
priors 
One prior is specified for all model parameters in the siteformula and obsformula model statements. The default prior is a Normal(mu = 0, sd = 1000). Other options are t(mu = 0, tau = 1000, df = 5), Uniform(0, 1000), and Gamma(shape = .001, rate = .001). 
dropbase 
Drops the first level of a factor variable. Default is TRUE. 
droplast 
Drops the last level of a factor variable. Default is FALSE. 
niter 
Number of iterations to run the MCMC. Default is 10,000 iterations. 
burnin 
Number of burnin iterations for MCMC. Default is 1000 iterations. 
initmcmc 
Vector of initial values for MCMC. Length of vector must equal the number of chains. The same initial values are used for all parameters. Default is c(1, 5, 10, 15) 
chains 
Number of MCMC chains to run. Default is 4. 
returncode 
Option to return the BUGS code passed to NIMBLE. Default is TRUE. 
returnsamp 
Option to return all posterior draws. Default is TRUE 
... 
Additional arguments can be passed to specify different parameters for the priors. Options are location, scale, lb, ub, df, shape, and rate. 
This is a function for fitting Binomial Mixture Models using a Bayesian framework. The model structure is:
N_{i} ~ Poisson(λ_{i})
y_{i,j}  N_{i} ~ Binomial(N_{i}, p_{i,j})
Where i is the number of sites and j the number of temporal replicates. N is a latent variable for population size. Both λ and p can be modeled as a linear function of covariates via a linked GLM. The link for λ is the log link. The link for p is the logit link. Estimation is performed using NIMBLE. In addition to fitting the model, generated BUGS code for the full model, as well as posterior draws, are returned.
Output is a named list with the following elements: Summary, BUGScode,
and Samples. In the Summary statement, variables from the siteformula will
start with "s.", and variables in the obsformula will start with "o." Note:
parameters are on the transformed scale (log for siteformula variables;
logit for obsformula variables). In addition to quantiles, the effective
sample size effectiveSize and Gelman Rubin diagnoistic
gelman.diag are provided from the coda package.
If the
ZIP mixture is selected theta is returned corresponding to the Bernoulli
random variable modeling if a site is suitable for a positive abundance
count.
If the NB mixture is selected log(alpha) is returned:
the log of the dispersion parameter, alpha, in the NB distribution.
Colin LewisBeck
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  # Simulate Abundance Data
R < 75
T < 3
X < runif(n = R, min = 1, max = 1)
A = c(rep('Red', 25), rep('Orange',20), rep('Blue', 30))
sim.covariates < data.frame("X" = X, "A" = A)
lam < exp(1 + 3*X)
N < rpois(n = R, lambda = lam)
p < plogis(5*X)
y < matrix(NA, nrow = R, ncol = T)
for (i in 1:T){y[,i] < rbinom(n = R, size = N, prob =p)}
#Fit Model
model < nimble.abund(siteformula = ~ 1 + (1A) + X, obsformula = ~
X, y = y, sitevars = sim.covariates, initmcmc = 1, chains = 1)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.