Skip to content

Commit

Permalink
update c++ files for noRemap on CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
doserjef committed May 17, 2024
1 parent 47e3c91 commit 5be6571
Show file tree
Hide file tree
Showing 60 changed files with 6,048 additions and 5,921 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: spAbundance
Type: Package
Title: Univariate and Multivariate Spatial Modeling of Species Abundance
Version: 0.1.3
Version: 0.1.4
Authors@R: c(person("Jeffrey", "Doser", role=c("aut", "cre"), email="[email protected]"), person("Andrew", "Finley", role = c("aut")))
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.
License: GPL (>= 3)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# spAbundance 0.1.4

+ Added in a check at the top of all model fitting functions to return an error when the number of posterior samples saved based on the MCMC criteria (`n.batch`, `batch.length`, `n.samples`, `n.burn`, `n.thin`, `n.chains`) are specified in a way that leads to a non-integer value. In such situations, models would previously run and return without an error, but sometimes the last posterior sample in any given chain could have widely inaccurate values, or values that prevented subsequent functions from working. Thanks to Wendy Leuenberger for bringing this to my attention.
+ Updated C++ code to adhere to the new lack of re-mapping of functions in Rinternals.h and R_ext/Error.h when building packages on CRAN.


# spAbundance 0.1.3

+ Added in the `independent.betas` tag into the `priors` list for certain multi-species model types to allow for specifying an independent prior on the species-specific random effects as opposed to treating species-specific effects as random effects. This can be useful under certain circumstances when the distribution of effects across species may not be adequately represented by a Gaussian distribution. This tag is available for the following functions: `lfMsAbund` (Gaussian only), `sfMsAbund` (Gaussian only), and `svcMsAbund`.
Expand Down
6 changes: 5 additions & 1 deletion R/DS.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DS <- function(abund.formula, det.formula, data, inits, priors, tuning,
n.batch, batch.length, accept.rate = 0.43, family = 'Poisson',
transect = 'line', det.func = 'halfnormal',
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, ...){
Expand Down Expand Up @@ -108,6 +108,10 @@ DS <- function(abund.formula, det.formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

if (!(family) %in% c('Poisson', 'NB')) {
stop("family must be either 'Poisson' or 'NB'")
Expand Down
4 changes: 4 additions & 0 deletions R/NMix.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ NMix <- function(abund.formula, det.formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

if (!(family) %in% c('Poisson', 'NB')) {
stop("family must be either 'Poisson' or 'NB'")
Expand Down
4 changes: 4 additions & 0 deletions R/abund.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ abund <- function(formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

# Get occurrence covariates in proper format
# Subset covariates to only use those that are included in the analysis
Expand Down
12 changes: 8 additions & 4 deletions R/abundGaussian.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
abundGaussian <- function(formula, data, inits, priors, tuning, 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, save.fitted = TRUE, ...){
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, save.fitted = TRUE, ...){

ptm <- proc.time()

Expand Down Expand Up @@ -182,6 +182,10 @@ abundGaussian <- function(formula, data, inits, priors, tuning, n.batch,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
n.post.samples <- length(seq(from = n.burn + 1,
to = n.samples,
by = as.integer(n.thin)))
Expand Down
8 changes: 6 additions & 2 deletions R/lfMsAbund.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
lfMsAbund <- function(formula, data, inits, priors,
tuning, n.factors, n.batch, batch.length,
accept.rate = 0.43, family = 'Poisson',
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, ...){
n.thin = 1, n.chains = 1, save.fitted = TRUE, ...){

ptm <- proc.time()

Expand Down Expand Up @@ -94,6 +94,10 @@ lfMsAbund <- function(formula, data, inits, priors,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
if (missing(n.factors)) {
stop("error: n.factors must be specified for a spatial factor GLMM")
}
Expand Down
6 changes: 5 additions & 1 deletion R/lfMsAbundGaussian.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ lfMsAbundGaussian <- function(formula, data, inits, priors, tuning, n.factors,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

# y is ordered by site, then species within site.
y.orig <- y
Expand Down Expand Up @@ -615,7 +619,7 @@ lfMsAbundGaussian <- function(formula, data, inits, priors, tuning, n.factors,
for (i in 1:n.chains) {
# Change initial values if i > 1
if ((i > 1) & (!fix.inits)) {
if (!ind.betas) {
if (!ind.betas) {
beta.comm.inits <- rnorm(p, mu.beta.comm, sqrt(sigma.beta.comm))
tau.sq.beta.inits <- runif(p, 0.5, 10)
}
Expand Down
5 changes: 5 additions & 0 deletions R/lfMsDS.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ lfMsDS <- function(abund.formula, det.formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

if (missing(n.factors)) {
stop("n.factors must be specified for a latent factor N-mixture model")
}
Expand Down
6 changes: 5 additions & 1 deletion R/lfMsNMix.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lfMsNMix <- function(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.samples), n.thin = 1,
n.burn = round(.10 * n.batch * batch.length), n.thin = 1,
n.chains = 1, ...){

ptm <- proc.time()
Expand Down Expand Up @@ -100,6 +100,10 @@ lfMsNMix <- function(abund.formula, det.formula, data, inits, priors,
if (n.thin > n.samples) {
stop("n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
if (missing(n.factors)) {
stop("n.factors must be specified for a latent factor N-mixture model")
}
Expand Down
4 changes: 4 additions & 0 deletions R/msAbund.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ msAbund <- function(formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
# For later
y.mat <- y
# Offset
Expand Down
4 changes: 4 additions & 0 deletions R/msAbundGaussian.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ msAbundGaussian <- function(formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

# y is ordered by site, then species within site.
y.orig <- y
Expand Down
4 changes: 4 additions & 0 deletions R/msDS.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ msDS <- function(abund.formula, det.formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

data$covs <- as.data.frame(data$covs)

Expand Down
6 changes: 5 additions & 1 deletion R/msNMix.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msNMix <- function(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.samples), n.thin = 1,
n.burn = round(.10 * n.batch * batch.length), n.thin = 1,
n.chains = 1, ...){

ptm <- proc.time()
Expand Down Expand Up @@ -93,6 +93,10 @@ msNMix <- function(abund.formula, det.formula, data, inits, priors,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

# First subset detection covariates to only use those that are included in the analysis.
data$det.covs <- data$det.covs[names(data$det.covs) %in% all.vars(det.formula)]
Expand Down
4 changes: 4 additions & 0 deletions R/sfMsAbund.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ sfMsAbund <- function(formula, data, inits, priors,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
if (missing(n.factors)) {
stop("error: n.factors must be specified for a spatial factor GLMM")
}
Expand Down
4 changes: 4 additions & 0 deletions R/sfMsAbundGaussian.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ sfMsAbundGaussian <- function(formula, data, inits, priors,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

# y is ordered by site, then species within site.
y.orig <- y
Expand Down
14 changes: 9 additions & 5 deletions R/sfMsDS.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
sfMsDS <- function(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',
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.burn = round(.10 * n.batch * batch.length), n.thin = 1,
n.chains = 1, ...){

ptm <- proc.time()
Expand Down Expand Up @@ -136,6 +136,10 @@ sfMsDS <- function(abund.formula, det.formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
if (missing(n.factors)) {
stop("n.factors must be specified for a latent factor N-mixture model")
}
Expand Down
4 changes: 4 additions & 0 deletions R/sfMsNMix.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ sfMsNMix <- function(abund.formula, det.formula, data, inits, priors,
if (n.thin > n.samples) {
stop("n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
if (missing(n.factors)) {
stop("n.factors must be specified for a spatial factor N-mixture model")
}
Expand Down
4 changes: 4 additions & 0 deletions R/spAbund.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ spAbund <- function(formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}

if (!(family) %in% c('Poisson', 'NB')) {
stop("family must be either 'Poisson' or 'NB'")
Expand Down
16 changes: 10 additions & 6 deletions R/spAbundGaussian.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spAbundGaussian <- function(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 = 'Gaussian',
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, ...){
cov.model = 'exponential', NNGP = TRUE,
n.neighbors = 15, search.type = 'cb', 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, save.fitted = TRUE, ...){

ptm <- proc.time()

Expand Down Expand Up @@ -203,6 +203,10 @@ spAbundGaussian <- function(formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
n.post.samples <- length(seq(from = n.burn + 1,
to = n.samples,
by = as.integer(n.thin)))
Expand Down
10 changes: 7 additions & 3 deletions R/spDS.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spDS <- function(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.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, ...){
Expand Down Expand Up @@ -110,6 +110,10 @@ spDS <- function(abund.formula, det.formula, data, inits, priors, tuning,
if (n.thin > n.samples) {
stop("error: n.thin must be less than n.samples")
}
# Check if n.burn, n.thin, and n.samples result in an integer and error if otherwise.
if (((n.samples - n.burn) / n.thin) %% 1 != 0) {
stop("the number of posterior samples to save ((n.samples - n.burn) / n.thin) is not a whole number. Please respecify the MCMC criteria such that the number of posterior samples saved is a whole number.")
}
if (!'coords' %in% names(data)) {
stop("error: coords must be specified in data for a spatial N-mixture model.")
}
Expand Down
Loading

0 comments on commit 5be6571

Please sign in to comment.