Skip to content

Commit

Permalink
fix svcAbund
Browse files Browse the repository at this point in the history
  • Loading branch information
doserjef committed Sep 25, 2024
1 parent 31a29c5 commit c6ba404
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 60 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ Package: spAbundance
Type: Package
Title: Univariate and Multivariate Spatial Modeling of Species Abundance
Version: 0.2.0
Authors@R: c(person("Jeffrey", "Doser", role=c("aut", "cre"), email="doserjef@msu.edu"), person("Andrew", "Finley", role = c("aut")))
Authors@R: c(person("Jeffrey", "Doser", role=c("aut", "cre"), email="jwdoser@ncsu.edu"), 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)
Encoding: UTF-8
LazyData: true
URL: https://www.jeffdoser.com/files/spabundance-web
URL: https://www.doserlab.com/files/spabundance-web
https://groups.google.com/g/spocc-spabund-users
BugReports: https://github.com/doserjef/spAbundance/issues
Depends: R (>= 3.5.0)
Imports:
Expand Down
12 changes: 6 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ knitr::opts_chunk$set(
out.width = "100%")
```

# spAbundance <a href='https://www.jeffdoser.com/files/spabundance-web/'><img src="man/figures/logo.png" align="right" height="139" width="120"/></a>
# spAbundance <a href='https://www.doserlab.com/files/spabundance-web/'><img src="man/figures/logo.png" align="right" height="139" width="120"/></a>

[![](http://cranlogs.r-pkg.org/badges/grand-total/spAbundance?color=blue)](https://CRAN.R-project.org/package=spAbundance)
[![CRAN](https://www.r-pkg.org/badges/version/spAbundance)](https://CRAN.R-project.org/package=spAbundance)
[![Codecov test coverage](https://codecov.io/gh/doserjef/spAbundance/branch/main/graph/badge.svg)](https://codecov.io/gh/doserjef/spAbundance?branch=main)

`spAbundance` fits univariate (i.e., single-species) and multivariate (i.e., multi-species) spatial N-mixture models, hierarchical distance sampling models, and generalized linear mixed models using Markov chain Monte Carlo (MCMC). Spatial models are fit using Nearest Neighbor Gaussian Processes (NNGPs) to facilitate model fitting to large spatial datasets. `spAbundance` uses analogous syntax to its "sister package" [spOccupancy](https://www.jeffdoser.com/files/spoccupancy-web/) (Doser et al. 2022). Below we provide a very brief introduction to some of the package's functionality, and illustrate just one of the model fitting functions. For more information, see the resources referenced at the bottom of this page and the "Articles" tab at the top of the page.
`spAbundance` fits univariate (i.e., single-species) and multivariate (i.e., multi-species) spatial N-mixture models, hierarchical distance sampling models, and generalized linear mixed models using Markov chain Monte Carlo (MCMC). Spatial models are fit using Nearest Neighbor Gaussian Processes (NNGPs) to facilitate model fitting to large spatial datasets. `spAbundance` uses analogous syntax to its "sister package" [spOccupancy](https://www.doserlab.com/files/spoccupancy-web/) (Doser et al. 2022). Below we provide a very brief introduction to some of the package's functionality, and illustrate just one of the model fitting functions. For more information, see the resources referenced at the bottom of this page and the "Articles" tab at the top of the page. Please also consider joining the [`spAbundance` and `spOccupancy` users google group](https://groups.google.com/g/spocc-spabund-users).

## Installation

Expand Down Expand Up @@ -65,7 +65,7 @@ Note that because we implement the MCMC in C++, you will need a C++ compiler on
|`simAbund()` | Simulate single-species count data |
|`simMsAbund()` | Simulate multi-species count data |

All model fitting functions allow for Poisson and negative binomial distributions for the abundance portion of the model. All GLM(M)s also allow for Gaussian and zero-inflated Gaussian models. Note the two functions for fitting spatailly-varying coefficient models are only available for Gaussian and zero-inflated Gaussian models.
All model fitting functions allow for Poisson and negative binomial distributions for the abundance portion of the model. All GLM(M)s also allow for Gaussian and zero-inflated Gaussian models. Note the multi-species spatailly-varying coefficient models are only available for Gaussian and zero-inflated Gaussian models.

## Example usage

Expand Down Expand Up @@ -147,15 +147,15 @@ out.pred <- predict(out, X.0, coords.0, verbose = FALSE)

## Learn more

The `vignette("distanceSampling")`, `vignette("nMixtureModels")`, and `vignette("glmm")` provide detailed descriptions and tutorials of all hierarchical distance sampling models, N-mixture models, and generalized linear mixed models in `spAbundance`, respectively. Given the similarity in syntax to fitting occupancy models in the `spOccupancy` package, much of the documentation on the [`spOccupancy` website](https://www.jeffdoser.com/files/spoccupancy-web/) will also be helpful for fitting models in `spAbundance`.
The `vignette("distanceSampling")`, `vignette("nMixtureModels")`, and `vignette("glmm")` provide detailed descriptions and tutorials of all hierarchical distance sampling models, N-mixture models, and generalized linear mixed models in `spAbundance`, respectively. Given the similarity in syntax to fitting occupancy models in the `spOccupancy` package, much of the documentation on the [`spOccupancy` website](https://www.doserlab.com/files/spoccupancy-web/) will also be helpful for fitting models in `spAbundance`. Please also consider joining the [`spAbundance` and `spOccupancy` users google group](https://groups.google.com/g/spocc-spabund-users) to learn from others who use the two packages.

## Citing `spAbundance`

Please cite `spAbundance` as:

Doser, J. W., Finley, A. O., Kéry, M., and Zipkin, E. F. (2023). spAbundance: An R package for single-species and multi-species spatially-explicit abundance models. arXiv Preprint.
Doser, J. W., Finley A. O., Kéry, M., & Zipkin E. F. (2024). spAbundance: An R package for single-species and multi-species spatially explicit abundance models, Methods in Ecology and Evolution. 15, 1024-1033. https://doi.org/10.1111/2041-210X.14332")

## References

Doser, J. W., Finley, A. O., Kéry, M., and Zipkin, E. F. (2022). spOccupancy: An R package for single-species, multi-species, and integrated spatial occupancy models. Methods in Ecology and Evolution. https://doi.org/10.1111/2041-210X.13897.
Doser, J. W., Finley, A. O., Kéry, M., and Zipkin, E. F. (2022). spOccupancy: An R package for single-species, multi-species, and integrated spatial occupancy models. Methods in Ecology and Evolution, 13(8), 1670-1678. https://doi.org/10.1111/2041-210X.13897.

38 changes: 21 additions & 17 deletions README.html

Large diffs are not rendered by default.

36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# spAbundance <a href='https://www.jeffdoser.com/files/spabundance-web/'><img src="man/figures/logo.png" align="right" height="139" width="120"/></a>
# spAbundance <a href='https://www.doserlab.com/files/spabundance-web/'><img src="man/figures/logo.png" align="right" height="139" width="120"/></a>

[![](http://cranlogs.r-pkg.org/badges/grand-total/spAbundance?color=blue)](https://CRAN.R-project.org/package=spAbundance)
[![CRAN](https://www.r-pkg.org/badges/version/spAbundance)](https://CRAN.R-project.org/package=spAbundance)
[![Codecov test
coverage](https://codecov.io/gh/doserjef/spAbundance/branch/main/graph/badge.svg)](https://app.codecov.io/gh/doserjef/spAbundance?branch=main)
coverage](https://codecov.io/gh/doserjef/spAbundance/branch/main/graph/badge.svg)](https://codecov.io/gh/doserjef/spAbundance?branch=main)

`spAbundance` fits univariate (i.e., single-species) and multivariate
(i.e., multi-species) spatial N-mixture models, hierarchical distance
sampling models, and generalized linear mixed models using Markov chain
Monte Carlo (MCMC). Spatial models are fit using Nearest Neighbor
Gaussian Processes (NNGPs) to facilitate model fitting to large spatial
datasets. `spAbundance` uses analogous syntax to its “sister package”
[spOccupancy](https://www.jeffdoser.com/files/spoccupancy-web/) (Doser
et al. 2022). Below we provide a very brief introduction to some of the
[spOccupancy](https://www.doserlab.com/files/spoccupancy-web/) (Doser et
al. 2022). Below we provide a very brief introduction to some of the
package’s functionality, and illustrate just one of the model fitting
functions. For more information, see the resources referenced at the
bottom of this page and the “Articles” tab at the top of the page.
Please also consider joining the [`spAbundance` and `spOccupancy` users
google group](https://groups.google.com/g/spocc-spabund-users).

## Installation

Expand Down Expand Up @@ -74,9 +76,9 @@ C++ on a Mac, you can install `XCode` from the Mac app store.

All model fitting functions allow for Poisson and negative binomial
distributions for the abundance portion of the model. All GLM(M)s also
allow for Gaussian and zero-inflated Gaussian models. Note the two
functions for fitting spatailly-varying coefficient models are only
available for Gaussian and zero-inflated Gaussian models.
allow for Gaussian and zero-inflated Gaussian models. Note the
multi-species spatailly-varying coefficient models are only available
for Gaussian and zero-inflated Gaussian models.

## Example usage

Expand Down Expand Up @@ -158,7 +160,7 @@ summary(out)
#> Thinning Rate: 5
#> Number of Chains: 3
#> Total Posterior Samples: 6000
#> Run Time (min): 0.7641
#> Run Time (min): 0.9219
#>
#> Abundance (log scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
Expand Down Expand Up @@ -248,20 +250,24 @@ hierarchical distance sampling models, N-mixture models, and generalized
linear mixed models in `spAbundance`, respectively. Given the similarity
in syntax to fitting occupancy models in the `spOccupancy` package, much
of the documentation on the [`spOccupancy`
website](https://www.jeffdoser.com/files/spoccupancy-web/) will also be
helpful for fitting models in `spAbundance`.
website](https://www.doserlab.com/files/spoccupancy-web/) will also be
helpful for fitting models in `spAbundance`. Please also consider
joining the [`spAbundance` and `spOccupancy` users google
group](https://groups.google.com/g/spocc-spabund-users) to learn from
others who use the two packages.

## Citing `spAbundance`

Please cite `spAbundance` as:

Doser, J. W., Finley, A. O., Kéry, M., and Zipkin, E. F. (2023).
spAbundance: An R package for single-species and multi-species
spatially-explicit abundance models. arXiv Preprint.
Doser, J. W., Finley A. O., Kéry, M., & Zipkin E. F. (2024).
spAbundance: An R package for single-species and multi-species spatially
explicit abundance models, Methods in Ecology and Evolution. 15,
1024-1033. <https://doi.org/10.1111/2041-210X.14332>“)

## References

Doser, J. W., Finley, A. O., Kéry, M., and Zipkin, E. F. (2022).
spOccupancy: An R package for single-species, multi-species, and
integrated spatial occupancy models. Methods in Ecology and Evolution.
<https://doi.org/10.1111/2041-210X.13897>.
integrated spatial occupancy models. Methods in Ecology and Evolution,
13(8), 1670-1678. <https://doi.org/10.1111/2041-210X.13897>.
34 changes: 25 additions & 9 deletions inst/CITATION
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
if (!exists("meta") || is.null(meta)) meta <- packageDescription("spOccupancy")
if (!exists("meta") || is.null(meta)) meta <- packageDescription("spAbundance")

year <- sub(".*(2[[:digit:]]{3})-.*", "\\1", meta$Date, perl = TRUE)
vers <- paste("R package version", meta$Version)
citHeader("To cite package", sQuote(meta$Package), "in publications, please cite the Doser, Finley, Kéry, Zipkin (2024) paper. If using functions lfMsAbund, sfMsAbund, lfMsDS, or sfMsDS, please also cite the Doser, Finley, Banerjee (2023) paper that developed much of the code used for these functions.")

bibentry(bibtype = "Article",
title = "{spAbundance: An R package for univariate and multivariate spatially-explicit abundance models}",
author = c(as.person("Jeffrey W. Doser"), as.person("Andrew O. Finley"), as.person("Marc Kéry"), as.person("Elise F. Zipkin")),
journal = "arXiv Preprint",
journal = "Methods in Ecology and Evolution",
year = "2024",
volume = "15",
number = "",
pages = "1024-1033",
url = "https://doi.org/10.1111/2041-210X.14332",

textVersion =
paste("Doser, J. W., Finley A. O., Kéry, M., & Zipkin E. F. (2024).",
"spAbundance: An R package for single-species and multi-species spatially explicit abundance models",
"Methods in Ecology and Evolution, 15, 1024-1033. https://doi.org/10.1111/2041-210X.14332")
)

bibentry(bibtype = "Article",
title = "Joint species distribution models with imperfect detection for high-dimensional spatial data",
author = c(as.person("Jeffrey W. Doser"), as.person("Andrew O. Finley"), as.person("Sudipto Banerjee")),
journal = "Ecology",
year = "2023",
volume = "",
number = "",
pages = "",
url = "",
pages = "e4137",
url = "https://doi.org/10.1002/ecy.4137",

textVersion =
paste("Doser, J. W., Finley A. O., Kéry, M., & Zipkin E. F. (2022).",
"spAbundance: An R package for univariate and multivariate spatially-explicit abundance models",
"arXiv Preprint.")
paste("Doser, J. W., Finley A. O., & Banerjee, S. (2023).",
"Joint species distribution models with imperfect detection for high-dimensional spatial data",
"Ecology e4137. https://doi.org/10.1002/ecy.4137")
)

4 changes: 2 additions & 2 deletions src/spAbundGaussianNNGP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ extern "C" {
for (j = 0; j < J; j++) {
if (XRE[betaStarIndx[l] * J + j] == betaLevelIndx[l]) {
tmp_02 = 0.0;
for (ll = 0; ll < pRE; ll++) {
for (ll = 0; ll < pRE; ll++) {
tmp_02 += betaStar[betaStarLongIndx[ll * J + j]] * XRandom[ll * J + j];
}
}
tmp_one[0] += XRandom[betaStarIndx[l] * J + j] * (y[j] - F77_NAME(ddot)(&p, &X[j], &J, beta, &inc) -
tmp_02 + (betaStar[l] * XRandom[betaStarIndx[l] * J + j]) -
w[j]) / tauSq;
Expand Down
2 changes: 1 addition & 1 deletion src/spAbundNNGP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ extern "C" {
// Proposal
for (k = 0; k < pAbund; k++) {
logPostBetaCand = 0.0;
logPostBetaCurr = 0.0;
logPostBetaCurr = 0.0;
betaCand[k] = rnorm(beta[k], exp(tuning[betaAMCMCIndx + k]));
logPostBetaCand += dnorm(betaCand[k], muBeta[k], sqrt(SigmaBeta[k * pAbund + k]), 1);
logPostBetaCurr += dnorm(beta[k], muBeta[k], sqrt(SigmaBeta[k * pAbund + k]), 1);
Expand Down
10 changes: 4 additions & 6 deletions src/svcAbundGaussianNNGP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ extern "C" {
/********************************************************************
*Update random effects
*******************************************************************/
// TODO: The random slopes component of this does not work.
if (pRE > 0) {
// Update each individual random effect one by one.
for (l = 0; l < nRE; l++) {
Expand All @@ -455,13 +454,12 @@ extern "C" {
for (j = 0; j < J; j++) {
if (XRE[betaStarIndx[l] * J + j] == betaLevelIndx[l]) {
tmp_02 = 0.0;
for (ll = 0; ll < pRE; ll++) {
for (ll = 0; ll < pRE; ll++) {
tmp_02 += betaStar[betaStarLongIndx[ll * J + j]] * XRandom[ll * J + j];
}
}
tmp_one[0] += XRandom[betaStarIndx[l] * J + j] * (y[j] - F77_NAME(ddot)(&p, &X[j], &J, beta, &inc) -
tmp_02 + (betaStar[l] * XRandom[betaStarIndx[l] * J + j]) -
wSites[j]) / tauSq;
// TODO: check this out. This matches postHocLM.
tmp_02 + (betaStar[l] * XRandom[betaStarIndx[l] * J + j]) -
wSites[j]) / tauSq;
tmp_0 += XRandom[betaStarIndx[l] * J + j] * XRandom[betaStarIndx[l] * J + j] / tauSq;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/svcAbundNNGP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ extern "C" {
e += B[ll * nIndx + nnIndxLU[jj]+i]*wCand[(nnIndx[nnIndxLU[jj]+i]) * pTilde + ll];
}
b = wCand[jj * pTilde + ll] - e;
a += b*b/F[ll * J + j];
a += b*b/F[ll * J + jj];
}
}
// MVN for j
Expand Down
1 change: 0 additions & 1 deletion src/svcAbundNNGPPredict.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: there is a memory problem here at 255 and 260 for spAbund type models (at least).
#define USE_FC_LEN_T
#include <string>
#include "util.h"
Expand Down

0 comments on commit c6ba404

Please sign in to comment.