From c6ba4049d81182d415045c329ba779a59628efd8 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 25 Sep 2024 17:18:50 -0400 Subject: [PATCH] fix svcAbund --- DESCRIPTION | 5 +++-- README.Rmd | 12 ++++++------ README.html | 38 ++++++++++++++++++++---------------- README.md | 36 ++++++++++++++++++++-------------- inst/CITATION | 34 +++++++++++++++++++++++--------- src/spAbundGaussianNNGP.cpp | 4 ++-- src/spAbundNNGP.cpp | 2 +- src/svcAbundGaussianNNGP.cpp | 10 ++++------ src/svcAbundNNGP.cpp | 2 +- src/svcAbundNNGPPredict.cpp | 1 - 10 files changed, 84 insertions(+), 60 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d0fd55a..44a6485 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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) and Finley, Datta, and Banerjee (2022) . 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 ) and hierarchical distance sampling models (Royle, Dawson, Bates (2004) ). 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: diff --git a/README.Rmd b/README.Rmd index 9d12bd8..29375e3 100644 --- a/README.Rmd +++ b/README.Rmd @@ -11,13 +11,13 @@ knitr::opts_chunk$set( out.width = "100%") ``` -# spAbundance +# spAbundance [![](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 @@ -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 @@ -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. diff --git a/README.html b/README.html index 536201d..799c856 100644 --- a/README.html +++ b/README.html @@ -604,9 +604,9 @@

spAbundance -

-

-CRAN + +

+CRAN @@ -635,12 +635,13 @@

spAbundance 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 +analogous syntax to its “sister package” spOccupancy (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.

+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.

Installation

You can install the released version of spAbundance from CRAN with

@@ -767,9 +768,9 @@

Functionality

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

Load package and data

To get started with spAbundance we load the package and @@ -838,7 +839,7 @@

Fit a #> 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 @@ -913,19 +914,22 @@

Learn more

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 +the spOccupancy website will also be helpful for fitting models in -spAbundance.

+spAbundance. Please also consider joining the spAbundance +and spOccupancy users google group 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.

diff --git a/README.md b/README.md index 3dd3f1c..40b5ca4 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -# spAbundance +# spAbundance [![](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 @@ -14,11 +14,13 @@ 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 @@ -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 @@ -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 @@ -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. “) ## 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. -. +integrated spatial occupancy models. Methods in Ecology and Evolution, +13(8), 1670-1678. . diff --git a/inst/CITATION b/inst/CITATION index 188b7db..e93231a 100644 --- a/inst/CITATION +++ b/inst/CITATION @@ -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") ) + diff --git a/src/spAbundGaussianNNGP.cpp b/src/spAbundGaussianNNGP.cpp index 26dcd50..d1f7ee9 100644 --- a/src/spAbundGaussianNNGP.cpp +++ b/src/spAbundGaussianNNGP.cpp @@ -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; diff --git a/src/spAbundNNGP.cpp b/src/spAbundNNGP.cpp index 32cf4f3..b42a4a8 100755 --- a/src/spAbundNNGP.cpp +++ b/src/spAbundNNGP.cpp @@ -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); diff --git a/src/svcAbundGaussianNNGP.cpp b/src/svcAbundGaussianNNGP.cpp index acf4168..6389e1e 100644 --- a/src/svcAbundGaussianNNGP.cpp +++ b/src/svcAbundGaussianNNGP.cpp @@ -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++) { @@ -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; } } diff --git a/src/svcAbundNNGP.cpp b/src/svcAbundNNGP.cpp index 36b6e4d..8cb65b6 100755 --- a/src/svcAbundNNGP.cpp +++ b/src/svcAbundNNGP.cpp @@ -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 diff --git a/src/svcAbundNNGPPredict.cpp b/src/svcAbundNNGPPredict.cpp index 1cc0ec1..c244afe 100644 --- a/src/svcAbundNNGPPredict.cpp +++ b/src/svcAbundNNGPPredict.cpp @@ -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 #include "util.h"