Title: | Univariate and Multivariate Spatial Modeling of Species Abundance |
---|---|
Description: | Fits single-species (univariate) and multi-species (multivariate) non-spatial and spatial abundance models in a Bayesian framework using Markov Chain Monte Carlo (MCMC). Spatial models are fit using Nearest Neighbor Gaussian Processes (NNGPs). Details on NNGP models are given in Datta, Banerjee, Finley, and Gelfand (2016) <doi:10.1080/01621459.2015.1044091> and Finley, Datta, and Banerjee (2022) <doi:10.18637/jss.v103.i05>. Fits single-species and multi-species spatial and non-spatial versions of generalized linear mixed models (Gaussian, Poisson, Negative Binomial), N-mixture models (Royle 2004 <doi:10.1111/j.0006-341X.2004.00142.x>) and hierarchical distance sampling models (Royle, Dawson, Bates (2004) <doi:10.1890/03-3127>). Multi-species spatial models are fit using a spatial factor modeling approach with NNGPs for computational efficiency. |
Authors: | Jeffrey Doser [aut, cre], Andrew Finley [aut] |
Maintainer: | Jeffrey Doser <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.1 |
Built: | 2024-11-24 06:10:42 UTC |
Source: | https://github.com/biodiverse/spabundance |
Function for fitting univariate abundance generalized linear (mixed) models
abund(formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
abund(formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
save.fitted |
logical value indicating whether or not fitted values and likelihood values
should be saved in the resulting model object. If |
... |
currently no additional arguments |
An object of class abund
that is a list comprised of:
beta.samples |
a |
kappa.samples |
a |
tau.sq.samples |
a |
y.rep.samples |
a two or three-dimensional array of posterior samples for the abundance replicate (fitted) values with dimensions corresponding to MCMC samples, site, and an optional third dimension of replicate. |
mu.samples |
a two or three-dimensional array of posterior samples for the expected abundance samples with dimensions corresponding to MCMC samples, site, and an optional third dimension of replicate. |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
like.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
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.
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(30), sigma.sq.mu = c(1.3)) kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4], abund.factor.1 = X.re[, , 1]) data.list <- list(y = y, covs = covs) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) tuning <- list(kappa = 0.2, beta = 0.1, beta.star = 0.2) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 | abund.factor.1), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(30), sigma.sq.mu = c(1.3)) kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4], abund.factor.1 = X.re[, , 1]) data.list <- list(y = y, covs = covs) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) tuning <- list(kappa = 0.2, beta = 0.1, beta.star = 0.2) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 | abund.factor.1), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Count data on 6 warblers in Pennsylvania, USA in 2018. Data come from the North American Breeding Bird Survey. Data indicate the total numer of individuals for each of 6 species counted at 50 stops along a 40km route (95 routes in the data set). The six species included in the data set are: (1) American Redstart (AMRE); (2) Blackburnian Warbler (BLBW); (3) Black-throated Blue Warbler; (4) Black-throated Green Warbler; (5) Hooded Warbler; and (6) Magnolia Warbler. Covariate data include three bioclimatic variables derived from PRISM. Two landcover variables (forest cover and developed land cover) come from USGS Earth Resources Observation and Science (EROS) Center.
data(bbsData)
data(bbsData)
bbsData
is a list with three elements:
y
: a two-dimensional matrix of count data with rows corresponding
to species (6) and columns corresponding to sites (95).
covs
: a data frame with 95 rows and 8 columns consisting of
covariates for use in modeling relative abundance.
coords
: a numeric matrix with 95 rows and two columns containing the
site coordinates. The proj4string is "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"
U.S. Geological Survey. Downloaded from https://www.sciencebase.gov/catalog/item/52b1dfa8e4b0d9b325230cd9 on August 25, 2023.
Daly, C., Halbleib, M., Smith, J. I., Gibson, W. P., Doggett, M. K., Taylor, G. H., Curtis, J., and Pasteris, P. P. (2008). Physiographically sensitive mapping of climatological temperature and precipitation across the conterminous united states. International Journal of Climatology: a Journal of the Royal Meteorological Society, 28(15):2031–2064
Ziolkowski Jr., D.J., Lutmerding, M., English, W.B., Aponte, V.I., and Hudson, M-A.R., 2023, North American Breeding Bird Survey Dataset 1966 - 2022: U.S. Geological Survey data release, https://doi.org/10.5066/P9GS9K64.
Sohl, T., Reker, R., Bouchard, M., Sayler, K., Dornbierer, J., Wika, S., ... & Friesz, A. (2016). Modeled historical land use and land cover for the conterminous United States. Journal of Land Use Science, 11(4), 476-499.
Bioclimatic and land cover variables extracted at a 12km resolution across the state of Pennsylvania, USA for use in predicting relative abundance of six warbler species across the state. Land cover data come from USGS EROS, while climate data come from PRISM.
data(bbsPredData)
data(bbsPredData)
bbsPredData
is a data frame with seven columns:
bio2
: bioclim variable 2.
bio8
: bioclim variable 8.
bio18
: bioclim variable 18.
forest
: proportion of forest cover within a 5km radius.
devel
: proportion of developed land cover within a 5km radius.
x
: the x coordinate of the point. The proj4strig is "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs".
y
: the y coordinate of the point. The proj4string is "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs".
Daly, C., Halbleib, M., Smith, J. I., Gibson, W. P., Doggett, M. K., Taylor, G. H., Curtis, J., and Pasteris, P. P. (2008). Physiographically sensitive mapping of climatological temperature and precipitation across the conterminous united states. International Journal of Climatology: a Journal of the Royal Meteorological Society, 28(15):2031–2064
Sohl, T., Reker, R., Bouchard, M., Sayler, K., Dornbierer, J., Wika, S., ... & Friesz, A. (2016). Modeled historical land use and land cover for the conterminous United States. Journal of Land Use Science, 11(4), 476-499.
A simulated data set of repeated count data for 6 species across 225 sites with a maximum of 3 replicate surveys performed at a given site.
data(dataNMixSim)
data(dataNMixSim)
dataNMixSim
is a list with four elements:
y
: a three-dimensional array of count data with
dimensions of species (6), sites (225) and replicates (3).
abund.covs
: a numeric matrix with 225 rows and two columns consisting
of a continuous covariate and a categorical variable which may both influence
abundance of the different species.
det.covs
: a list of two numeric matrices with 225 rows and 3 columns.
Both matrices contain a continuous covariate that may affect detection probability
of the species
coords
: a numeric matrix with 225 rows and two columns containing the
site coordinates (X and Y). Note the data are generated across
a unit square (i.e., the x and y coordinates are both between 0 and 1).
set.seed(6) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) # n.rep <- rep(5, J) n.sp <- 6 # Community-level covariate effects # Occurrence beta.mean <- c(-1, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.4, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() mu.RE <- list(levels = c(10), sigma.sq.mu = c(0.5), beta.indx = list(1)) p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } alpha.true <- alpha sp <- TRUE n.factors <- 3 factor.model <- TRUE phi <- runif(n.factors, 3/1, 3 / .2) kappa <- runif(n.sp, 0.1, 1) family <- 'Poisson' dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = sp, kappa = kappa, family = family, factor.model = factor.model, phi = phi, cov.model = 'exponential', n.factors = n.factors) table(dat$N) apply(dat$N, 1, sum) y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re coords <- dat$coords dimnames(coords)[[2]] <- c('X', 'Y') # Package all data into a list abund.covs <- cbind(X, X.re) colnames(abund.covs) <- c('int', 'abund.cov.1', 'abund.factor.1') abund.covs <- abund.covs[, -1] det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) dataNMixSim <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords)
set.seed(6) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) # n.rep <- rep(5, J) n.sp <- 6 # Community-level covariate effects # Occurrence beta.mean <- c(-1, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.4, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() mu.RE <- list(levels = c(10), sigma.sq.mu = c(0.5), beta.indx = list(1)) p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } alpha.true <- alpha sp <- TRUE n.factors <- 3 factor.model <- TRUE phi <- runif(n.factors, 3/1, 3 / .2) kappa <- runif(n.sp, 0.1, 1) family <- 'Poisson' dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = sp, kappa = kappa, family = family, factor.model = factor.model, phi = phi, cov.model = 'exponential', n.factors = n.factors) table(dat$N) apply(dat$N, 1, sum) y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re coords <- dat$coords dimnames(coords)[[2]] <- c('X', 'Y') # Package all data into a list abund.covs <- cbind(X, X.re) colnames(abund.covs) <- c('int', 'abund.cov.1', 'abund.factor.1') abund.covs <- abund.covs[, -1] det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) dataNMixSim <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords)
Function for fitting single-sepcies hierarchical distance sampling models
DS(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
DS(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name, whose value
defines the initial tuning variance of the adaptive sampler. Valid tags include
|
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 number of MCMC samples in each batch in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
transect |
the type of transect. Currently supports line transects ( |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class DS
that is a list comprised of:
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
N.samples |
a |
mu.samples |
a |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
y.rep.samples |
a three-dimensional array of fitted values. Array dimensions correspond to MCMC samples, sites, and distance band. |
pi.samples |
a three-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, sites, and distance band. |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
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.
Royle, J. A., Dawson, D. K., & Bates, S. (2004). Modeling abundance effects in distance sampling. Ecology, 85(6), 1591-1597.
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- FALSE family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect) y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2) out <- DS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out)
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- FALSE family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect) y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2) out <- DS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out)
Method for extracting model fitted values from a fitted GLMM (abund
).
## S3 method for class 'abund' fitted(object, ...)
## S3 method for class 'abund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class abund
.
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and replicates
Method for extracting model fitted values and cell-specific detection probabilities from a hierarchical distance sampling (DS
) model.
## S3 method for class 'DS' fitted(object, ...)
## S3 method for class 'DS' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class DS
.
A list comprised of:
y.rep.samples |
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and distance bin. |
pi.samples |
A three-dimensional numeric array of cell-specific detection probability values. Values correspond to the probability of detecting an individual within a given distance band at a given location. Array dimensions correspond to MCMC samples, sites, and distance band. |
Method for extracting model fitted values from a fitted latent factor multivariate GLMM (lfMsAbund
).
## S3 method for class 'lfMsAbund' fitted(object, ...)
## S3 method for class 'lfMsAbund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class lfMsAbund
.
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates.
Method for extracting model fitted values and cell-specific detection probabilities from a latent factor multi-species hierarchical distance sampling (lfMsDS
) model.
## S3 method for class 'lfMsDS' fitted(object, ...)
## S3 method for class 'lfMsDS' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class lfMsDS
.
A list comprised of:
y.rep.samples |
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and distance bin. |
pi.samples |
A four-dimensional numeric array of cell-specific detection probability values. Values correspond to the probability of detecting an individual within a given distance band at a given location. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
Method for extracting model fitted values and detection probability values from a fitted latent factor multi-species N-mixture (lfMsNMix
) model.
## S3 method for class 'lfMsNMix' fitted(object, type = 'marginal', ...)
## S3 method for class 'lfMsNMix' fitted(object, type = 'marginal', ...)
object |
object of class |
type |
a character string indicating whether fitted values should be generated conditional on the estimated latent abundance values ( |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probability values for fitted model objects of class lfMsNMix
.
A list comprised of:
y.rep.samples |
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates. |
p.samples |
A four-dimensional numeric array of detection probability values. Array dimensions correspond to MCMC samples, species, sites, and replicates. |
Method for extracting model fitted values from a fitted multivariate GLMM (msAbund
).
## S3 method for class 'msAbund' fitted(object, ...)
## S3 method for class 'msAbund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class msAbund
.
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates.
Method for extracting model fitted values and cell-specific detection probabilities from a multi-species hierarchical distance sampling (msDS
) model.
## S3 method for class 'msDS' fitted(object, ...)
## S3 method for class 'msDS' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class msDS
.
A list comprised of:
y.rep.samples |
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and distance bin. |
pi.samples |
A four-dimensional numeric array of cell-specific detection probability values. Values correspond to the probability of detecting an individual within a given distance band at a given location. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
Method for extracting model fitted values and detection probability values from a fitted multi-species N-mixture (msNMix
) model.
## S3 method for class 'msNMix' fitted(object, type = 'marginal', ...)
## S3 method for class 'msNMix' fitted(object, type = 'marginal', ...)
object |
object of class |
type |
a character string indicating whether fitted values should be generated conditional on the estimated latent abundance values ( |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probability values for fitted model objects of class msNMix
.
A list comprised of:
y.rep.samples |
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates. |
p.samples |
A four-dimensional numeric array of detection probability values. Array dimensions correspond to MCMC samples, species, sites, and replicates. |
Method for extracting model fitted values and detection probabilities from a fitted N-mixture (NMix
) model.
## S3 method for class 'NMix' fitted(object, type = 'marginal', ...)
## S3 method for class 'NMix' fitted(object, type = 'marginal', ...)
object |
object of class |
type |
a character string indicating whether fitted values should be generated conditional on the estimated latent abundance values ( |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class NMix
.
A list comprised of:
y.rep.samples |
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and replicates. |
p.samples |
A three-dimensional numeric array of detection probability values. Array dimensions correspond to MCMC samples, sites, and replicates. |
Method for extracting model fitted values from a fitted spatial factor multivariate GLMM (sfMsAbund
).
## S3 method for class 'sfMsAbund' fitted(object, ...)
## S3 method for class 'sfMsAbund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class sfMsAbund
.
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates.
Method for extracting model fitted values and cell-specific detection probabilities from a spatial factor multi-species hierarchical distance sampling (sfMsDS
) model.
## S3 method for class 'sfMsDS' fitted(object, ...)
## S3 method for class 'sfMsDS' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class sfMsDS
.
A list comprised of:
y.rep.samples |
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and distance bin. |
pi.samples |
A four-dimensional numeric array of cell-specific detection probability values. Values correspond to the probability of detecting an individual within a given distance band at a given location. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
Method for extracting model fitted values and detection probability values from a fitted spatial factor multi-species N-mixture (sfMsNMix
) model.
## S3 method for class 'sfMsNMix' fitted(object, type = 'marginal', ...)
## S3 method for class 'sfMsNMix' fitted(object, type = 'marginal', ...)
object |
object of class |
type |
a character string indicating whether fitted values should be generated conditional on the estimated latent abundance values ( |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probability values for fitted model objects of class sfMsNMix
.
A list comprised of:
y.rep.samples |
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates. |
p.samples |
A four-dimensional numeric array of detection probability values. Array dimensions correspond to MCMC samples, species, sites, and replicates. |
Method for extracting model fitted values from a fitted spatial GLMM (spAbund
).
## S3 method for class 'spAbund' fitted(object, ...)
## S3 method for class 'spAbund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class spAbund
.
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and replicates
Method for extracting model fitted values and cell-specific detection probabilities from a spatial hierarchical distance sampling (spDS
) model.
## S3 method for class 'spDS' fitted(object, ...)
## S3 method for class 'spDS' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class spDS
.
A list comprised of:
y.rep.samples |
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and distance bin. |
pi.samples |
A three-dimensional numeric array of cell-specific detection probability values. Values correspond to the probability of detecting an individual within a given distance band at a given location. Array dimensions correspond to MCMC samples, sites, and distance band. |
Method for extracting model fitted values and detection probabilities from a fitted spatial N-mixture (spNMix
) model.
## S3 method for class 'spNMix' fitted(object, type = 'marginal', ...)
## S3 method for class 'spNMix' fitted(object, type = 'marginal', ...)
object |
object of class |
type |
a character string indicating whether fitted values should be generated conditional on the estimated latent abundance values ( |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values and detection probabilities for fitted model objects of class spNMix
.
A list comprised of:
y.rep.samples |
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and replicates. |
p.samples |
A three-dimensional numeric array of detection probability values. Array dimensions correspond to MCMC samples, sites, and replicates. |
Method for extracting model fitted values from a fitted spatially-varying coefficient GLMM (svcAbund
).
## S3 method for class 'svcAbund' fitted(object, ...)
## S3 method for class 'svcAbund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class svcAbund
.
A three-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, sites, and replicates
Method for extracting model fitted values from a fitted multivatiate spatially-varying coefficient GLMM (svcMsAbund
).
## S3 method for class 'svcMsAbund' fitted(object, ...)
## S3 method for class 'svcMsAbund' fitted(object, ...)
object |
object of class |
... |
currently no additional arguments |
A method to the generic fitted
function to extract fitted values for fitted model objects of class svcMsAbund
.
A four-dimensional numeric array of fitted values for use in Goodness of Fit assessments. Array dimensions correspond to MCMC samples, species, sites, and replicates.
Repeated count data of 12 foliage gleaning bird species in 2015 in the Hubbard Brook Experimental Forest (HBEF) in New Hampshire, USA. Data were collected at 373 sites over three replicate point counts each of 10 minutes in length, with a detection radius of 100m. Some sites were not visited for all three replicates. The 12 species included in the data set are as follows: (1) AMRE: American Redstart; (2) BAWW: Black-and-white Warbler; (3) BHVI: Blue-headed Vireo; (4) BLBW: Blackburnian Warbler; (5) BLPW: Blackpoll Warbler; (6) BTBW: Black-throated Blue Warbler; (7) BTNW: BLack-throated Green Warbler; (8) CAWA: Canada Warbler; (9) MAWA: Magnolia Warbler; (10) NAWA: Nashville Warbler; (11) OVEN: Ovenbird; (12) REVI: Red-eyed Vireo.
data(hbefCount2015)
data(hbefCount2015)
hbefCount2015
is a list with four elements:
y
: a three-dimensional array of count data with
dimensions of species (12), sites (373) and replicates (3).
abund.covs
: a data frame with 373 rows and one column consisting of the
elevation at each site.
det.covs
: a list of two numeric matrices with 373 rows and 3 columns.
The first element is the day of year when the survey was
conducted for a given site and replicate. The second element is the
time of day when the survey was conducted.
coords
: a numeric matrix with 373 rows and two columns containing the
site coordinates (Easting and Northing) in UTM Zone 19. The proj4string is
"+proj=utm +zone=19 +units=m +datum=NAD83".
Rodenhouse, N. and S. Sillett. 2019. Valleywide Bird Survey, Hubbard Brook Experimental Forest, 1999-2016 (ongoing) ver 3. Environmental Data Initiative. doi:10.6073/pasta/faca2b2cf2db9d415c39b695cc7fc217 (Accessed 2021-09-07)
Doser, J. W., Leuenberger, W., Sillett, T. S., Hallworth, M. T. & Zipkin, E. F. (2022). Integrated community occupancy models: A framework to assess occurrence and biodiversity dynamics using multiple data sources. Methods in Ecology and Evolution, 00, 1-14. doi:10.1111/2041-210X.13811
Function for fitting multivariate generalized linear (mixed) models with species correlations (i.e., an abundance-based joint species distribution model). We use a factor modeling approach for dimension reduction.
lfMsAbund(formula, data, inits, priors, tuning, n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
lfMsAbund(formula, data, inits, priors, tuning, n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name, whose
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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.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. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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 |
verbose |
if |
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.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
save.fitted |
logical value indicating whether or not fitted values and likelihood values
should be saved in the resulting model object. If |
... |
currently no additional arguments |
An object of class lfMsAbund
that is a list comprised of:
beta.comm.samples |
a |
tau.sq.beta.samples |
a |
beta.samples |
a |
kappa.samples |
a |
tau.sq.samples |
a |
lambda.samples |
a |
w.samples |
a three-dimensional array of posterior samples for the latent effects for each latent factor. Array dimensions correspond to MCMC sample, latent factor, then site. |
y.rep.samples |
a three or four-dimensional array of posterior samples for the fitted (replicate) values for each species with dimensions corresponding to MCMC sample, species, site, and replicate. |
mu.samples |
a three or four-dimensional array of posterior samples for the expected abundance values for each species with dimensions corresponding to MCMC samples, species, site, and replicate. |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
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.
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) factor.model <- TRUE n.factors <- 3 dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, beta.star = 0.1, lambda = 0.5, w = 0.5) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- lfMsAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) + (1 | abund.factor.2), data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.factors = n.factors, n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) factor.model <- TRUE n.factors <- 3 dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, beta.star = 0.1, lambda = 0.5, w = 0.5) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- lfMsAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) + (1 | abund.factor.2), data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.factors = n.factors, n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Function for fitting latent factor multi-species hierarchical distance sampling models.
lfMsDS(abund.formula, det.formula, data, inits, priors, tuning, n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
lfMsDS(abund.formula, det.formula, data, inits, priors, tuning, n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name, whose
value defines the initial variance of the adaptive sampler.
Valid tags are |
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.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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
transect |
the type of transect. Currently supports line transects ( |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class lfMsDS
that is a list comprised of:
beta.comm.samples |
a |
alpha.comm.samples |
a |
tau.sq.beta.samples |
a |
tau.sq.alpha.samples |
a |
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
lambda.samples |
a |
w.samples |
a three-dimensional array of posterior samples for the latent effects for each latent factor. |
N.samples |
a three-dimensional array of posterior samples for the latent abundance values for each species. Note that these values always represent transect-level abundance, even when an offset is supplied. Array dimensions correspond to MCMC sample, species, and site. |
mu.samples |
a three-dimensional array of posterior samples for
the latent expected abundance values for each species. When an offset
is supplied in the |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
y.rep.samples |
a four-dimensional array of fitted values. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
pi.samples |
a four-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
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.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Sollmann, R., Gardner, B., Williams, K. A., Gilbert, A. T., & Veit, R. R. (2016). A hierarchical distance sampling model to estimate abundance and covariate associations of species and communities. Methods in Ecology and Evolution, 7(5), 529-537.
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) y <- dat$y X <- dat$X X.p <- dat$X.p coords <- dat$coords dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- lfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) y <- dat$y X <- dat$X X.p <- dat$X.p coords <- dat$coords dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- lfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
Function for fitting multi-species N-mixture models with species correlations (i.e., an abundance-based joint species distribution model with imperfect detection). We use a factor modeling approach for dimension reduction.
lfMsNMix(abund.formula, det.formula, data, inits, priors, tuning, n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
lfMsNMix(abund.formula, det.formula, data, inits, priors, tuning, n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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.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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class lfMsNMix
that is a list comprised of:
beta.comm.samples |
a |
alpha.comm.samples |
a |
tau.sq.beta.samples |
a |
tau.sq.alpha.samples |
a |
beta.samples |
a |
alpha.samples |
a |
lambda.samples |
a |
w.samples |
a three-dimensional array of posterior samples for the latent effects for each latent factor. |
kappa.samples |
a |
N.samples |
a three-dimensional array of posterior samples for the latent abundance values for each species. |
mu.samples |
a three-dimensional array of posterior samples for the latent expected abundance values for each species. |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
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 |
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()
.
Jeffrey W. Doser [email protected],
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.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Yamaura, Y., Royle, J. A., Shimada, N., Asanuma, S., Sato, T., Taki, H., & Makino, S. I. (2012). Biodiversity of man-made open habitats in an underused country: a class of multispecies abundance models for count data. Biodiversity and Conservation, 21(6), 1365-1380.
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(5, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } n.factors <- 3 dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = FALSE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors) y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re coords <- dat$coords # Package all data into a list abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = as.data.frame(X.p[, , 2]), det.cov.2 = as.data.frame(X.p[, , 3])) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.comm.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) inits.list <- list(beta.comm = 0, alpha.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 0.5, tau.sq.alpha = 0.5, N = apply(y, c(1, 2), max, na.rm = TRUE)) tuning.list <- list(beta = 0.5, alpha = 0.5, lambda = 0.5, w = 0.5) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- lfMsNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = n.factors, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(5, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } n.factors <- 3 dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = FALSE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors) y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re coords <- dat$coords # Package all data into a list abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = as.data.frame(X.p[, , 2]), det.cov.2 = as.data.frame(X.p[, , 3])) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.comm.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) inits.list <- list(beta.comm = 0, alpha.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 0.5, tau.sq.alpha = 0.5, N = apply(y, c(1, 2), max, na.rm = TRUE)) tuning.list <- list(beta = 0.5, alpha = 0.5, lambda = 0.5, w = 0.5) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- lfMsNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = n.factors, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
The function msAbund
fits multivariate abundance GLMMs.
msAbund(formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
msAbund(formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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 |
verbose |
if |
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.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
save.fitted |
logical value indicating whether or not fitted values and likelihood values
should be saved in the resulting model object. If |
... |
currently no additional arguments |
An object of class msAbund
that is a list comprised of:
beta.comm.samples |
a |
tau.sq.beta.samples |
a |
beta.samples |
a |
kappa.samples |
a |
tau.sq.samples |
a |
y.rep.samples |
a three or four-dimensional array of posterior samples for the fitted (replicate) values for each species with dimensions corresponding to MCMC sample, species, site, and replicate. |
mu.samples |
a three or four-dimensional array of posterior samples for the expected abundance values for each species with dimensions corresponding to MCMC samples, species, site, and replicate. |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
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.
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, beta.star = 0.1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- msAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) + (1 | abund.factor.2), data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, beta.star = 0.1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- msAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) + (1 | abund.factor.2), data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Function for fitting multi-species hierarchical distance sampling models.
msDS(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
msDS(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name, whose
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
transect |
the type of transect. Currently supports line transects ( |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class msDS
that is a list comprised of:
beta.comm.samples |
a |
alpha.comm.samples |
a |
tau.sq.beta.samples |
a |
tau.sq.alpha.samples |
a |
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
N.samples |
a three-dimensional array of posterior samples for the latent abundance values for each species. Note that these values always represent transect-level abundance, even when an offset is supplied. Array dimensions correspond to MCMC sample, species, and site. |
mu.samples |
a three-dimensional array of posterior samples for
the latent expected abundance values for each species. When an offset
is supplied in the |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
y.rep.samples |
a four-dimensional array of fitted values. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
pi.samples |
a four-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
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.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Sollmann, R., Gardner, B., Williams, K. A., Gilbert, A. T., & Veit, R. R. (2016). A hierarchical distance sampling model to estimate abundance and covariate associations of species and communities. Methods in Ecology and Evolution, 7(5), 529-537.
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- FALSE dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model) y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- msDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- FALSE dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model) y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- msDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
Function for fitting multi-species N-mixture models.
msNMix(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
msNMix(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class msNMix
that is a list comprised of:
beta.comm.samples |
a |
alpha.comm.samples |
a |
tau.sq.beta.samples |
a |
tau.sq.alpha.samples |
a |
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
N.samples |
a three-dimensional array of posterior samples for the latent abundance values for each species. |
mu.samples |
a three-dimensional array of posterior samples for the latent expected abundance values for each species. |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
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 |
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()
.
Jeffrey W. Doser [email protected],
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.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Yamaura, Y., Royle, J. A., Shimada, N., Asanuma, S., Sato, T., Taki, H., & Makino, S. I. (2012). Biodiversity of man-made open habitats in an underused country: a class of multispecies abundance models for count data. Biodiversity and Conservation, 21(6), 1365-1380.
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(5, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = FALSE, family = 'Poisson') y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re # Package all data into a list abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = as.data.frame(X.p[, , 2]), det.cov.2 = as.data.frame(X.p[, , 3])) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) prior.list <- list(beta.comm.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.comm.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) inits.list <- list(beta.comm = 0, alpha.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 0.5, tau.sq.alpha = 0.5, N = apply(y, c(1, 2), max, na.rm = TRUE)) tuning.list <- list(beta = 0.5, alpha = 0.5) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- msNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(5, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = FALSE, family = 'Poisson') y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re # Package all data into a list abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = as.data.frame(X.p[, , 2]), det.cov.2 = as.data.frame(X.p[, , 3])) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) prior.list <- list(beta.comm.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.comm.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) inits.list <- list(beta.comm = 0, alpha.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 0.5, tau.sq.alpha = 0.5, N = apply(y, c(1, 2), max, na.rm = TRUE)) tuning.list <- list(beta = 0.5, alpha = 0.5) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- msNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
Distance sampling data of 16 bird species in 2018 in the Disney Wilderness Preserve in Florida, USA. These data were collected as part of the National Ecological Observatory Network (NEON). Data were collected at 90 sites where observers recorded the number of all bird species observed during a six minute, unlimited radius point count survey once during the breeding season. Distance of each individual bird to the observer was recorded using a laser rangefinder. For modeling, we binned the distance measurements into 4 distance bins, and only used observations within 250m. The 16 species included in the data set are as follows: (1) EATO (Eastern Towhee); (2) EAME (Eastern Meadowlark); (3) AMCR (American Crow); (4) BACS (Bachman's Sparrow); (5) CARW (Carolina Wren); (6) COGD (Common Ground Dove); (7) CONI (Common Nighthawk); (8) COYE (Common Yellowthroat); (9) EABL (Eastern Bluebird); (10) GCFL (Great-crested Flycatcher); (11) MODO (Mourning Dover); (12) NOCA (Northern Cardinal); (13) NOMO (Northern Mockingbird); (14) RBWO (Red-bellied Woodpecker); (15) RHWO (Red-headed Woodpecker); (16) WEVI (White-eyed Vireo).
data(neonDWP)
data(neonDWP)
neonDWP
is a list with five elements:
y
: a three-dimensional array of distance sampling data with
dimensions of species (16), sites (90) and distance bin (4).
covs
: a data frame with 90 rows and four columns consisting of
covariates for use in modeling abundance and/or detection.
dist.breaks
: a vector of five values indicating the break points
of the four distance bands in the data.
offset
: an offset used to scale the 250m radius point count surveys
to ha, such that resulting estimates will be the number of individuals
per ha.
coords
: a numeric matrix with 373 rows and two columns containing the
site coordinates (Easting and Northing) in UTM Zone 17N. The EPSG is 32617.
These data were updated on November 28, 2023 (v0.1.1), and thus any analyses run with the data from v0.1.0 will not give the same results. The data were updated after NEON discovered an error in how the bird count data was associated with the spatial locations of each site. These data have been updated following the procedures outlined at https://www.neonscience.org/impact/observatory-blog/bird-point-ids-within-grids-were-transposed-resulting-inaccurate-point.
NEON (National Ecological Observatory Network). Breeding landbird point counts (DP1.10003.001), RELEASE-2023. https://doi.org/10.48443/00pg-vm19. Dataset accessed from https://data.neonscience.org on May 25, 2023
Barnett, D. T., Duffy, P. A., Schimel, D. S., Krauss, R. E., Irvine, K. M., Davis, F. W.,Gross, J. E., Azuaje, E. I., Thorpe, A. S., Gudex-Cross, D., et al. (2019). The terrestrial organism and biogeochemistry spatial sampling design for the national ecological observatory network. Ecosphere, 10(2):e02540.
Land cover covariates (forest cover and grassland cover) extracted at a 1km resolution across the Disney Wildnerss Preserve for use in predicting density across the park. Land cover data come from USGS EROS.
data(neonPredData)
data(neonPredData)
neonPredData
is a data frame with four columns:
forest
: proportion of forest cover within 1km radius.
grass
: proportion of grassland cover within 1km radius.
Easting
: the x coordinate of the point. The EPSG is 32617 (UTM Zone 17N).
Northing
: the y coordinate of the point. The EPSG is 32617 (UTM Zone 17N).
USGS Earth Resources Observation and Science Ceneter https://www.usgs.gov/centers/eros
Function for fitting single-species N-mixture models.
NMix(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
NMix(abund.formula, det.formula, data, inits, priors, tuning, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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
|
verbose |
if |
n.report |
the interval to report Metropolis sampler acceptance and MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class NMix
that is a list comprised of:
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
N.samples |
a |
mu.samples |
a |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
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 |
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()
.
Jeffrey W. Doser [email protected],
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.
Roberts, G.O. and Rosenthal J.S. (2009) Examples of adaptive MCMC. Journal of Computational and Graphical Statistics, 18(2):349-367.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115. doi:10.1111/j.0006-341X.2004.00142.x.
set.seed(1010) J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- 3/.6 sigma.sq <- 2 kappa <- 0.3 sp <- FALSE cov.model <- 'exponential' dist <- 'NB' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.p <- dat$X.p abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = kappa, N = apply(y, 1, max, na.rm = TRUE)) n.batch <- 4 batch.length <- 25 n.burn <- 50 n.thin <- 1 n.chains <- 1 out <- NMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains)
set.seed(1010) J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- 3/.6 sigma.sq <- 2 kappa <- 0.3 sp <- FALSE cov.model <- 'exponential' dist <- 'NB' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.p <- dat$X.p abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = kappa, N = apply(y, 1, max, na.rm = TRUE)) n.batch <- 4 batch.length <- 25 n.burn <- 50 n.thin <- 1 n.chains <- 1 out <- NMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains)
Function for performing posterior predictive checks on spAbundance
model objects.
ppcAbund(object, fit.stat, group, type = 'marginal', ...)
ppcAbund(object, fit.stat, group, type = 'marginal', ...)
object |
an object of class |
fit.stat |
a quoted keyword that specifies the fit statistic
to use in the posterior predictive check. Supported fit statistics are
|
group |
a positive integer indicating the way to group the abundance data for the posterior predictive check. Value 0 will not group the data and use the raw counts, 1 will group values by row (site), and value 2 will group values by column (replicate). |
type |
a character string indicating whether fitted values should be generated conditional on the estimated latent abundance values ( |
... |
currently no additional arguments |
An object of class ppcAbund
that is a list comprised of:
fit.y |
a numeric vector of posterior samples for the
fit statistic calculated on the observed data when |
fit.y.rep |
a numeric vector of posterior samples for the
fit statistic calculated on a replicate data set generated from the
model when |
fit.y.group.quants |
a matrix consisting of posterior quantiles
for the fit statistic using the observed data for each unique element
the fit statistic is calculated for (i.e., observations when group = 0, sites when group = 1,
replicates when group = 2) when |
fit.y.rep.group.quants |
a matrix consisting of posterior quantiles
for the fit statistic using the model replicated data for each unique element
the fit statistic is calculated for (i.e., observations when group = 0, sites when group = 1,
replicates when group = 2) when |
The return object will include additional objects used for standard extractor functions.
ppcAbund
will return an error for Gaussian or zero-inflated Gaussian models.
For Gaussian models, standard residual diagnostics can be used to assess model fit.
Jeffrey W. Doser [email protected],
set.seed(1010) J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- 3/.6 sigma.sq <- 2 kappa <- 0.3 sp <- FALSE cov.model <- 'exponential' dist <- 'NB' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.p <- dat$X.p abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = kappa, N = apply(y, 1, max, na.rm = TRUE)) tuning <- 0.5 n.batch <- 4 batch.length <- 25 n.burn <- 50 n.thin <- 1 n.chains <- 1 out <- NMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Posterior predictive check ppc.out <- ppcAbund(out, fit.stat = 'chi-squared', group = 0) summary(ppc.out)
set.seed(1010) J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- 3/.6 sigma.sq <- 2 kappa <- 0.3 sp <- FALSE cov.model <- 'exponential' dist <- 'NB' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.p <- dat$X.p abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = kappa, N = apply(y, 1, max, na.rm = TRUE)) tuning <- 0.5 n.batch <- 4 batch.length <- 25 n.burn <- 50 n.thin <- 1 n.chains <- 1 out <- NMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Posterior predictive check ppc.out <- ppcAbund(out, fit.stat = 'chi-squared', group = 0) summary(ppc.out)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'abund'.
## S3 method for class 'abund' predict(object, X.0, ignore.RE = FALSE, z.0.samples, ...)
## S3 method for class 'abund' predict(object, X.0, ignore.RE = FALSE, z.0.samples, ...)
object |
an object of class abund |
X.0 |
the design matrix of covariates at the prediction locations. This should be a three-dimensional array, with dimensions corresponding to site, replicate, and covariate, respectively. Note that the first covariate should consist of all 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance portion of the model, the levels of the random effects at the new locations/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data in |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a matrix with rows corresponding to MCMC samples and columns corresponding to prediction locations containing the full posterior samples of the predicted binary portion of a zero-inflated Gaussian model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of the species at the location. When using |
... |
currently no additional arguments |
A list object of class predict.abund
. The list consists of:
mu.0.samples |
a three-dimensional object of posterior predictive samples for the
expected abundance values with dimensions corresponding to posterior predictive
sample, site, and replicate. When there is no replication, this will be a two-dimensional
matrix. Note if an offset was used when fitting the model with |
y.0.samples |
a three-dimensional object of posterior predictive samples for the
abundance values with dimensions corresponding to posterior predictive sample, site,
and replicate. When there is no replication, this will be a two-dimensional matrix.
These will be in the same units as |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(1, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list() kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Prediction covariates X.0 <- dat$X[pred.indx, , ] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) abund.covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4]) data.list <- list(y = y, covs = abund.covs) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(1, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list() kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Prediction covariates X.0 <- dat$X[pred.indx, , ] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) abund.covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4]) data.list <- list(y = y, covs = abund.covs) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'DS'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'DS' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
## S3 method for class 'DS' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
object |
an object of class DS |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
ignore.RE |
logical value that specifies whether or not to remove random abundance (or detection if |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict latent abundance and expected abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
... |
currently no additional arguments |
A list object of class predict.DS
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a |
N.0.samples |
a |
When type = 'detection'
, the list consists of:
sigma.0.samples |
a |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- FALSE family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, family = family, offset = offset, transect = transect) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, ] dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2) out <- DS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- FALSE family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, family = family, offset = offset, transect = transect) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, ] dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2) out <- DS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'lfMsAbund'.
## S3 method for class 'lfMsAbund' predict(object, X.0, coords.0, ignore.RE = FALSE, z.0.samples, include.w = TRUE, ...)
## S3 method for class 'lfMsAbund' predict(object, X.0, coords.0, ignore.RE = FALSE, z.0.samples, include.w = TRUE, ...)
object |
an object of class lfMsAbund |
X.0 |
the design matrix of covariates at the prediction locations. This can be either a two-dimensional matrix with rows corresponding to sites and columns corresponding to covariates, or can be a three-dimensional array, with dimensions corresponding to site, replicate, and covariate, respectively. Note that the first covariate should consist of all 1s for the intercept if an intercept is included in the model. If random effects are included in the the model, the levels of the random effects at the new locations/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data in |
coords.0 |
the spatial coordinates corresponding to |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a three-dimensional array with dimensions corresponding to MCMC samples, species, and prediction locations. The array contains the full posterior samples of the predicted binary portion of a Gaussian zero-inflated model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of each species at the location. When using |
include.w |
a logical value used to indicate whether the latent random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.lfMsAbund
. The list consists of:
mu.0.samples |
a three or four-dimensional object of posterior predictive samples for the
expected abundance values with dimensions corresponding to posterior predictive
sample, species, site, and replicate. Note if an offset was used when fitting the model
with |
y.0.samples |
a three or four-dimensional object of posterior predictive samples for the
abundance values with dimensions corresponding to posterior predictive sample, species, site,
and replicate. These will be in the same units as |
w.0.samples |
a three-dimensional array of posterior predictive samples for the latent factors. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) factor.model <- TRUE n.factors <- 3 dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, , drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values y.0 <- dat$y[, pred.indx, , drop = FALSE] X.0 <- dat$X[pred.indx, , , drop = FALSE] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, lambda = 0.5, w = 0.5) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- lfMsAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) factor.model <- TRUE n.factors <- 3 dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, , drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values y.0 <- dat$y[, pred.indx, , drop = FALSE] X.0 <- dat$X[pred.indx, , , drop = FALSE] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, lambda = 0.5, w = 0.5) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- lfMsAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'lfMsDS'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'lfMsDS' predict(object, X.0, coords.0, ignore.RE = FALSE, type = 'abundance', include.w = TRUE, ...)
## S3 method for class 'lfMsDS' predict(object, X.0, coords.0, ignore.RE = FALSE, type = 'abundance', include.w = TRUE, ...)
object |
an object of class lfMsDS |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
coords.0 |
the spatial coordinates corresponding to |
ignore.RE |
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect. |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
include.w |
a logical value used to indicate whether the latent random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.lfMsDS
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a three-dimensional array of posterior predictive samples for the
expected abundance values, or expected abundance values per unit area (i.e., density)
values when an offset was used when fitting the model with |
N.0.samples |
a three-dimensional array of posterior predictive samples for the
latent abundance values. These will be in the same units as |
When type = 'detection'
, the list consists of:
sigma.0.samples |
a three-dimensional array of posterior predictive samples for sigma (the parameter controlling detection probability). |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'NB' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Occupancy covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , drop = FALSE] X.p.0 <- dat$X.p[pred.indx, , drop = FALSE] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- lfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'NB' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Occupancy covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , drop = FALSE] X.p.0 <- dat$X.p[pred.indx, , drop = FALSE] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- lfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'lfMsNMix'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'lfMsNMix' predict(object, X.0, coords.0, ignore.RE = FALSE, type = 'abundance', include.w = TRUE, ...)
## S3 method for class 'lfMsNMix' predict(object, X.0, coords.0, ignore.RE = FALSE, type = 'abundance', include.w = TRUE, ...)
object |
an object of class lfMsNMix |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
coords.0 |
the spatial coordinates corresponding to |
ignore.RE |
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect. |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
include.w |
a logical value used to indicate whether the latent random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.lfMsNMix
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a three-dimensional array of posterior predictive samples for the
expected abundance values. Note these will be per unit area if an offset was used when
fitting the model with |
N.0.samples |
a three-dimensional array of posterior predictive samples for the
latent abundance values. These will be in the same units as |
When type = 'detection'
, the list consists of:
p.0.samples |
a three-dimensional array of posterior predictive samples for the detection probability values. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(400) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep<- sample(2:4, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0.2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2, -0.1) tau.sq.alpha <- c(0.2, 0.3, 1) p.det <- length(alpha.mean) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } family <- 'Poisson' n.factors <- 3 dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, sp = FALSE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values X.0 <- dat$X[pred.indx, ] mu.0 <- dat$psi[, pred.indx] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Occupancy initial values 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)) # Initial values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 1, tau.sq.alpha = 1, N = apply(y, c(1, 2), max, na.rm = TRUE)) # Tuning values tuning <- list(beta = 0.3, alpha = 0.3, lambda = 0.5, w = 0.5) n.batch <- 4 batch.length <- 25 accept.rate <- 0.43 out <- lfMsNMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, inits = inits.list, family = 'Poisson', n.factors = n.factors, n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, tuning = tuning, priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(400) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep<- sample(2:4, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0.2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2, -0.1) tau.sq.alpha <- c(0.2, 0.3, 1) p.det <- length(alpha.mean) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } family <- 'Poisson' n.factors <- 3 dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, sp = FALSE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values X.0 <- dat$X[pred.indx, ] mu.0 <- dat$psi[, pred.indx] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Occupancy initial values 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)) # Initial values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 1, tau.sq.alpha = 1, N = apply(y, c(1, 2), max, na.rm = TRUE)) # Tuning values tuning <- list(beta = 0.3, alpha = 0.3, lambda = 0.5, w = 0.5) n.batch <- 4 batch.length <- 25 accept.rate <- 0.43 out <- lfMsNMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, inits = inits.list, family = 'Poisson', n.factors = n.factors, n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, tuning = tuning, priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'msAbund'.
## S3 method for class 'msAbund' predict(object, X.0, ignore.RE = FALSE, z.0.samples, ...)
## S3 method for class 'msAbund' predict(object, X.0, ignore.RE = FALSE, z.0.samples, ...)
object |
an object of class msAbund |
X.0 |
the design matrix of covariates at the prediction locations. This can be either a two-dimensional matrix with rows corresponding to sites and columns corresponding to covariates, or can be a three-dimensional array, with dimensions corresponding to site, replicate, and covariate, respectively. Note that the first covariate should consist of all 1s for the intercept if an intercept is included in the model. If random effects are included in the the model, the levels of the random effects at the new locations/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data in |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a three-dimensional array with dimensions corresponding to MCMC samples, species, and prediction locations. The array contains the full posterior samples of the predicted binary portion of a Gaussian zero-inflated model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of each species at the location. When using |
... |
currently no additional arguments |
A list object of class predict.msAbund
. The list consists of:
mu.0.samples |
a three or four-dimensional object of posterior predictive samples for the
expected abundance values with dimensions corresponding to posterior predictive
sample, species, site, and replicate. Note if an offset was used when fitting the model with
|
y.0.samples |
a three or four-dimensional object of posterior predictive samples for the
abundance values with dimensions corresponding to posterior predictive sample, species, site,
and replicate. These will be in the same units as |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, , drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Prediction values y.0 <- dat$y[, pred.indx, , drop = FALSE] X.0 <- dat$X[pred.indx, , , drop = FALSE] # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2]) data.list <- list(y = y, covs = covs) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- msAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0) str(out.pred)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- FALSE kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, , drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Prediction values y.0 <- dat$y[, pred.indx, , drop = FALSE] X.0 <- dat$X[pred.indx, , , drop = FALSE] # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2]) data.list <- list(y = y, covs = covs) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- msAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'msDS'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'msDS' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
## S3 method for class 'msDS' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
object |
an object of class msDS |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
ignore.RE |
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect. |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
... |
currently no additional arguments |
A list object of class predict.msDS
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a three-dimensional array of posterior predictive samples for the
expected abundance values, or expected abundance values per unit area (i.e., density)
values when an offset was used when fitting the model with |
N.0.samples |
a three-dimensional array of posterior predictive samples for the
latent abundance values. These will be in the same units as |
When type = 'detection'
, the list consists of:
sigma.0.samples |
a three-dimensional array of posterior predictive samples for sigma (the parameter controlling detection probability). |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- FALSE dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Occupancy covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , drop = FALSE] X.p.0 <- dat$X.p[pred.indx, , drop = FALSE] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- msDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0) str(out.pred)
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- FALSE dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Occupancy covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , drop = FALSE] X.p.0 <- dat$X.p[pred.indx, , drop = FALSE] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- msDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'msNMix'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'msNMix' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
## S3 method for class 'msNMix' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
object |
an object of class msNMix |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
ignore.RE |
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect. |
... |
currently no additional arguments |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
A list object of class predict.msNMix
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a three-dimensional array of posterior predictive samples for the
expected abundance values. Note these will be per unit area if an offset was used when
fitting the model with |
N.0.samples |
a three-dimensional array of posterior predictive samples for the
latent abundance values. These will be in the same units as |
When type = 'detection'
, the list consists of:
p.0.samples |
a three-dimensional array of posterior predictive samples for the detection probability values. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(400) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep<- sample(2:4, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0.2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2, -0.1) tau.sq.alpha <- c(0.2, 0.3, 1) p.det <- length(alpha.mean) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } family <- 'Poisson' dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, sp = FALSE, family = 'Poisson') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Prediction values X.0 <- dat$X[pred.indx, ] mu.0 <- dat$psi[, pred.indx] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Occupancy initial values 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)) # Initial values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 1, tau.sq.alpha = 1, N = apply(y, c(1, 2), max, na.rm = TRUE)) # Tuning values tuning <- list(beta = 0.3, alpha = 0.3) n.batch <- 4 batch.length <- 25 accept.rate <- 0.43 out <- msNMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, inits = inits.list, family = 'Poisson', n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, tuning = tuning, priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ out.pred <- predict(out, X.0) str(out.pred)
set.seed(400) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep<- sample(2:4, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0.2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2, -0.1) tau.sq.alpha <- c(0.2, 0.3, 1) p.det <- length(alpha.mean) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } family <- 'Poisson' dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, sp = FALSE, family = 'Poisson') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Prediction values X.0 <- dat$X[pred.indx, ] mu.0 <- dat$psi[, pred.indx] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Occupancy initial values 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)) # Initial values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 1, tau.sq.alpha = 1, N = apply(y, c(1, 2), max, na.rm = TRUE)) # Tuning values tuning <- list(beta = 0.3, alpha = 0.3) n.batch <- 4 batch.length <- 25 accept.rate <- 0.43 out <- msNMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, inits = inits.list, family = 'Poisson', n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, tuning = tuning, priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ out.pred <- predict(out, X.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'NMix'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'NMix' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
## S3 method for class 'NMix' predict(object, X.0, ignore.RE = FALSE, type = 'abundance', ...)
object |
an object of class NMix |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
ignore.RE |
logical value that specifies whether or not to remove random abundance (or detection if |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict latent abundance and expected abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
... |
currently no additional arguments |
A list object of class predict.NMix
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a |
N.0.samples |
a |
When type = 'detection'
, the list consists of:
p.0.samples |
a |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
set.seed(100) # Simulate Data ----------------------------------------------------------- J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- sample(2:4, J, replace = TRUE) beta <- c(0.5, 2) p.abund <- length(beta) alpha <- c(0, 1) p.det <- length(alpha) dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, sp = FALSE) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov = X.p[, , 2]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0.001, 10)) # Initial values inits.list <- list(alpha = rep(0, p.det), beta = rep(0, p.abund), kappa = 0.5, N = apply(y, 1, max, na.rm = TRUE)) n.batch <- 10 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- NMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov, data = data.list, inits = inits.list, n.batch = n.batch, batch.length = batch.length, family = 'Poisson', priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov') out.pred <- predict(out, X.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
set.seed(100) # Simulate Data ----------------------------------------------------------- J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- sample(2:4, J, replace = TRUE) beta <- c(0.5, 2) p.abund <- length(beta) alpha <- c(0, 1) p.det <- length(alpha) dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, sp = FALSE) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov = X.p[, , 2]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0.001, 10)) # Initial values inits.list <- list(alpha = rep(0, p.det), beta = rep(0, p.abund), kappa = 0.5, N = apply(y, 1, max, na.rm = TRUE)) n.batch <- 10 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- NMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov, data = data.list, inits = inits.list, n.batch = n.batch, batch.length = batch.length, family = 'Poisson', priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov') out.pred <- predict(out, X.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'sfMsAbund'.
## S3 method for class 'sfMsAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, include.sp = TRUE, ...)
## S3 method for class 'sfMsAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, include.sp = TRUE, ...)
object |
an object of class sfMsAbund |
X.0 |
the design matrix of covariates at the prediction locations. This can be either a two-dimensional matrix with rows corresponding to sites and columns corresponding to covariates, or can be a three-dimensional array, with dimensions corresponding to site, replicate, and covariate, respectively. Note that the first covariate should consist of all 1s for the intercept if an intercept is included in the model. If random effects are included in the the model, the levels of the random effects at the new locations/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data in |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a three-dimensional array with dimensions corresponding to MCMC samples, species, and prediction locations. The array contains the full posterior samples of the predicted binary portion of a zero-inflated Gaussian model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of each species at the location. When using |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.sfMsAbund
. The list consists of:
mu.0.samples |
a three or four-dimensional object of posterior predictive samples for the
expected abundance values with dimensions corresponding to posterior predictive
sample, species, site, and replicate. Note if an offset was used when fitting the model
with |
y.0.samples |
a three or four-dimensional object of posterior predictive samples for the
abundance values with dimensions corresponding to posterior predictive sample, species, site,
and replicate. These will be in the same units as |
w.0.samples |
a three-dimensional array of posterior predictive samples for the latent factors. Array dimensions correspond to MCMC sample, latent factor, and site. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE kappa <- runif(n.sp, 0.1, 1) factor.model <- TRUE n.factors <- 3 cov.model <- 'spherical' phi <- runif(n.factors, 3 / 1, 3 / .1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, n.factors = n.factors, phi = phi, cov.model = cov.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, , drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values y.0 <- dat$y[, pred.indx, , drop = FALSE] X.0 <- dat$X[pred.indx, , , drop = FALSE] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, phi = 3 / .5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, lambda = 0.5, w = 0.5, phi = 1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- sfMsAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = 1, verbose = TRUE, n.neighbors = 5, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE kappa <- runif(n.sp, 0.1, 1) factor.model <- TRUE n.factors <- 3 cov.model <- 'spherical' phi <- runif(n.factors, 3 / 1, 3 / .1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, n.factors = n.factors, phi = phi, cov.model = cov.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, , drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values y.0 <- dat$y[, pred.indx, , drop = FALSE] X.0 <- dat$X[pred.indx, , , drop = FALSE] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, phi = 3 / .5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, lambda = 0.5, w = 0.5, phi = 1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- sfMsAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = 1, verbose = TRUE, n.neighbors = 5, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'sfMsDS'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'sfMsDS' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
## S3 method for class 'sfMsDS' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
object |
an object of class sfMsDS |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect. |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.sfMsDS
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a three-dimensional array of posterior predictive samples for the
expected abundance values, or expected abundance values per unit area (i.e., density)
values when an offset was used when fitting the model with |
N.0.samples |
a three-dimensional array of posterior predictive samples for the
latent abundance values. These will be in the same units as |
w.0.samples |
a three-dimensional array of posterior predictive samples for the spatial latent factors. |
When type = 'detection'
, the list consists of:
sigma.0.samples |
a three-dimensional array of posterior predictive samples for sigma (the parameter controlling detection probability). |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected]
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- TRUE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .2) cov.model <- 'exponential' dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Occupancy covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , drop = FALSE] X.p.0 <- dat$X.p[pred.indx, , drop = FALSE] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), phi.unif = list(3 / 1, 3 / .1), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1, phi = 3 / .5) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1, phi = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- sfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- TRUE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .2) cov.model <- 'exponential' dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Occupancy covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , drop = FALSE] X.p.0 <- dat$X.p[pred.indx, , drop = FALSE] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), phi.unif = list(3 / 1, 3 / .1), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1, phi = 3 / .5) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1, phi = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- sfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'sfMsNMix'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'sfMsNMix' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
## S3 method for class 'sfMsNMix' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
object |
an object of class sfMsNMix |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect. |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict expected abundance and latent abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.sfMsNMix
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a three-dimensional array of posterior predictive samples for the
expected abundance values. Note these will be per unit area if an offset was used when
fitting the model with |
N.0.samples |
a three-dimensional array of posterior predictive samples for the
latent abundance values. These will be in the same units as |
w.0.samples |
a three-dimensional array of posterior predictive samples for the spatial latent factors. |
When type = 'detection'
, the list consists of:
p.0.samples |
a three-dimensional array of posterior predictive samples for the detection probability values. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
set.seed(400) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep<- sample(2:4, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0.2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2, -0.1) tau.sq.alpha <- c(0.2, 0.3, 1) p.det <- length(alpha.mean) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } family <- 'Poisson' n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .1) dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, sp = TRUE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors, phi = phi, cov.model = 'exponential') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values X.0 <- dat$X[pred.indx, ] mu.0 <- dat$psi[, pred.indx] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Initial values 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), phi.unif = list(a = 3 / 1, 3 / .1)) # Initial values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, phi = 3 / .5, tau.sq.beta = 1, tau.sq.alpha = 1, N = apply(y, c(1, 2), max, na.rm = TRUE)) # Tuning values tuning <- list(beta = 0.3, alpha = 0.3, lambda = 0.5, w = 0.5, phi = 1.5) n.batch <- 4 batch.length <- 25 accept.rate <- 0.43 out <- sfMsNMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, inits = inits.list, family = 'Poisson', n.factors = n.factors, n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, cov.model = 'exponential', n.neighbors = 5, tuning = tuning, priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(400) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep<- sample(2:4, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0.2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2, -0.1) tau.sq.alpha <- c(0.2, 0.3, 1) p.det <- length(alpha.mean) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } family <- 'Poisson' n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .1) dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, sp = TRUE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors, phi = phi, cov.model = 'exponential') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values X.0 <- dat$X[pred.indx, ] mu.0 <- dat$psi[, pred.indx] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list abund.covs <- X[, 2, drop = FALSE] colnames(abund.covs) <- c('abund.cov') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Initial values 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), phi.unif = list(a = 3 / 1, 3 / .1)) # Initial values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, phi = 3 / .5, tau.sq.beta = 1, tau.sq.alpha = 1, N = apply(y, c(1, 2), max, na.rm = TRUE)) # Tuning values tuning <- list(beta = 0.3, alpha = 0.3, lambda = 0.5, w = 0.5, phi = 1.5) n.batch <- 4 batch.length <- 25 accept.rate <- 0.43 out <- sfMsNMix(abund.formula = ~ abund.cov, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, inits = inits.list, family = 'Poisson', n.factors = n.factors, n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, cov.model = 'exponential', n.neighbors = 5, tuning = tuning, priors = prior.list, n.omp.threads = 1, verbose = TRUE, n.report = 1) summary(out, level = 'community') # Predict at new locations ------------------------------------------------ out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'spAbund'.
## S3 method for class 'spAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, include.sp = TRUE, ...)
## S3 method for class 'spAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, include.sp = TRUE, ...)
object |
an object of class spAbund |
X.0 |
the design matrix of covariates at the prediction locations. This should be a three-dimensional array, with dimensions corresponding to site, replicate, and covariate, respectively. Note that the first covariate should consist of all 1s for the intercept if an intercept is included in the model. If random effects are included in the spAbundance portion of the model, the levels of the random effects at the new locations/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data in |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a matrix with rows corresponding to MCMC samples and columns corresponding to prediction locations containing the full posterior samples of the predicted binary portion of a zero-inflated Gaussian model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of the species at the location. When using |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.spAbund
. The list consists of:
mu.0.samples |
a three-dimensional object of posterior predictive samples for the
expected spAbundance values with dimensions corresponding to posterior predictive
sample, site, and replicate. Note if an offset was used when fitting the model
with |
y.0.samples |
a three-dimensional object of posterior predictive samples for the
spAbundance values with dimensions corresponding to posterior predictive sample, site,
and replicate. These will be in the same units as |
w.0.samples |
a |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(1, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.spAbund <- length(beta) mu.RE <- list() kappa <- 0.5 sp <- TRUE sigma.sq <- 0.5 phi <- 3 / .5 family <- 'NB' cov.model = 'exponential' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB', sigma.sq = sigma.sq, phi = phi, cov.model = cov.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Prediction covariates X.0 <- dat$X[pred.indx, , ] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) abund.covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4]) data.list <- list(y = y, covs = abund.covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- spAbund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.neighbors = 5, cov.model = cov.model, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(1, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.spAbund <- length(beta) mu.RE <- list() kappa <- 0.5 sp <- TRUE sigma.sq <- 0.5 phi <- 3 / .5 family <- 'NB' cov.model = 'exponential' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB', sigma.sq = sigma.sq, phi = phi, cov.model = cov.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, , , drop = FALSE] # Prediction covariates X.0 <- dat$X[pred.indx, , ] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) abund.covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4]) data.list <- list(y = y, covs = abund.covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- spAbund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.neighbors = 5, cov.model = cov.model, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'spDS'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'spDS' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
## S3 method for class 'spDS' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
object |
an object of class spDS |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
logical value that specifies whether or not to remove random abundance (or detection if |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict latent abundance and expected abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.spDS
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a |
N.0.samples |
a |
w.0.samples |
a |
When type = 'detection'
, the list consists of:
sigma.0.samples |
a |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- TRUE phi <- 3 / .5 sigma.sq <- 0.8 cov.model <- 'exponential' family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, family = family, offset = offset, transect = transect, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, ] dist.breaks <- dat$dist.breaks coords <- dat$coords[-pred.indx, ] coords.0 <- dat$coords[pred.indx, ] covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100), phi.unif = c(3 / 1, 3 / .1), sigma.sq.ig = c(2, 1)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1, phi = 3 / .5, sigma.sq = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2, phi = 1, w = 1) out <- spDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- TRUE phi <- 3 / .5 sigma.sq <- 0.8 cov.model <- 'exponential' family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, family = family, offset = offset, transect = transect, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, ] dist.breaks <- dat$dist.breaks coords <- dat$coords[-pred.indx, ] coords.0 <- dat$coords[pred.indx, ] covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100), phi.unif = c(3 / 1, 3 / .1), sigma.sq.ig = c(2, 1)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1, phi = 3 / .5, sigma.sq = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2, phi = 1, w = 1) out <- spDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3') out.pred <- predict(out, X.0, coords.0) mu.0.quants <- apply(out.pred$mu.0.samples, 2, quantile, c(0.025, 0.5, 0.975)) plot(dat$mu[pred.indx], mu.0.quants[2, ], pch = 19, xlab = 'True', ylab = 'Fitted', ylim = c(min(mu.0.quants), max(mu.0.quants))) segments(dat$mu[pred.indx], mu.0.quants[1, ], dat$mu[pred.indx], mu.0.quants[3, ]) lines(dat$mu[pred.indx], dat$mu[pred.indx])
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'spNMix'. Prediction is possible for both the latent abundance state as well as detection.
## S3 method for class 'spNMix' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
## S3 method for class 'spNMix' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, type = 'abundance', include.sp = TRUE, ...)
object |
an object of class spNMix |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the abundance (or detection if |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
logical value that specifies whether or not to remove random abundance (or detection if |
type |
a quoted keyword indicating what type of prediction to produce. Valid keywords are 'abundance' to predict latent abundance and expected abundance values (this is the default), or 'detection' to predict detection probability given new values of detection covariates. |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.spNMix
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a |
N.0.samples |
a |
w.0.samples |
a |
When type = 'detection'
, the list consists of:
p.0.samples |
a |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
set.seed(200) # Simulate Data ----------------------------------------------------------- J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0.5, 1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- runif(1, 3 / 1, 3 / .1) sigma.sq <- runif(1, 0.2, 1.5) kappa <- 0.5 sp <- TRUE cov.model <- 'exponential' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .5), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) mu.0 <- dat$mu[pred.indx] w.0 <- dat$w[pred.indx] abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = alpha, beta = beta, kappa = kappa, phi = 3 / 0.5, sigma.sq = 1, N = apply(y, 1, max, na.rm = TRUE)) # Tuning values tuning.list <- list(phi = 0.5, kappa = 0.5, beta = 0.1, alpha = 0.1, w = 0.1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- spNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, NNGP = TRUE, cov.model = 'spherical', n.neighbors = 10, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov') out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(200) # Simulate Data ----------------------------------------------------------- J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0.5, 1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- runif(1, 3 / 1, 3 / .1) sigma.sq <- runif(1, 0.2, 1.5) kappa <- 0.5 sp <- TRUE cov.model <- 'exponential' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .5), replace = FALSE) y <- dat$y[-pred.indx, ] # Abundance covariates X <- dat$X[-pred.indx, ] # Prediction covariates X.0 <- dat$X[pred.indx, ] # Detection covariates X.p <- dat$X.p[-pred.indx, , ] coords <- as.matrix(dat$coords[-pred.indx, ]) coords.0 <- as.matrix(dat$coords[pred.indx, ]) mu.0 <- dat$mu[pred.indx] w.0 <- dat$w[pred.indx] abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = alpha, beta = beta, kappa = kappa, phi = 3 / 0.5, sigma.sq = 1, N = apply(y, 1, max, na.rm = TRUE)) # Tuning values tuning.list <- list(phi = 0.5, kappa = 0.5, beta = 0.1, alpha = 0.1, w = 0.1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- spNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, NNGP = TRUE, cov.model = 'spherical', n.neighbors = 10, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out) # Predict at new locations ------------------------------------------------ colnames(X.0) <- c('intercept', 'abund.cov') out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'svcAbund'.
## S3 method for class 'svcAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, include.sp = TRUE, ...)
## S3 method for class 'svcAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, include.sp = TRUE, ...)
object |
an object of class svcAbund |
X.0 |
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the model, the levels of the random effects at the new locations should be included as a column in the design matrix. The ordering of the levels should match the ordering used to fit the data in |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a matrix with rows corresponding to MCMC samples and columns corresponding to prediction locations containing the full posterior samples of the predicted binary portion of a zero-inflated Gaussian model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of the species at the location. When using |
include.sp |
a logical value used to indicate whether spatial random effects should be included in the predictions. By default, this is set to |
... |
currently no additional arguments |
A list object of class predict.svcAbund
. When type = 'abundance'
, the list consists of:
mu.0.samples |
a |
y.0.samples |
a |
w.0.samples |
a three-dimensional array of posterior predictive samples for the spatially-varying coefficients, with dimensions corresponding to MCMC iteration, coefficient, and site. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected],
set.seed(1000) # Sites J.x <- 10 J.y <- 10 J <- J.x * J.y # Occurrence -------------------------- beta <- c(10, 0.5, -0.2, 0.75) p <- length(beta) mu.RE <- list() # Spatial parameters ------------------ sp <- TRUE svc.cols <- c(1, 2) p.svc <- length(svc.cols) cov.model <- "exponential" sigma.sq <- runif(p.svc, 0.4, 4) phi <- runif(p.svc, 3/1, 3/0.7) tau.sq <- 2 # Get all the data dat <- simAbund(J.x = J.x, J.y = J.y, beta = beta, tau.sq = tau.sq, mu.RE = mu.RE, sp = sp, svc.cols = svc.cols, family = 'Gaussian', cov.model = cov.model, sigma.sq = sigma.sq, phi = phi) # Prep the data for spAbundance ------------------------------------------- y <- dat$y X <- dat$X coords <- dat$coords # Subset data for prediction if desired pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y.0 <- y[pred.indx, drop = FALSE] X.0 <- X[pred.indx, , drop = FALSE] coords.0 <- coords[pred.indx, ] y <- y[-pred.indx, drop = FALSE] X <- X[-pred.indx, , drop = FALSE] coords <- coords[-pred.indx, ] # Package all data into a list covs <- cbind(X) colnames(covs) <- c('int', 'cov.1', 'cov.2', 'cov.3') # Data list bundle data.list <- list(y = y, covs = covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 1000), sigma.sq.ig = list(a = 2, b = 1), tau.sq = c(2, 1), sigma.sq.mu.ig = list(a = 2, b = 1), phi.unif = list(a = 3 / 1, b = 3 / 0.1)) # Starting values inits.list <- list(beta = 0, alpha = 0, sigma.sq = 1, phi = phi, tau.sq = 2, sigma.sq.mu = 0.5) # Tuning tuning.list <- list(phi = 1) n.batch <- 10 batch.length <- 25 n.burn <- 100 n.thin <- 1 n.chains <- 3 out <- svcAbund(formula = ~ cov.1 + cov.2 + cov.3, svc.cols = svc.cols, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, family = 'Gaussian', cov.model = "exponential", tuning = tuning.list, n.omp.threads = 1, verbose = TRUE, NNGP = TRUE, n.neighbors = 5, n.report = 25, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new values --------------------------------------------------- out.pred <- predict(out, X.0, coords.0) mu.0.means <- apply(out.pred$mu.0.samples, 2, mean) mu.0 <- dat$mu[pred.indx] plot(mu.0, mu.0.means, pch = 19) abline(0, 1)
set.seed(1000) # Sites J.x <- 10 J.y <- 10 J <- J.x * J.y # Occurrence -------------------------- beta <- c(10, 0.5, -0.2, 0.75) p <- length(beta) mu.RE <- list() # Spatial parameters ------------------ sp <- TRUE svc.cols <- c(1, 2) p.svc <- length(svc.cols) cov.model <- "exponential" sigma.sq <- runif(p.svc, 0.4, 4) phi <- runif(p.svc, 3/1, 3/0.7) tau.sq <- 2 # Get all the data dat <- simAbund(J.x = J.x, J.y = J.y, beta = beta, tau.sq = tau.sq, mu.RE = mu.RE, sp = sp, svc.cols = svc.cols, family = 'Gaussian', cov.model = cov.model, sigma.sq = sigma.sq, phi = phi) # Prep the data for spAbundance ------------------------------------------- y <- dat$y X <- dat$X coords <- dat$coords # Subset data for prediction if desired pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y.0 <- y[pred.indx, drop = FALSE] X.0 <- X[pred.indx, , drop = FALSE] coords.0 <- coords[pred.indx, ] y <- y[-pred.indx, drop = FALSE] X <- X[-pred.indx, , drop = FALSE] coords <- coords[-pred.indx, ] # Package all data into a list covs <- cbind(X) colnames(covs) <- c('int', 'cov.1', 'cov.2', 'cov.3') # Data list bundle data.list <- list(y = y, covs = covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 1000), sigma.sq.ig = list(a = 2, b = 1), tau.sq = c(2, 1), sigma.sq.mu.ig = list(a = 2, b = 1), phi.unif = list(a = 3 / 1, b = 3 / 0.1)) # Starting values inits.list <- list(beta = 0, alpha = 0, sigma.sq = 1, phi = phi, tau.sq = 2, sigma.sq.mu = 0.5) # Tuning tuning.list <- list(phi = 1) n.batch <- 10 batch.length <- 25 n.burn <- 100 n.thin <- 1 n.chains <- 3 out <- svcAbund(formula = ~ cov.1 + cov.2 + cov.3, svc.cols = svc.cols, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, family = 'Gaussian', cov.model = "exponential", tuning = tuning.list, n.omp.threads = 1, verbose = TRUE, NNGP = TRUE, n.neighbors = 5, n.report = 25, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new values --------------------------------------------------- out.pred <- predict(out, X.0, coords.0) mu.0.means <- apply(out.pred$mu.0.samples, 2, mean) mu.0 <- dat$mu[pred.indx] plot(mu.0, mu.0.means, pch = 19) abline(0, 1)
The function predict
collects posterior predictive samples for a set of new locations given an object of class 'svcMsAbund'.
## S3 method for class 'svcMsAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, ...)
## S3 method for class 'svcMsAbund' predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, n.report = 100, ignore.RE = FALSE, z.0.samples, ...)
object |
an object of class svcMsAbund |
X.0 |
the design matrix of covariates at the prediction locations. This can be either a two-dimensional matrix with rows corresponding to sites and columns corresponding to covariates, or can be a three-dimensional array, with dimensions corresponding to site, replicate, and covariate, respectively. Note that the first covariate should consist of all 1s for the intercept if an intercept is included in the model. If random effects are included in the the model, the levels of the random effects at the new locations/time periods should be included as an element of the three-dimensional array. The ordering of the levels should match the ordering used to fit the data in |
coords.0 |
the spatial coordinates corresponding to |
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
|
verbose |
if |
n.report |
the interval to report sampling progress. |
ignore.RE |
logical value that specifies whether or not to remove unstructured random effects from the subsequent predictions. If |
z.0.samples |
a three-dimensional array with dimensions corresponding to MCMC samples, species, and prediction locations. The array contains the full posterior samples of the predicted binary portion of a zero-inflated Gaussian model. In the context of abundance models, this typically corresponds to estimates of the presence or absence of each species at the location. When using |
... |
currently no additional arguments |
A list object of class predict.svcMsAbund
. The list consists of:
mu.0.samples |
a three or four-dimensional object of posterior predictive samples for the expected abundance values with dimensions corresponding to posterior predictive sample, species, site, and replicate. |
y.0.samples |
a three or four-dimensional object of posterior predictive samples for the abundance values with dimensions corresponding to posterior predictive sample, species, site, and replicate. |
w.0.samples |
a four-dimensional array of posterior predictive samples for the spatial factors for each spatially-varying coefficient. Dimensions correspond to MCMC sample, spatial factor, site, and spatially varying coefficient. |
The return object will include additional objects used for standard extractor functions.
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random effect corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- rep(1, J) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE factor.model <- TRUE n.factors <- 2 svc.cols <- c(1, 2) cov.model <- 'spherical' tau.sq <- runif(n.sp, 0.1, 2) phi <- runif(n.factors * length(svc.cols), 3 / 1, 3 / .1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, family = 'Gaussian', tau.sq = tau.sq, factor.model = factor.model, n.factors = n.factors, phi = phi, cov.model = cov.model, svc.cols = svc.cols) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , drop = FALSE] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values y.0 <- dat$y[, pred.indx, drop = FALSE] X.0 <- dat$X[pred.indx, , drop = FALSE] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list covs <- data.frame(abund.cov.1 = X[, 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), tau.sq.ig = list(a = .01, b = .01), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq = 1, phi = 3 / .5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, lambda = 0.5, w = 0.5, phi = 1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- svcMsAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, svc.cols = c(1, 2), n.factors = n.factors, cov.model = 'exponential', family = 'Gaussian', verbose = TRUE, n.neighbors = 5, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0, coords.0) str(out.pred)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- rep(1, J) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE factor.model <- TRUE n.factors <- 2 svc.cols <- c(1, 2) cov.model <- 'spherical' tau.sq <- runif(n.sp, 0.1, 2) phi <- runif(n.factors * length(svc.cols), 3 / 1, 3 / .1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, family = 'Gaussian', tau.sq = tau.sq, factor.model = factor.model, n.factors = n.factors, phi = phi, cov.model = cov.model, svc.cols = svc.cols) # Split into fitting and prediction data set pred.indx <- sample(1:J, round(J * .25), replace = FALSE) y <- dat$y[, -pred.indx, drop = FALSE] # Occupancy covariates X <- dat$X[-pred.indx, , drop = FALSE] # Coordinates coords <- dat$coords[-pred.indx, ] # Prediction values y.0 <- dat$y[, pred.indx, drop = FALSE] X.0 <- dat$X[pred.indx, , drop = FALSE] coords.0 <- dat$coords[pred.indx, ] # Package all data into a list covs <- data.frame(abund.cov.1 = X[, 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), tau.sq.ig = list(a = .01, b = .01), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq = 1, phi = 3 / .5, tau.sq.beta = 1) tuning.list <- list(kappa = 0.3, beta = 0.1, lambda = 0.5, w = 0.5, phi = 1) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- svcMsAbund(formula = ~ abund.cov.1, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, svc.cols = c(1, 2), n.factors = n.factors, cov.model = 'exponential', family = 'Gaussian', verbose = TRUE, n.neighbors = 5, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Predict at new locations out.pred <- predict(out, X.0, coords.0) str(out.pred)
The function sfMsAbund
fits multivariate spatial abundance GLMMs with species correlations (i.e., a spatially-explicit abundace-based joint species distribution model). We use a spatial factor modeling approach. Currently, models are implemented using a Nearest Neighbor Gaussian Process. Future development may allow for running the models using full Gaussian Processes.
sfMsAbund(formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
sfMsAbund(formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name, whose value
defines the initial tuning variance of the adaptive sampler for the given parameter.
Valid tags include |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
n.factors |
the number of factors to use in the spatial 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 the number of species in the modeled community. |
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. |
family |
the distribution to use for the abundance. Currently
supports |
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 |
verbose |
if |
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.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
save.fitted |
logical value indicating whether or not fitted values and likelihood values
should be saved in the resulting model object. If |
... |
currently no additional arguments |
An object of class sfMsAbund
that is a list comprised of:
beta.comm.samples |
a |
tau.sq.beta.samples |
a |
beta.samples |
a |
kappa.samples |
a |
tau.sq.samples |
a |
theta.samples |
a |
lambda.samples |
a |
y.rep.samples |
a three or four-dimensional array of posterior samples for the fitted (replicate) values for each species with dimensions corresponding to MCMC sample, species, site, and replicate. |
mu.samples |
a three or four-dimensional array of posterior samples for the expected abundance values for each species with dimensions corresponding to MCMC samples, species, site, and replicate. |
w.samples |
a three-dimensional array of posterior samples for the latent effects for each latent factor. Array dimensions correspond to MCMC sample, latent factor, then site. |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
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.
Christensen, W. F., and Amemiya, Y. (2002). Latent variable analysis of multivariate spatial data. Journal of the American Statistical Association, 97(457), 302-317.
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE n.factors <- 2 factor.model <- TRUE phi <- runif(n.factors, 3/1, 3 / .1) kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, phi = phi, cov.model = 'exponential', n.factors = n.factors) y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1, phi = 3 / 0.5) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- sfMsAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) + (1 | abund.factor.2), data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, NNGP = TRUE, cov.model = 'exponential', n.neighbors = 5, n.factors = n.factors, batch.length = batch.length, n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE n.factors <- 2 factor.model <- TRUE phi <- runif(n.factors, 3/1, 3 / .1) kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, phi = phi, cov.model = 'exponential', n.factors = n.factors) y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords # Package all data into a list covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), kappa.unif = list(a = 0, b = 10), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, kappa = 0.5, tau.sq.beta = 1, phi = 3 / 0.5) # Small n.batch <- 2 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- sfMsAbund(formula = ~ abund.cov.1 + (1 | abund.factor.1) + (1 | abund.factor.2), data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, NNGP = TRUE, cov.model = 'exponential', n.neighbors = 5, n.factors = n.factors, batch.length = batch.length, n.omp.threads = 3, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Function for fitting spatial factor multi-species hierarchical distance sampling models.
sfMsDS(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
sfMsDS(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
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.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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
transect |
the type of transect. Currently supports line transects ( |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class sfMsDS
that is a list comprised of:
beta.comm.samples |
a |
alpha.comm.samples |
a |
tau.sq.beta.samples |
a |
tau.sq.alpha.samples |
a |
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
theta.samples |
a |
lambda.samples |
a |
w.samples |
a three-dimensional array of posterior samples for the latent effects for each spatial factor. Array dimensions correspond to MCMC sample, spatial factor, then site. |
N.samples |
a three-dimensional array of posterior samples for the latent abundance values for each species. Note that these values always represent transect-level abundance, even when an offset is supplied. Array dimensions correspond to MCMC sample, species, then site. |
mu.samples |
a three-dimensional array of posterior samples for
the latent expected abundance values for each species. When an offset
is supplied in the |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
y.rep.samples |
a four-dimensional array of fitted values. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
pi.samples |
a four-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, species, sites, and distance band. |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
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.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
Sollmann, R., Gardner, B., Williams, K. A., Gilbert, A. T., & Veit, R. R. (2016). A hierarchical distance sampling model to estimate abundance and covariate associations of species and communities. Methods in Ecology and Evolution, 7(5), 529-537.
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- TRUE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .2) cov.model <- 'exponential' dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) y <- dat$y X <- dat$X X.p <- dat$X.p coords <- dat$coords dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), phi.unif = list(3 / 1, 3 / .1), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1, phi = 3 / .5) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1, phi = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- sfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
set.seed(210) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- TRUE family <- 'Poisson' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- TRUE n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .2) cov.model <- 'exponential' dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model, n.factors = n.factors) y <- dat$y X <- dat$X X.p <- dat$X.p coords <- dat$coords dist.breaks <- dat$dist.breaks covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.comm.normal = list(mean = 0, var = 10), alpha.comm.normal = list(mean = 0, var = 10), kappa.unif = list(0, 100), phi.unif = list(3 / 1, 3 / .1), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1)) # Starting values inits.list <- list(alpha.comm = 0, beta.comm = 0, beta = 0, alpha = 0, kappa = 1, phi = 3 / .5) tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.8, lambda = 1, w = 1, phi = 0.8) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- sfMsDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, family = 'Poisson', det.func = 'halfnormal', transect = transect, tuning = tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, n.factors = n.factors, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
Function for fitting spatial multi-species N-mixture models with species correlations (i.e., an abundance-based spatially-explicit joint species distribution model with imperfect detection). We use Nearest Neighbor Gaussian Processes and a spatial factor modeling approach to achieve dimension reducition.
sfMsNMix(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
sfMsNMix(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
n.factors |
the number of factors to use in the spatial 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.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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class sfMsNMix
that is a list comprised of:
beta.comm.samples |
a |
alpha.comm.samples |
a |
tau.sq.beta.samples |
a |
tau.sq.alpha.samples |
a |
beta.samples |
a |
alpha.samples |
a |
lambda.samples |
a |
theta.samples |
a |
w.samples |
a three-dimensional array of posterior samples for the latent effects for each latent factor. |
kappa.samples |
a |
N.samples |
a three-dimensional array of posterior samples for the latent abundance values for each species. |
mu.samples |
a three-dimensional array of posterior samples for the latent expected abundance values for each species. |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
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 |
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()
.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
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.
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
Christensen, W. F., and Amemiya, Y. (2002). Latent variable analysis of multivariate spatial data. Journal of the American Statistical Association, 97(457), 302-317.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Yamaura, Y., Royle, J. A., Shimada, N., Asanuma, S., Sato, T., Taki, H., & Makino, S. I. (2012). Biodiversity of man-made open habitats in an underused country: a class of multispecies abundance models for count data. Biodiversity and Conservation, 21(6), 1365-1380.
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(5, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .2) dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors, sp = TRUE, phi = phi, cov.model = 'exponential') y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re coords <- dat$coords # Package all data into a list abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = as.data.frame(X.p[, , 2]), det.cov.2 = as.data.frame(X.p[, , 3])) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.comm.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1), phi.unif = list(a = 3 / 1, 3 / .1)) inits.list <- list(beta.comm = 0, alpha.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 0.5, tau.sq.alpha = 0.5, N = apply(y, c(1, 2), max, na.rm = TRUE)) tuning.list <- list(beta = 0.5, alpha = 0.5, lambda = 0.5, w = 0.5, phi = 1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- sfMsNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = n.factors, cov.model = 'exponential', n.neighbors = 5, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(5, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects # Abundance beta.mean <- c(0, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Detection alpha.mean <- c(0, 0.5, 0.8) tau.sq.alpha <- c(0.2, 1, 1.5) p.det <- length(alpha.mean) # Random effects mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } n.factors <- 3 phi <- runif(n.factors, 3 / 1, 3 / .2) dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, family = 'Poisson', factor.model = TRUE, n.factors = n.factors, sp = TRUE, phi = phi, cov.model = 'exponential') y <- dat$y X <- dat$X X.p <- dat$X.p X.re <- dat$X.re X.p.re <- dat$X.p.re coords <- dat$coords # Package all data into a list abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = as.data.frame(X.p[, , 2]), det.cov.2 = as.data.frame(X.p[, , 3])) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.comm.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = 0.1, b = 0.1), phi.unif = list(a = 3 / 1, 3 / .1)) inits.list <- list(beta.comm = 0, alpha.comm = 0, beta = 0, alpha = 0, tau.sq.beta = 0.5, tau.sq.alpha = 0.5, N = apply(y, c(1, 2), max, na.rm = TRUE)) tuning.list <- list(beta = 0.5, alpha = 0.5, lambda = 0.5, w = 0.5, phi = 1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- sfMsNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, batch.length = batch.length, n.omp.threads = 1, n.factors = n.factors, cov.model = 'exponential', n.neighbors = 5, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out, level = 'community')
The function simAbund
simulates univariate data without imperfect detection for simulation studies, power assessments, or function testing related to GLMMs. Data can be optionally simulated with a spatial Gaussian Process in the model. Non-spatial random effects can also be included in the model.
simAbund(J.x, J.y, n.rep, n.rep.max, beta, kappa, tau.sq, mu.RE = list(), offset = 1, sp = FALSE, svc.cols = 1, cov.model, sigma.sq, phi, nu, family = 'Poisson', z, trend = FALSE, x.positive = FALSE, ...)
simAbund(J.x, J.y, n.rep, n.rep.max, beta, kappa, tau.sq, mu.RE = list(), offset = 1, sp = FALSE, svc.cols = 1, cov.model, sigma.sq, phi, nu, family = 'Poisson', z, trend = FALSE, x.positive = FALSE, ...)
J.x |
a single numeric value indicating the number of sites to simulate count data along the horizontal axis. Total number of sites with simulated data is |
J.y |
a single numeric value indicating the number of sites to simulate count data along the vertical axis. Total number of sites with simulated data is |
n.rep |
a numeric vector of length |
n.rep.max |
a single numeric value indicating the maximum number of replicate surveys. This is an optional argument, with its default value set to |
beta |
a numeric vector containing the intercept and regression coefficient parameters for the abundance model. |
kappa |
a single numeric value containing the dispersion parameter for the abundance portion of the model. Only relevant when |
tau.sq |
a single numeric value containing the residual variance parameter of the Gaussian distribution. Only relevant when |
mu.RE |
a list used to specify the non-spatial random intercepts included in the model. The list must have two tags: |
sp |
a logical value indicating whether to simulate a spatially-explicit model with a Gaussian process. By default set to |
offset |
either a single numeric value, a vector of length |
svc.cols |
a vector indicating the variables whose effects will be
estimated as spatially-varying coefficients. |
cov.model |
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the abundance data. Supported covariance model key words are: |
sigma.sq |
a numeric value indicating the spatial variance parameter. Ignored when |
phi |
a numeric value indicating the spatial decay parameter. Ignored when |
nu |
a numeric value indicating the spatial smoothness parameter. Only used when |
family |
the distribution to use for the data. Currently
supports |
z |
a vector of length |
trend |
a logical value indicating whether a trend should be included when simulating relative abundance. If |
x.positive |
a logical value indicating whether the simulated covariates should be simulated as random standard normal covariates ( |
... |
currently no additional arguments |
A list comprised of:
X |
a three-dimensional numeric design array of covariates with dimensions corresponding to sites, replicates, and number of covariates (including an intercept) for the model. |
coords |
a |
w |
a matrix of the spatial random effects. Only used to simulate data when |
mu |
a |
y |
a |
X.re |
a numeric three-dimensional array containing the levels of any abundance random effect included in the model. Only relevant when abundance random effects are specified in |
beta.star |
a numeric vector that contains the simulated abundance random effects for each given level of the random effects included in the abundance model. Only relevant when abundance random effects are included in the model. |
Jeffrey W. Doser [email protected]
set.seed(401) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(30), sigma.sq.mu = c(1.3)) kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB')
set.seed(401) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(30), sigma.sq.mu = c(1.3)) kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB')
The function simDS
simulates single-species distance sampling data for simulation studies, power assessments, or function testing. Data can be optionally simulated with a spatial Gaussian Process in the abundance portion of the model. Non-spatial random effects can also be included in the detection or abundance portions of the distance sampling model.
simDS(J.x, J.y, n.bins, bin.width, beta, alpha, det.func, transect = 'line', kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', ...)
simDS(J.x, J.y, n.bins, bin.width, beta, alpha, det.func, transect = 'line', kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', ...)
J.x |
a single numeric value indicating the number of sites to simulate count data along the horizontal axis. Total number of sites with simulated data is |
J.y |
a single numeric value indicating the number of sites to simulate count data along the vertical axis. Total number of sites with simulated data is |
n.bins |
a single numeric value indicating the number of distance bins from which to generate data. |
bin.width |
a vector of length |
beta |
a numeric vector containing the intercept and regression coefficient parameters for the abundance portion of the single-species distance sampling model. |
alpha |
a numeric vector containing the intercept and regression coefficient parameters for the detection portion of the single-species distance sampling model. |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
transect |
the type of transect. Currently supports line transects ( |
kappa |
a single numeric value containing the dispersion parameter for the abundance portion of the hierarchical distance sampling model. Only relevant when |
mu.RE |
a list used to specify the non-spatial random intercepts included in the abundance portion of the model. The list must have two tags: |
p.RE |
a list used to specify the non-spatial random intercepts included in the detection portion of the model. The list must have two tags: |
offset |
either a single numeric value or a vector of length |
sp |
a logical value indicating whether to simulate a spatially-explicit HDS model with a Gaussian process. By default set to |
cov.model |
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the latent abundance values. Supported covariance model key words are: |
sigma.sq |
a numeric value indicating the spatial variance parameter. Ignored when |
phi |
a numeric value indicating the spatial decay parameter. Ignored when |
nu |
a numeric value indicating the spatial smoothness parameter. Only used when |
family |
the distribution to use for the latent abundance process. Currently
supports |
... |
currently no additional arguments |
A list comprised of:
X |
a |
X.p |
a |
coords |
a |
w |
a |
mu |
a |
N |
a length |
p |
a length J vector of the detection probabilities at each site. |
pi.full |
a |
y |
a |
X.p.re |
a numeric matrix containing the levels of any detection random effect included in the model. Only relevant when detection random effects are specified in |
X.re |
a numeric matrix containing the levels of any abundance random effect included in the model. Only relevant when abundance random effects are specified in |
alpha.star |
a numeric vector that contains the simulated detection random effects for each given level of the random effects included in the detection model. Only relevant when detection random effects are included in the model. |
beta.star |
a numeric vector that contains the simulated abundance random effects for each given level of the random effects included in the HDS model. Only relevant when abundance random effects are included in the model. |
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
set.seed(110) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- FALSE family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect)
set.seed(110) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- FALSE family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect)
The function simMsAbund
simulates multivariate data without imperfect detection for simulation studies, power assessments, or function testing related to GLMMs. Data can be optionally simulated with a spatial Gaussian Process in the model, as well as an option to allow for species correlations using a factor modeling approach. Non-spatial random effects can also be included in the abundance portions of the model.
simMsAbund(J.x, J.y, n.rep, n.rep.max, n.sp, beta, kappa, tau.sq, mu.RE = list(), offset = 1, sp = FALSE, cov.model, svc.cols = 1, sigma.sq, phi, nu, family = 'Poisson', factor.model = FALSE, n.factors, z, ...)
simMsAbund(J.x, J.y, n.rep, n.rep.max, n.sp, beta, kappa, tau.sq, mu.RE = list(), offset = 1, sp = FALSE, cov.model, svc.cols = 1, sigma.sq, phi, nu, family = 'Poisson', factor.model = FALSE, n.factors, z, ...)
J.x |
a single numeric value indicating the number of sites to simulate count data along the horizontal axis. Total number of sites with simulated data is |
J.y |
a single numeric value indicating the number of sites to simulate count data along the vertical axis. Total number of sites with simulated data is |
n.rep |
a numeric vector of length |
n.rep.max |
a single numeric value indicating the maximum number of replicate surveys. This is an optional argument, with its default value set to |
n.sp |
a single numeric value indicating the number of species to simulate count data. |
beta |
a numeric matrix with |
kappa |
a numeric vector of length |
tau.sq |
a numeric vector of length |
mu.RE |
a list used to specify the non-spatial random intercepts included in the model. The list must have two tags: |
offset |
either a single numeric value, a vector of length |
sp |
a logical value indicating whether to simulate a spatially-explicit model with a Gaussian process. By default set to |
cov.model |
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the abundance values. Supported covariance model key words are: |
svc.cols |
a vector indicating the variables whose effects will be
estimated as spatially-varying coefficients. |
sigma.sq |
a numeric vector of length |
phi |
a numeric vector of length |
nu |
a numeric vector of length |
factor.model |
a logical value indicating whether to simulate data following a factor modeling approach that explicitly incoporates species correlations. If |
n.factors |
a single numeric value specifying the number of latent factors to use to simulate the data if |
family |
the distribution to use for the latent abundance process. Currently
supports |
z |
a matrix with |
... |
currently no additional arguments |
A list comprised of:
X |
a three-dimensional numeric design array of covariates with dimensions corresponding to sites, replicates, and number of covariates (including an intercept) for the model. |
coords |
a |
w |
a list of |
mu |
a |
y |
a |
X.re |
a numeric matrix containing the levels of any abundance random effect included in the model. Only relevant when abundance random effects are specified in |
beta.star |
a numeric matrix where each row contains the simulated abundance random effects for each given level of the random effects included in the abundance model. Only relevant when abundance random effects are included in the model. |
Jeffrey W. Doser [email protected]
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE n.factors <- 2 factor.model <- TRUE phi <- runif(n.factors, 3/1, 3 / .1) kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, phi = phi, cov.model = 'spherical', n.factors = n.factors)
set.seed(408) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, size = J, replace = TRUE) n.sp <- 6 # Community-level covariate effects beta.mean <- c(-2, 0.5) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2) # Random effects (two random intercepts) mu.RE <- list(levels = c(10, 15), sigma.sq.mu = c(0.43, 0.5)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE n.factors <- 2 factor.model <- TRUE phi <- runif(n.factors, 3/1, 3 / .1) kappa <- runif(n.sp, 0.1, 1) dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, kappa = kappa, family = 'NB', factor.model = factor.model, phi = phi, cov.model = 'spherical', n.factors = n.factors)
The function simMsDS
simulates multi-species distance sampling data for simulation studies, power assessments, or function testing. Data can be optionally simulated with a spatial Gaussian Process in the abundance portion of the model, as well as an option to allow for species correlations using a factor modeling approach. Non-spatial random effects can also be included in the detection or abundance portions of the model.
simMsDS(J.x, J.y, n.bins, bin.width, n.sp, beta, alpha, det.func, transect = 'line', kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', factor.model = FALSE, n.factors, ...)
simMsDS(J.x, J.y, n.bins, bin.width, n.sp, beta, alpha, det.func, transect = 'line', kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', factor.model = FALSE, n.factors, ...)
J.x |
a single numeric value indicating the number of sites to simulate count data along the horizontal axis. Total number of sites with simulated data is |
J.y |
a single numeric value indicating the number of sites to simulate count data along the vertical axis. Total number of sites with simulated data is |
n.bins |
a single numeric value indicating the number of distance bins from which to generate data. |
bin.width |
a vector of length |
n.sp |
a single numeric value indicating the number of species to simulate count data. |
beta |
a numeric matrix with |
alpha |
a numeric matrix with |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
transect |
the type of transect. Currently supports line transects ( |
kappa |
a numeric vector of length |
mu.RE |
a list used to specify the non-spatial random effects included in the abundance portion of the model. The list must have two tags: |
p.RE |
a list used to specify the non-spatial random effects included in the detection portion of the model. The list must have two tags: |
offset |
either a single numeric value or a vector of length |
sp |
a logical value indicating whether to simulate a spatially-explicit model with a Gaussian process. By default set to |
cov.model |
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the latent abundance values. Supported covariance model key words are: |
sigma.sq |
a numeric vector of length |
phi |
a numeric vector of length |
nu |
a numeric vector of length |
factor.model |
a logical value indicating whether to simulate data following a factor modeling approach that explicitly incoporates species correlations. If |
n.factors |
a single numeric value specifying the number of latent factors to use to simulate the data if |
family |
the distribution to use for the latent abundance process. Currently
supports |
... |
currently no additional arguments |
A list comprised of:
X |
a |
X.p |
a |
coords |
a |
w |
a |
mu |
a |
N |
a |
p |
a |
y |
a |
X.p.re |
a numeric matrix containing the levels of any detection random effect included in the model. Only relevant when detection random effects are specified in |
X.re |
a numeric matrix containing the levels of any abundance random effect included in the model. Only relevant when abundance random effects are specified in |
alpha.star |
a numeric matrix where each row contains the simulated detection random effects for each given level of the random effects included in the detection model. Only relevant when detection random effects are included in the model. |
beta.star |
a numeric matrix where each row contains the simulated abundance random effects for each given level of the random effects included in the abundance model. Only relevant when abundance random effects are included in the model. |
Jeffrey W. Doser [email protected]
J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'NB' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- FALSE dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model)
J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Number of species n.sp <- 5 # Community-level abundance coefficients beta.mean <- c(-1, 0.2, 0.3, -0.2) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 0.3, 0.5, 0.4) # Detection coefficients alpha.mean <- c(-1.0, -0.3) p.det <- length(alpha.mean) tau.sq.alpha <- c(0.1, 0.2) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } sp <- FALSE family <- 'NB' kappa <- runif(n.sp, 0.3, 3) offset <- pi * .8^2 transect <- 'line' factor.model <- FALSE dat <- simMsDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, n.sp = n.sp, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, nu = nu, family = family, offset = offset, transect = transect, factor.model = factor.model)
The function simMsNMix
simulates multi-species count data for simulation studies, power assessments, or function testing. Data can be optionally simulated with a spatial Gaussian Process in the abundance portion of the model, as well as an option to allow for species correlations using a factor modeling approach. Non-spatial random effects can also be included in the detection or abundance portions of the model.
simMsNMix(J.x, J.y, n.rep, n.rep.max, n.sp, beta, alpha, kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', factor.model = FALSE, n.factors, ...)
simMsNMix(J.x, J.y, n.rep, n.rep.max, n.sp, beta, alpha, kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', factor.model = FALSE, n.factors, ...)
J.x |
a single numeric value indicating the number of sites to simulate count data along the horizontal axis. Total number of sites with simulated data is |
J.y |
a single numeric value indicating the number of sites to simulate count data along the vertical axis. Total number of sites with simulated data is |
n.rep |
a numeric vector of length |
n.rep.max |
a single numeric value indicating the maximum number of replicate surveys. This is an optional argument, with its default value set to |
n.sp |
a single numeric value indicating the number of species to simulate count data. |
beta |
a numeric matrix with |
alpha |
a numeric matrix with |
kappa |
a numeric vector of length |
mu.RE |
a list used to specify the non-spatial random effects included in the abundance portion of the model. The list must have two tags: |
p.RE |
a list used to specify the non-spatial random effects included in the detection portion of the model. The list must have two tags: |
offset |
either a single numeric value or a vector of length |
sp |
a logical value indicating whether to simulate a spatially-explicit model with a Gaussian process. By default set to |
cov.model |
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the latent abundance values. Supported covariance model key words are: |
sigma.sq |
a numeric vector of length |
phi |
a numeric vector of length |
nu |
a numeric vector of length |
factor.model |
a logical value indicating whether to simulate data following a factor modeling approach that explicitly incoporates species correlations. If |
n.factors |
a single numeric value specifying the number of latent factors to use to simulate the data if |
family |
the distribution to use for the latent abundance process. Currently
supports |
... |
currently no additional arguments |
A list comprised of:
X |
a |
X.p |
a three-dimensional numeric array with dimensions corresponding to sites, repeat visits, and number of detection regression coefficients. This is the design matrix used for the detection portion of the N-mixture model. |
coords |
a |
w |
a |
mu |
a |
N |
a |
p |
a |
y |
a |
X.p.re |
a three-dimensional numeric array containing the levels of any detection random effect included in the model. Only relevant when detection random effects are specified in |
X.re |
a numeric matrix containing the levels of any abundance random effect included in the model. Only relevant when abundance random effects are specified in |
alpha.star |
a numeric matrix where each row contains the simulated detection random effects for each given level of the random effects included in the detection model. Only relevant when detection random effects are included in the model. |
beta.star |
a numeric matrix where each row contains the simulated abundance random effects for each given level of the random effects included in the abundance model. Only relevant when abundance random effects are included in the model. |
Jeffrey W. Doser [email protected],
J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(2:4, size = J, replace = TRUE) n.sp <- 10 # Community-level covariate effects # Abundance beta.mean <- c(0.2, -0.15) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2) tau.sq.alpha <- c(0.2, 0.3) p.det <- length(alpha.mean) mu.RE <- list(levels = c(10, 12), sigma.sq.mu = c(1.5, 0.3), beta.indx = list(1, 2)) p.RE <- list(levels = c(15, 10), sigma.sq.p = c(0.8, 0.5), alpha.indx = list(1, 2)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } factor.model <- TRUE n.factors <- 3 # Spatial parameters if desired phi <- runif(n.factors, 3/1, 3/.1) sp <- TRUE family <- 'Poisson' dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = TRUE, cov.model = 'exponential', phi = phi, factor.model = factor.model, n.factors = n.factors, family = family)
J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(2:4, size = J, replace = TRUE) n.sp <- 10 # Community-level covariate effects # Abundance beta.mean <- c(0.2, -0.15) p.abund <- length(beta.mean) tau.sq.beta <- c(0.6, 0.3) # Detection alpha.mean <- c(0.5, 0.2) tau.sq.alpha <- c(0.2, 0.3) p.det <- length(alpha.mean) mu.RE <- list(levels = c(10, 12), sigma.sq.mu = c(1.5, 0.3), beta.indx = list(1, 2)) p.RE <- list(levels = c(15, 10), sigma.sq.p = c(0.8, 0.5), alpha.indx = list(1, 2)) # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) alpha <- matrix(NA, nrow = n.sp, ncol = p.det) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } for (i in 1:p.det) { alpha[, i] <- rnorm(n.sp, alpha.mean[i], sqrt(tau.sq.alpha[i])) } factor.model <- TRUE n.factors <- 3 # Spatial parameters if desired phi <- runif(n.factors, 3/1, 3/.1) sp <- TRUE family <- 'Poisson' dat <- simMsNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, alpha = alpha, mu.RE = mu.RE, p.RE = p.RE, sp = TRUE, cov.model = 'exponential', phi = phi, factor.model = factor.model, n.factors = n.factors, family = family)
The function simNMix
simulates single-species count data for simulation studies, power assessments, or function testing. Data can be optionally simulated with a spatial Gaussian Process in the abundance portion of the model. Non-spatial random intercepts/slopes can also be included in the detection or abundance portions of the N-mixture model.
simNMix(J.x, J.y, n.rep, n.rep.max, beta, alpha, kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', ...)
simNMix(J.x, J.y, n.rep, n.rep.max, beta, alpha, kappa, mu.RE = list(), p.RE = list(), offset = 1, sp = FALSE, cov.model, sigma.sq, phi, nu, family = 'Poisson', ...)
J.x |
a single numeric value indicating the number of sites to simulate count data along the horizontal axis. Total number of sites with simulated data is |
J.y |
a single numeric value indicating the number of sites to simulate count data along the vertical axis. Total number of sites with simulated data is |
n.rep |
a numeric vector of length |
n.rep.max |
a single numeric value indicating the maximum number of replicate surveys. This is an optional argument, with its default value set to |
beta |
a numeric vector containing the intercept and regression coefficient parameters for the abundance portion of the single-species N-mixture model. |
alpha |
a numeric vector containing the intercept and regression coefficient parameters for the detection portion of the single-species N-mixture model. |
kappa |
a single numeric value containing the dispersion parameter for the abundance portion of the N-mixture model. Only relevant when |
mu.RE |
a list used to specify the non-spatial random effects included in the abundance portion of the model. The list must have two tags: |
p.RE |
a list used to specify the non-spatial random effects included in the detection portion of the model. The list must have two tags: |
offset |
either a single numeric value or a vector of length |
sp |
a logical value indicating whether to simulate a spatially-explicit N-mixture model with a Gaussian process. By default set to |
cov.model |
a quoted keyword that specifies the covariance function used to model the spatial dependence structure among the latent abundance values. Supported covariance model key words are: |
sigma.sq |
a numeric value indicating the spatial variance parameter. Ignored when |
phi |
a numeric value indicating the spatial decay parameter. Ignored when |
nu |
a numeric value indicating the spatial smoothness parameter. Only used when |
family |
the distribution to use for the latent abundance process. Currently
supports |
... |
currently no additional arguments |
A list comprised of:
X |
a |
X.p |
a three-dimensional numeric array with dimensions corresponding to sites, repeat visits, and number of detection regression coefficients. This is the design matrix used for the detection portion of the N-mixture model. |
coords |
a |
w |
a |
mu |
a |
N |
a length |
p |
a |
y |
a |
X.p.re |
a three-dimensional numeric array containing the levels of any detection random effect included in the model. Only relevant when detection random effects are specified in |
X.re |
a numeric matrix containing the levels of any abundance random effect included in the model. Only relevant when abundance random effects are specified in |
alpha.star |
a numeric vector that contains the simulated detection random effects for each given level of the random effects included in the detection model. Only relevant when detection random effects are included in the model. |
beta.star |
a numeric vector that contains the simulated abundance random effects for each given level of the random effects included in the N-mixture model. Only relevant when abundance random effects are included in the model. |
Jeffrey W. Doser [email protected]
set.seed(400) J.x <- 10 J.y <- 10 n.rep <- rep(4, J.x * J.y) beta <- c(0.5, -0.15) alpha <- c(0.7, 0.4) kappa <- 0.5 phi <- 3 / .6 sigma.sq <- 2 mu.RE <- list(levels = 10, sigma.sq.mu = 1.2) p.RE <- list(levels = 15, sigma.sq.p = 0.8) dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = TRUE, cov.model = 'spherical', sigma.sq = sigma.sq, phi = phi, family = 'NB')
set.seed(400) J.x <- 10 J.y <- 10 n.rep <- rep(4, J.x * J.y) beta <- c(0.5, -0.15) alpha <- c(0.7, 0.4) kappa <- 0.5 phi <- 3 / .6 sigma.sq <- 2 mu.RE <- list(levels = 10, sigma.sq.mu = 1.2) p.RE <- list(levels = 15, sigma.sq.p = 0.8) dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = TRUE, cov.model = 'spherical', sigma.sq = sigma.sq, phi = phi, family = 'NB')
The function spAbund
fits univariate spatial abundance GLMs.
spAbund(formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
spAbund(formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
tuning |
a list with each tag corresponding to a parameter name, whose value defines
the initial variance of the adaptive sampler. Valid tags include
|
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
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 in each chain to run for the adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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 |
verbose |
if |
n.report |
the interval to report Metropolis sampler acceptance and MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of MCMC chains to run in sequence. |
save.fitted |
logical value indicating whether or not fitted values and likelihood values
should be saved in the resulting model object. If |
... |
currently no additional arguments |
An object of class spAbund
that is a list comprised of:
beta.samples |
a |
kappa.samples |
a |
tau.sq.samples |
a |
y.rep.samples |
a two or three-dimensional object of posterior samples for the abundance replicate (fitted) values with dimensions corresponding to MCMC samples, site, and replicate. |
mu.samples |
a two or -three-dimensional array of posterior samples for the expected abundance samples with dimensions corresponding to MCMC samples, site, and replicate. |
theta.samples |
a |
w.samples |
a |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
like.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
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.
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
Roberts, G.O. and Rosenthal J.S. (2009) Examples of adaptive MCMC. Journal of Computational and Graphical Statistics, 18(2):349-367.
set.seed(888) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(50, 45), sigma.sq.mu = c(1.3, 0.5), beta.indx = c(1, 2)) phi <- 3/.6 sigma.sq <- 2 kappa <- 0.2 sp <- TRUE cov.model <- 'exponential' family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), phi.unif = c(3 / 1, 3 / .1), sigma.sq.ig = c(2, 1), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = beta, kappa = kappa, sigma.sq = sigma.sq, phi = phi) tuning <- list(phi = 0.3, kappa = 0.05, beta = 0.1, beta.star = 0.1, w = 0.1) n.batch <- 4 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- spAbund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 | abund.factor.1) + (abund.cov.1 | abund.factor.2), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, tuning = tuning, priors = prior.list, NNGP = TRUE, cov.model = 'exponential', search.type = 'cb', n.neighbors = 5, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(888) J.x <- 8 J.y <- 8 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(50, 45), sigma.sq.mu = c(1.3, 0.5), beta.indx = c(1, 2)) phi <- 3/.6 sigma.sq <- 2 kappa <- 0.2 sp <- TRUE cov.model <- 'exponential' family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4], abund.factor.1 = X.re[, , 1], abund.factor.2 = X.re[, , 2]) data.list <- list(y = y, covs = covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), phi.unif = c(3 / 1, 3 / .1), sigma.sq.ig = c(2, 1), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = beta, kappa = kappa, sigma.sq = sigma.sq, phi = phi) tuning <- list(phi = 0.3, kappa = 0.05, beta = 0.1, beta.star = 0.1, w = 0.1) n.batch <- 4 batch.length <- 25 n.burn <- 20 n.thin <- 1 n.chains <- 1 out <- spAbund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 | abund.factor.1) + (abund.cov.1 | abund.factor.2), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, tuning = tuning, priors = prior.list, NNGP = TRUE, cov.model = 'exponential', search.type = 'cb', n.neighbors = 5, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Function for fitting single-sepcies spatially-explicit hierarchical distance sampling models. Spatial models are fit using Nearest Neighbor Gaussian Processes.
spDS(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
spDS(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', transect = 'line', det.func = 'halfnormal', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and slopes 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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
tuning |
a list with each tag corresponding to a parameter name, whose value
defines the initial variance of the adpative sampler. Valid tags include
|
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
transect |
the type of transect. Currently supports line transects ( |
det.func |
the detection model used to describe how detection probability varies
with distance. In other software, this is often referred to as the key function. Currently
supports two functions: half normal ( |
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
|
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class spDS
that is a list comprised of:
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
N.samples |
a |
mu.samples |
a |
theta.samples |
a |
w.samples |
a |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
y.rep.samples |
a three-dimensional array of fitted values. Array dimensions correspond to MCMC samples, sites, and distance band. |
pi.samples |
a three-dimensional array of cell-specific detection probabilities. Array dimensions correspond to MCMC samples, sites, and distance band. |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
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.
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
Royle, J. A., Dawson, D. K., & Bates, S. (2004). Modeling abundance effects in distance sampling. Ecology, 85(6), 1591-1597.
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- TRUE phi <- 3 / .5 sigma.sq <- 0.8 cov.model <- 'exponential' family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, offset = offset, transect = transect, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model) y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re dist.breaks <- dat$dist.breaks coords <- dat$coords covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100), phi.unif = c(3 / 1, 3 / .1), sigma.sq.ig = c(2, 1)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1, phi = 3 / .5, sigma.sq = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2, phi = 1, w = 1) out <- spDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out)
set.seed(123) J.x <- 10 J.y <- 10 J <- J.x * J.y # Number of distance bins from which to simulate data. n.bins <- 5 # Length of each bin. This should be of length n.bins bin.width <- c(.10, .10, .20, .3, .1) # Abundance coefficients beta <- c(1.0, 0.2, 0.3, -0.2) p.abund <- length(beta) # Detection coefficients alpha <- c(-1.0, -0.3) p.det <- length(alpha) # Detection decay function det.func <- 'halfnormal' mu.RE <- list() p.RE <- list() sp <- TRUE phi <- 3 / .5 sigma.sq <- 0.8 cov.model <- 'exponential' family <- 'NB' kappa <- 0.1 offset <- 1.8 transect <- 'point' dat <- simDS(J.x = J.x, J.y = J.y, n.bins = n.bins, bin.width = bin.width, beta = beta, alpha = alpha, det.func = det.func, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, offset = offset, transect = transect, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model) y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re dist.breaks <- dat$dist.breaks coords <- dat$coords covs <- cbind(X, X.p) colnames(covs) <- c('int.abund', 'abund.cov.1', 'abund.cov.2', 'abund.cov.3', 'int.det', 'det.cov.1') data.list <- list(y = y, covs = covs, dist.breaks = dist.breaks, coords = coords, offset = offset) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 10), alpha.normal = list(mean = 0, var = 10), kappa.unif = c(0, 100), phi.unif = c(3 / 1, 3 / .1), sigma.sq.ig = c(2, 1)) # Starting values inits.list <- list(alpha = 0, beta = 0, kappa = 1, phi = 3 / .5, sigma.sq = 1) # Tuning values tuning <- list(beta = 0.1, alpha = 0.1, beta.star = 0.3, alpha.star = 0.1, kappa = 0.2, phi = 1, w = 1) out <- spDS(abund.formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3, det.formula = ~ det.cov.1, data = data.list, n.batch = 10, batch.length = 25, inits = inits.list, family = 'NB', det.func = 'halfnormal', transect = 'point', cov.model = 'exponential', NNGP = TRUE, n.neighbors = 5, tuning = tuning, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = 100, n.thin = 1, n.chains = 1) summary(out)
The function spNMix
fits single-species spatial N-mixture models. Spatial models are fit using Nearest Neighbor Gaussian Processes.
spNMix(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
spNMix(abund.formula, det.formula, data, inits, priors, tuning, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
abund.formula |
a symbolic description of the model to be fit for the abundance portion of the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts and random slopes 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 and random slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
tuning |
a list with each tag corresponding to a parameter name, whose value
corresponds to the initial tuning variance of the adaptive sampler for
|
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
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 in each chain to run for the Adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for Adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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 |
verbose |
if |
n.report |
the interval to report Metropolis sampler acceptance and MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of MCMC chains to run in sequence. |
... |
currently no additional arguments |
An object of class spNMix
that is a list comprised of:
beta.samples |
a |
alpha.samples |
a |
kappa.samples |
a |
N.samples |
a |
mu.samples |
a |
theta.samples |
a |
w.samples |
a |
sigma.sq.mu.samples |
a |
sigma.sq.p.samples |
a |
beta.star.samples |
a |
alpha.star.samples |
a |
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 |
The return object will include additional objects used for
subsequent prediction and/or model fit evaluation. Note that detection
probability values are not included in the model object, but can be
extracted using fitted()
.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
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.
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
Roberts, G.O. and Rosenthal J.S. (2009) Examples of adaptive MCMC. Journal of Computational and Graphical Statistics, 18(2):349-367.
Royle, J. A. (2004). N‐mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
set.seed(350) # Simulate Data ----------------------------------------------------------- J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0.5, 1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- runif(1, 3 / 1, 3 / .1) sigma.sq <- runif(1, 0.2, 1.5) kappa <- 0.5 sp <- TRUE cov.model <- 'exponential' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re coords <- dat$coords abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = alpha, beta = beta, kappa = kappa, w = rep(0, J), phi = 3 / 0.5, sigma.sq = 1, N = apply(y, 1, max, na.rm = TRUE)) # Tuning values tuning.list <- list(phi = 0.5, kappa = 0.5, beta = 0.1, alpha = 0.1, w = 0.1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- spNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, NNGP = TRUE, cov.model = 'spherical', n.neighbors = 10, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(350) # Simulate Data ----------------------------------------------------------- J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0.5, 1.5) p.abund <- length(beta) alpha <- c(0.5, 1.2, -0.5) p.det <- length(alpha) mu.RE <- list() p.RE <- list() phi <- runif(1, 3 / 1, 3 / .1) sigma.sq <- runif(1, 0.2, 1.5) kappa <- 0.5 sp <- TRUE cov.model <- 'exponential' dat <- simNMix(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, alpha = alpha, kappa = kappa, mu.RE = mu.RE, p.RE = p.RE, sp = sp, phi = phi, sigma.sq = sigma.sq, cov.model = cov.model, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re X.p <- dat$X.p X.p.re <- dat$X.p.re coords <- dat$coords abund.covs <- X colnames(abund.covs) <- c('int', 'abund.cov.1') det.covs <- list(det.cov.1 = X.p[, , 2], det.cov.2 = X.p[, , 3]) data.list <- list(y = y, abund.covs = abund.covs, det.covs = det.covs, coords = coords) # Priors prior.list <- list(beta.normal = list(mean = rep(0, p.abund), var = rep(100, p.abund)), alpha.normal = list(mean = rep(0, p.det), var = rep(2.72, p.det)), kappa.unif = c(0, 10)) # Starting values inits.list <- list(alpha = alpha, beta = beta, kappa = kappa, w = rep(0, J), phi = 3 / 0.5, sigma.sq = 1, N = apply(y, 1, max, na.rm = TRUE)) # Tuning values tuning.list <- list(phi = 0.5, kappa = 0.5, beta = 0.1, alpha = 0.1, w = 0.1) n.batch <- 4 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- spNMix(abund.formula = ~ abund.cov.1, det.formula = ~ det.cov.1 + det.cov.2, data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, NNGP = TRUE, cov.model = 'spherical', n.neighbors = 10, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Methods for extracting information from fitted univariate GLMMs (abund
).
## S3 method for class 'abund' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'abund' print(x, ...) ## S3 method for class 'abund' plot(x, param, density = TRUE, ...)
## S3 method for class 'abund' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'abund' print(x, ...) ## S3 method for class 'abund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class abund
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a abund
object.
Methods for extracting information from fitted single-species hierarchiacl distance sampling (DS
) models.
## S3 method for class 'DS' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'DS' print(x, ...) ## S3 method for class 'DS' plot(x, param, density = TRUE, ...)
## S3 method for class 'DS' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'DS' print(x, ...) ## S3 method for class 'DS' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class DS
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a DS
object.
Methods for extracting information from fitted latent factor multivariate abundance GLMMs (lfMsAbund
).
## S3 method for class 'lfMsAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'lfMsAbund' print(x, ...) ## S3 method for class 'lfMsAbund' plot(x, param, density = TRUE, ...)
## S3 method for class 'lfMsAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'lfMsAbund' print(x, ...) ## S3 method for class 'lfMsAbund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class lfMsAbund
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a lfMsAbund
object.
Methods for extracting information from fitted latent factor multi-species hierarchical distance sampling (lfMsDS
) model.
## S3 method for class 'lfMsDS' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'lfMsDS' print(x, ...) ## S3 method for class 'lfMsDS' plot(x, param, density = TRUE, ...)
## S3 method for class 'lfMsDS' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'lfMsDS' print(x, ...) ## S3 method for class 'lfMsDS' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class lfMsDS
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a lfMsDS
object.
Methods for extracting information from fitted latent factor multi-species N-mixture (lfMsNMix
) model.
## S3 method for class 'lfMsNMix' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'lfMsNMix' print(x, ...) ## S3 method for class 'lfMsNMix' plot(x, param, density = TRUE, ...)
## S3 method for class 'lfMsNMix' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'lfMsNMix' print(x, ...) ## S3 method for class 'lfMsNMix' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class lfMsNMix
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a lfMsNMix
object.
Methods for extracting information from fitted multivariate abundance GLMMs (msAbund
).
## S3 method for class 'msAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'msAbund' print(x, ...) ## S3 method for class 'msAbund' plot(x, param, density = TRUE, ...)
## S3 method for class 'msAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'msAbund' print(x, ...) ## S3 method for class 'msAbund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class msAbund
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a msAbund
object.
Methods for extracting information from fitted multi-species hierarchical distance sampling (msDS
) model.
## S3 method for class 'msDS' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'msDS' print(x, ...) ## S3 method for class 'msDS' plot(x, param, density = TRUE, ...)
## S3 method for class 'msDS' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'msDS' print(x, ...) ## S3 method for class 'msDS' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class msDS
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a msDS
object.
Methods for extracting information from fitted multi-species N-mixture (msNMix
) model.
## S3 method for class 'msNMix' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'msNMix' print(x, ...) ## S3 method for class 'msNMix' plot(x, param, density = TRUE, ...)
## S3 method for class 'msNMix' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'msNMix' print(x, ...) ## S3 method for class 'msNMix' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class msNMix
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a msNMix
object.
Methods for extracting information from fitted single-species N-mixture (NMix
) model.
## S3 method for class 'NMix' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'NMix' print(x, ...) ## S3 method for class 'NMix' plot(x, param, density = TRUE, ...)
## S3 method for class 'NMix' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'NMix' print(x, ...) ## S3 method for class 'NMix' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class NMix
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a NMix
object.
Methods for extracting information from fitted spatial factor multivariate abundance GLMMs (sfMsAbund
).
## S3 method for class 'sfMsAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'sfMsAbund' print(x, ...) ## S3 method for class 'sfMsAbund' plot(x, param, density = TRUE, ...)
## S3 method for class 'sfMsAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'sfMsAbund' print(x, ...) ## S3 method for class 'sfMsAbund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class sfMsAbund
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a sfMsAbund
object.
Methods for extracting information from fitted spatial multi-species hierarchical distance sampling (sfMsDS
) model.
## S3 method for class 'sfMsDS' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'sfMsDS' print(x, ...) ## S3 method for class 'sfMsDS' plot(x, param, density = TRUE, ...)
## S3 method for class 'sfMsDS' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'sfMsDS' print(x, ...) ## S3 method for class 'sfMsDS' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class sfMsDS
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a sfMsDS
object.
Methods for extracting information from fitted spatial factor multi-species N-mixture (sfMsNMix
) model.
## S3 method for class 'sfMsNMix' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'sfMsNMix' print(x, ...) ## S3 method for class 'sfMsNMix' plot(x, param, density = TRUE, ...)
## S3 method for class 'sfMsNMix' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'sfMsNMix' print(x, ...) ## S3 method for class 'sfMsNMix' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class sfMsNMix
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a sfMsNMix
object.
Methods for extracting information from fitted univariate spatially-explicit GLMMs (spAbund
).
## S3 method for class 'spAbund' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'spAbund' print(x, ...) ## S3 method for class 'spAbund' plot(x, param, density = TRUE, ...)
## S3 method for class 'spAbund' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'spAbund' print(x, ...) ## S3 method for class 'spAbund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class spAbund
, including methods to the generic functions print
, summary
, plot
.
No return value, called to display summary information of a spAbund
object.
Methods for extracting information from fitted single-species spatial hierarchiacl distance sampling (spDS
) models.
## S3 method for class 'spDS' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'spDS' print(x, ...) ## S3 method for class 'spDS' plot(x, param, density = TRUE, ...)
## S3 method for class 'spDS' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'spDS' print(x, ...) ## S3 method for class 'spDS' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class spDS
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a spDS
object.
Methods for extracting information from fitted single-species spatial N-mixture (spNMix
) models.
## S3 method for class 'spNMix' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'spNMix' print(x, ...) ## S3 method for class 'spNMix' plot(x, param, density = TRUE, ...)
## S3 method for class 'spNMix' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'spNMix' print(x, ...) ## S3 method for class 'spNMix' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class spNMix
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a spNMix
object.
Methods for extracting information from fitted univariate spatially-varying coefficient GLMMs (svcAbund
).
## S3 method for class 'svcAbund' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'svcAbund' print(x, ...) ## S3 method for class 'svcAbund' plot(x, param, density = TRUE, ...)
## S3 method for class 'svcAbund' summary(object, quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'svcAbund' print(x, ...) ## S3 method for class 'svcAbund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class svcAbund
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a svcAbund
object.
Methods for extracting information from fitted multivariate spatially-varying coefficient abundance GLMMs (svcMsAbund
).
## S3 method for class 'svcMsAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'svcMsAbund' print(x, ...) ## S3 method for class 'svcMsAbund' plot(x, param, density = TRUE, ...)
## S3 method for class 'svcMsAbund' summary(object, level = 'both', quantiles = c(0.025, 0.5, 0.975), digits = max(3L, getOption("digits") - 3L), ...) ## S3 method for class 'svcMsAbund' print(x, ...) ## S3 method for class 'svcMsAbund' plot(x, param, density = TRUE, ...)
object , x
|
object of class |
level |
a quoted keyword that indicates the level to summarize the
model results. Valid key words are: |
quantiles |
for |
digits |
for |
param |
parameter name for which to generate a traceplot. Valid names are
|
density |
logical value indicating whether to also generate a density plot for each parameter in addition to the MCMC traceplot. |
... |
currently no additional arguments |
A set of standard extractor functions for fitted model objects of
class svcMsAbund
, including methods to the generic functions print
, summary
, and plot
.
No return value, called to display summary information of a svcMsAbund
object.
The function svcAbund
fits univariate spatially-varying coefficient GLMMs.
svcAbund(formula, data, inits, priors, tuning, svc.cols = 1, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
svcAbund(formula, data, inits, priors, tuning, svc.cols = 1, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.batch, batch.length, accept.rate = 0.43, family = 'Poisson', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, save.fitted = TRUE, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
svc.cols |
a vector indicating the variables whose effects will be
estimated as spatially-varying coefficients. |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
tuning |
a list with each tag corresponding to a parameter name,
whose value defines the initial variance of the adaptive sampler.
Valid tags are |
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
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 in each chain to run for the adaptive MCMC sampler. See Roberts and Rosenthal (2009) for details. |
accept.rate |
target acceptance rate for adaptive MCMC. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for the latent abundance process. Currently
supports |
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 |
verbose |
if |
n.report |
the interval to report Metropolis sampler acceptance and MCMC progress. |
n.burn |
the number of samples out of the total |
n.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of MCMC chains to run in sequence. |
save.fitted |
logical value indicating whether or not fitted values and likelihood values
should be saved in the resulting model object. If |
... |
currently no additional arguments |
An object of class svcAbund
that is a list comprised of:
beta.samples |
a |
tau.sq.samples |
a |
kappa.samples |
a |
y.rep.samples |
a two or three-dimensional object of posterior samples for the abundance replicate (fitted) values with dimensions corresponding to MCMC samples, site, and replicate. |
mu.samples |
a two or -three-dimensional array of posterior samples for the expected abundance samples with dimensions corresponding to MCMC samples, site, and replicate. |
theta.samples |
a |
w.samples |
a three-dimensional array of posterior samples for the spatially-varying coefficients with dimensions corresponding to MCMC sample, SVC, and site. |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
like.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
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.
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
Roberts, G.O. and Rosenthal J.S. (2009) Examples of adaptive MCMC. Journal of Computational and Graphical Statistics, 18(2):349-367.
set.seed(1000) # Sites J.x <- 10 J.y <- 10 J <- J.x * J.y # Abundance --------------------------- beta <- c(5, 0.5, -0.2, 0.75) p <- length(beta) mu.RE <- list() mu.RE <- list(levels = c(35, 40), sigma.sq.mu = c(0.7, 1.5), beta.indx = list(1, 1)) # Spatial parameters ------------------ sp <- TRUE svc.cols <- c(1, 2) p.svc <- length(svc.cols) cov.model <- "exponential" sigma.sq <- runif(p.svc, 0.4, 4) phi <- runif(p.svc, 3/1, 3/0.6) tau.sq <- 2 z <- rbinom(J, 1, 0.5) # Get all the data dat <- simAbund(J.x = J.x, J.y = J.y, beta = beta, tau.sq = tau.sq, mu.RE = mu.RE, sp = sp, svc.cols = svc.cols, family = 'zi-Gaussian', cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, z = z) # Get data in format for spAbundance -------------------------------------- y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords # Package all data into a list covs <- cbind(X, X.re) colnames(covs) <- c('int', 'cov.1', 'cov.2', 'cov.3', 'factor.1', 'factor.2') # Data list bundle data.list <- list(y = y, covs = covs, coords = coords, z = z) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 1000), sigma.sq.ig = list(a = 2, b = 1), tau.sq = c(2, 1), sigma.sq.mu.ig = list(a = 2, b = 1), phi.unif = list(a = 3 / 1, b = 3 / 0.1)) # Starting values inits.list <- list(beta = 0, alpha = 0, sigma.sq = 1, phi = 3 / 0.5, tau.sq = 2, sigma.sq.mu = 0.5) # Tuning tuning.list <- list(phi = 1) n.batch <- 10 batch.length <- 25 n.burn <- 100 n.thin <- 1 out <- svcAbund(formula = ~ cov.1 + cov.2 + cov.3 + (1 | factor.1) + (1 | factor.2), svc.cols = c(1, 2), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, family = 'zi-Gaussian', cov.model = "exponential", tuning = tuning.list, n.omp.threads = 1, verbose = TRUE, NNGP = TRUE, n.neighbors = 5, n.report = 25, n.burn = n.burn, n.thin = n.thin, n.chains = 3)
set.seed(1000) # Sites J.x <- 10 J.y <- 10 J <- J.x * J.y # Abundance --------------------------- beta <- c(5, 0.5, -0.2, 0.75) p <- length(beta) mu.RE <- list() mu.RE <- list(levels = c(35, 40), sigma.sq.mu = c(0.7, 1.5), beta.indx = list(1, 1)) # Spatial parameters ------------------ sp <- TRUE svc.cols <- c(1, 2) p.svc <- length(svc.cols) cov.model <- "exponential" sigma.sq <- runif(p.svc, 0.4, 4) phi <- runif(p.svc, 3/1, 3/0.6) tau.sq <- 2 z <- rbinom(J, 1, 0.5) # Get all the data dat <- simAbund(J.x = J.x, J.y = J.y, beta = beta, tau.sq = tau.sq, mu.RE = mu.RE, sp = sp, svc.cols = svc.cols, family = 'zi-Gaussian', cov.model = cov.model, sigma.sq = sigma.sq, phi = phi, z = z) # Get data in format for spAbundance -------------------------------------- y <- dat$y X <- dat$X X.re <- dat$X.re coords <- dat$coords # Package all data into a list covs <- cbind(X, X.re) colnames(covs) <- c('int', 'cov.1', 'cov.2', 'cov.3', 'factor.1', 'factor.2') # Data list bundle data.list <- list(y = y, covs = covs, coords = coords, z = z) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 1000), sigma.sq.ig = list(a = 2, b = 1), tau.sq = c(2, 1), sigma.sq.mu.ig = list(a = 2, b = 1), phi.unif = list(a = 3 / 1, b = 3 / 0.1)) # Starting values inits.list <- list(beta = 0, alpha = 0, sigma.sq = 1, phi = 3 / 0.5, tau.sq = 2, sigma.sq.mu = 0.5) # Tuning tuning.list <- list(phi = 1) n.batch <- 10 batch.length <- 25 n.burn <- 100 n.thin <- 1 out <- svcAbund(formula = ~ cov.1 + cov.2 + cov.3 + (1 | factor.1) + (1 | factor.2), svc.cols = c(1, 2), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, family = 'zi-Gaussian', cov.model = "exponential", tuning = tuning.list, n.omp.threads = 1, verbose = TRUE, NNGP = TRUE, n.neighbors = 5, n.report = 25, n.burn = n.burn, n.thin = n.thin, n.chains = 3)
The function svcMsAbund
fits multivariate spatially-varying coefficient GLMs with species correlations (i.e., a spatially-explicit abundace-based joint species distribution model). We use a spatial factor modeling approach. Models are implemented using a Nearest Neighbor Gaussian Process.
svcMsAbund(formula, data, inits, priors, tuning, svc.cols = 1, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Gaussian', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
svcMsAbund(formula, data, inits, priors, tuning, svc.cols = 1, cov.model = 'exponential', NNGP = TRUE, n.neighbors = 15, search.type = 'cb', n.factors, n.batch, batch.length, accept.rate = 0.43, family = 'Gaussian', n.omp.threads = 1, verbose = TRUE, n.report = 100, n.burn = round(.10 * n.batch * batch.length), n.thin = 1, n.chains = 1, ...)
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 and slopes are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
tuning |
a list with each tag corresponding to a parameter name, whose value defines
the initial tuning variance of the adaptive sampler for |
svc.cols |
a vector indicating the variables whose effects will be
estimated as spatially-varying coefficients. |
cov.model |
a quoted keyword that specifies the covariance
function used to model the spatial dependence structure among the
observations. Supported covariance model key words are:
|
NNGP |
if |
n.neighbors |
number of neighbors used in the NNGP. Only used if
|
search.type |
a quoted keyword that specifies the type of nearest
neighbor search algorithm. Supported method key words are: |
n.factors |
the number of factors to use in the spatial factor model approach for each spatially-varying coefficient. 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 the number of species in the modeled community. |
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. Default is 0.43. See Roberts and Rosenthal (2009) for details. |
family |
the distribution to use for abundance. Currently, spatially-varying
coefficient models are available for |
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 |
verbose |
if |
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.thin |
the thinning interval for collection of MCMC samples. The
thinning occurs after the |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
An object of class svcMsAbund
that is a list comprised of:
beta.comm.samples |
a |
tau.sq.beta.samples |
a |
beta.samples |
a |
tau.sq.samples |
a |
theta.samples |
a |
lambda.samples |
a |
y.rep.samples |
a three or four-dimensional array of posterior samples for the fitted (replicate) values for each species with dimensions corresponding to MCMC sample, species, site, and replicate. |
mu.samples |
a three or four-dimensional array of posterior samples for the expected abundance values for each species with dimensions corresponding to MCMC samples, species, site, and replicate. |
w.samples |
a four-dimensional array of posterior samples for the latent spatial random effects for each spatial factor within each spatially-varying coefficient. Dimensions correspond to MCMC sample, factor, site, and spatially-varying coefficient. |
sigma.sq.mu.samples |
a |
beta.star.samples |
a |
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 |
The return object will include additional objects used for subsequent prediction and/or model fit evaluation.
Jeffrey W. Doser [email protected],
Andrew O. Finley [email protected]
Datta, A., S. Banerjee, A.O. Finley, and A.E. Gelfand. (2016) Hierarchical Nearest-Neighbor Gaussian process models for large geostatistical datasets. Journal of the American Statistical Association, doi:10.1080/01621459.2015.1044091.
Finley, A.O., A. Datta, B.D. Cook, D.C. Morton, H.E. Andersen, and S. Banerjee. (2019) Efficient algorithms for Bayesian Nearest Neighbor Gaussian Processes. Journal of Computational and Graphical Statistics, doi:10.1080/10618600.2018.1537924.
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.
Christensen, W. F., and Amemiya, Y. (2002). Latent variable analysis of multivariate spatial data. Journal of the American Statistical Association, 97(457), 302-317.
set.seed(332) J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- rep(1, J) n.sp <- 6 # Community-level covariate effects beta.mean <- c(0, 0.25, 0.6) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2, 0.4) # Random effects mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE svc.cols <- c(1, 2) n.factors <- 2 q.p.svc <- length(svc.cols) * n.factors factor.model <- TRUE phi <- runif(q.p.svc, 3/1, 3 / .4) tau.sq <- runif(n.sp, 0.1, 5) cov.model <- 'exponential' family <- 'Gaussian' dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, tau.sq = tau.sq, family = family, factor.model = factor.model, phi = phi, cov.model = cov.model, n.factors = n.factors, svc.cols = svc.cols) y <- dat$y X <- dat$X coords <- dat$coords covs <- data.frame(abund.cov.1 = X[, 2], abund.cov.2 = X[, 3]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), tau.sq.ig = list(a = 2, b = 2), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, tau.sq = 1, tau.sq.beta = 1, phi = 3 / 0.5) tuning.list <- list(phi = 0.5) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- svcMsAbund(formula = ~ abund.cov.1 + abund.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, NNGP = TRUE, svc.cols = c(1, 2), family = 'Gaussian', cov.model = 'exponential', n.neighbors = 5, n.factors = n.factors, batch.length = batch.length, n.omp.threads = 1, verbose = TRUE, n.report = 20, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
set.seed(332) J.x <- 10 J.y <- 10 J <- J.x * J.y n.rep <- rep(1, J) n.sp <- 6 # Community-level covariate effects beta.mean <- c(0, 0.25, 0.6) p.abund <- length(beta.mean) tau.sq.beta <- c(0.2, 1.2, 0.4) # Random effects mu.RE <- list() # Draw species-level effects from community means. beta <- matrix(NA, nrow = n.sp, ncol = p.abund) for (i in 1:p.abund) { beta[, i] <- rnorm(n.sp, beta.mean[i], sqrt(tau.sq.beta[i])) } sp <- TRUE svc.cols <- c(1, 2) n.factors <- 2 q.p.svc <- length(svc.cols) * n.factors factor.model <- TRUE phi <- runif(q.p.svc, 3/1, 3 / .4) tau.sq <- runif(n.sp, 0.1, 5) cov.model <- 'exponential' family <- 'Gaussian' dat <- simMsAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, n.sp = n.sp, beta = beta, mu.RE = mu.RE, sp = sp, tau.sq = tau.sq, family = family, factor.model = factor.model, phi = phi, cov.model = cov.model, n.factors = n.factors, svc.cols = svc.cols) y <- dat$y X <- dat$X coords <- dat$coords covs <- data.frame(abund.cov.1 = X[, 2], abund.cov.2 = X[, 3]) data.list <- list(y = y, covs = covs, coords = coords) prior.list <- list(beta.comm.normal = list(mean = 0, var = 100), tau.sq.ig = list(a = 2, b = 2), phi.unif = list(a = 3 / 1, b = 3 / .1), tau.sq.beta.ig = list(a = .1, b = .1)) inits.list <- list(beta.comm = 0, beta = 0, tau.sq = 1, tau.sq.beta = 1, phi = 3 / 0.5) tuning.list <- list(phi = 0.5) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- svcMsAbund(formula = ~ abund.cov.1 + abund.cov.2, data = data.list, n.batch = n.batch, inits = inits.list, priors = prior.list, tuning = tuning.list, NNGP = TRUE, svc.cols = c(1, 2), family = 'Gaussian', cov.model = 'exponential', n.neighbors = 5, n.factors = n.factors, batch.length = batch.length, n.omp.threads = 1, verbose = TRUE, n.report = 20, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) summary(out)
Function for computing the Widely Applicable Information Criterion
(WAIC; Watanabe 2010) for spAbundance
model objects.
waicAbund(object, N.max, by.species = FALSE, ...)
waicAbund(object, N.max, by.species = FALSE, ...)
object |
an object of class |
N.max |
values indicating the upper limit on the latent abundance
values when calculating WAIC for N-mixture models or hierarchical distance sampling models.
For single-species models, this can be a single value or a vector of
different values for each site. For multi-species models, this can be a
single value, a vector of values for each species, or a species by site matrix
for a separate value for each species/site combination.
Defaults to ten plus the largest abundance value for each site/species
in the posterior samples |
by.species |
a logical value indicating whether or not WAIC should be
reported individually for each species ( |
... |
currently no additional arguments |
The effective number of parameters is calculated following the recommendations of Gelman et al. (2014).
Returns a vector with three elements corresponding to
estimates of the expected log pointwise predictive density (elpd), the
effective number of parameters (pD), and the WAIC. If calculating WAIC
for a multi-species model and by.species = TRUE
, this will be a
data frame with rows corresponding to the different species.
When fitting zero-inflated Gaussian models, the WAIC is only calculated for the
non-zero values. If fitting a first stage model with
spOccupancy
to the binary portion of the
zero-inflated model, you can use the spOccupancy::waicOcc
function to
calculate WAIC for the binary component.
Jeffrey W. Doser [email protected],
Watanabe, S. (2010). Asymptotic equivalence of Bayes cross validation and widely applicable information criterion in singular learning theory. Journal of Machine Learning Research, 11:3571-3594.
Gelman, A., J. B. Carlin, H. S. Stern, D. B. Dunson, A. Vehtari, and D. B. Rubin. (2013). Bayesian Data Analysis. 3rd edition. CRC Press, Taylor and Francis Group
Gelman, A., J. Hwang, and A. Vehtari (2014). Understanding predictive information criteria for Bayesian models. Statistics and Computing, 24:997-1016.
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(30), sigma.sq.mu = c(1.3)) kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re abund.covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4], abund.factor.1 = X.re[, , 1]) data.list <- list(y = y, covs = abund.covs) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 | abund.factor.1), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Calculate WAIC waicAbund(out)
set.seed(1010) J.x <- 15 J.y <- 15 J <- J.x * J.y n.rep <- sample(3, J, replace = TRUE) beta <- c(0, -1.5, 0.3, -0.8) p.abund <- length(beta) mu.RE <- list(levels = c(30), sigma.sq.mu = c(1.3)) kappa <- 0.5 sp <- FALSE family <- 'NB' dat <- simAbund(J.x = J.x, J.y = J.y, n.rep = n.rep, beta = beta, kappa = kappa, mu.RE = mu.RE, sp = sp, family = 'NB') y <- dat$y X <- dat$X X.re <- dat$X.re abund.covs <- list(int = X[, , 1], abund.cov.1 = X[, , 2], abund.cov.2 = X[, , 3], abund.cov.3 = X[, , 4], abund.factor.1 = X.re[, , 1]) data.list <- list(y = y, covs = abund.covs) # Priors prior.list <- list(beta.normal = list(mean = 0, var = 100), kappa.unif = c(0.001, 10)) # Starting values inits.list <- list(beta = 0, kappa = kappa) n.batch <- 5 batch.length <- 25 n.burn <- 0 n.thin <- 1 n.chains <- 1 out <- abund(formula = ~ abund.cov.1 + abund.cov.2 + abund.cov.3 + (1 | abund.factor.1), data = data.list, n.batch = n.batch, batch.length = batch.length, inits = inits.list, priors = prior.list, accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 1, n.burn = n.burn, n.thin = n.thin, n.chains = n.chains) # Calculate WAIC waicAbund(out)