diff --git a/Makefile b/Makefile index 552aa53..ec75d24 100644 --- a/Makefile +++ b/Makefile @@ -19,13 +19,6 @@ test: document: Rscript -e "devtools::document()" -README: - Rscript -e "rmarkdown::render('README.Rmd')" - pandoc README.md -o README.html - firefox README.html - sleep 3 - rm README.html - vignettes: vignettes/random-effects.Rmd vignettes/JAGS-comparison.Rmd vignettes/spatial-models.Rmd vignettes/grouse-example.Rmd touch test.png; mv *.png vignettes; rm vignettes/test.png Rscript -e "devtools::build_vignettes()" diff --git a/README.Rmd b/README.Rmd deleted file mode 100644 index 7905901..0000000 --- a/README.Rmd +++ /dev/null @@ -1,127 +0,0 @@ ---- -output: - md_document: - variant: gfm ---- - -```{r, echo = FALSE} -knitr::opts_chunk$set( - fig.path = "README_figs/README-" -) -``` - -# ubms: Unmarked Bayesian Models with Stan - - -[![R build status](https://github.com/kenkellner/ubms/workflows/R-CMD-check/badge.svg)](https://github.com/kenkellner/ubms/actions) -[![Codecov test coverage](https://codecov.io/gh/kenkellner/ubms/branch/master/graph/badge.svg)](https://codecov.io/gh/kenkellner/ubms?branch=master) -[![CRAN status](https://www.r-pkg.org/badges/version/ubms)](https://cran.r-project.org/web/packages/ubms/index.html) - - -`ubms` is an `R` package for fitting Bayesian hierarchical models of animal occurrence and abundance. -The package has a formula-based interface compatible with [unmarked](https://cran.r-project.org/web/packages/unmarked/index.html), but the model is fit using MCMC with [Stan](https://mc-stan.org/) instead of using maximum likelihood. -Currently there are Stan versions of unmarked functions `occu`, `occuRN`, `colext`, `occuTTD`, `pcount`, `distsamp`, and `multinomPois`. -These functions follow the `stan_` prefix naming format established by [rstanarm](https://cran.r-project.org/web/packages/rstanarm/index.html). For example, the Stan version of the `unmarked` function `occu` is `stan_occu`. - -Advantages compared to `unmarked`: - - 1. Obtain posterior distributions of parameters and derived parameters - 2. Include random effects in parameter formulas (same syntax as `lme4`) - 3. Assess model fit using WAIC and LOO via the [loo](https://cran.r-project.org/web/packages/loo/index.html) package - -Disadvantages compared to `unmarked`: - - 1. MCMC is slower than maximum likelihood - 2. Not all model types are supported - 3. Potential for convergence issues - -## Installation - -`ubms` is on [CRAN](https://cran.r-project.org/web/packages/ubms/index.html): - -```{r, eval=FALSE} -install.packages("ubms") -``` - -Alternatively, the latest development version can be installed from Github: - -```{r, eval=FALSE} -# install.packages("devtools") -devtools::install_github("kenkellner/ubms") -``` - -## Example - -Simulate occupancy data including a random effect on occupancy: - -```{r,message=FALSE} -library(ubms) - -set.seed(123) -dat_occ <- data.frame(x1=rnorm(500)) -dat_p <- data.frame(x2=rnorm(500*5)) - -y <- matrix(NA, 500, 5) -z <- rep(NA, 500) - -b <- c(0.4, -0.5, 0.3, 0.5) - -re_fac <- factor(sample(letters[1:26], 500, replace=T)) -dat_occ$group <- re_fac -re <- rnorm(26, 0, 1.2) -re_idx <- as.numeric(re_fac) - -idx <- 1 -for (i in 1:500){ - z[i] <- rbinom(1,1, plogis(b[1] + b[2]*dat_occ$x1[i] + re[re_idx[i]])) - for (j in 1:5){ - y[i,j] <- z[i]*rbinom(1,1, - plogis(b[3] + b[4]*dat_p$x2[idx])) - idx <- idx + 1 - } -} -``` - -Create `unmarked` frame: - -```{r} -umf <- unmarkedFrameOccu(y=y, siteCovs=dat_occ, obsCovs=dat_p) -``` - -Fit a model with a random intercept, using 3 parallel cores: - -```{r, eval=FALSE} -(fm <- stan_occu(~x2 ~x1 + (1|group), umf, chains=3, cores=3, seed=123)) -``` - -```{r, echo=FALSE, warning=FALSE} -(fm <- stan_occu(~x2 ~x1 + (1|group), umf, chains=3, cores=3, refresh=0, seed=123)) -``` - - -Examine residuals for occupancy and detection submodels (following [Wright et al. 2019](https://doi.org/10.1002/ecy.2703)). -Each panel represents a draw from the posterior distribution. - -```{r resids, fig.height=7} -plot(fm) -``` - -Assess model goodness-of-fit with a posterior predictive check, using the MacKenzie-Bailey chi-square test: - -```{r, eval=FALSE} -fm_fit <- gof(fm, draws=500) -plot(fm_fit) -``` - -```{r gof, echo=FALSE} -fm_fit <- gof(fm, draws=500, quiet=TRUE) -plot(fm_fit) -``` - -Look at the marginal effect of `x2` on detection: - -```{r marginal} -plot_effects(fm, "det") -``` - -A more detailed vignette for the package is available [here](https://kenkellner.com/ubms/articles/ubms.html). diff --git a/README.md b/README.md index 5dfa0d9..31b3300 100644 --- a/README.md +++ b/README.md @@ -54,96 +54,6 @@ Github: devtools::install_github("kenkellner/ubms") ``` -## Example +## Package Overview -Simulate occupancy data including a random effect on occupancy: - -``` r -library(ubms) - -set.seed(123) -dat_occ <- data.frame(x1=rnorm(500)) -dat_p <- data.frame(x2=rnorm(500*5)) - -y <- matrix(NA, 500, 5) -z <- rep(NA, 500) - -b <- c(0.4, -0.5, 0.3, 0.5) - -re_fac <- factor(sample(letters[1:26], 500, replace=T)) -dat_occ$group <- re_fac -re <- rnorm(26, 0, 1.2) -re_idx <- as.numeric(re_fac) - -idx <- 1 -for (i in 1:500){ - z[i] <- rbinom(1,1, plogis(b[1] + b[2]*dat_occ$x1[i] + re[re_idx[i]])) - for (j in 1:5){ - y[i,j] <- z[i]*rbinom(1,1, - plogis(b[3] + b[4]*dat_p$x2[idx])) - idx <- idx + 1 - } -} -``` - -Create `unmarked` frame: - -``` r -umf <- unmarkedFrameOccu(y=y, siteCovs=dat_occ, obsCovs=dat_p) -``` - -Fit a model with a random intercept, using 3 parallel cores: - -``` r -(fm <- stan_occu(~x2 ~x1 + (1|group), umf, chains=3, cores=3, seed=123)) -``` - - ## - ## Call: - ## stan_occu(formula = ~x2 ~ x1 + (1 | group), data = umf, chains = 3, - ## cores = 3, refresh = 0, seed = 123) - ## - ## Occupancy (logit-scale): - ## Estimate SD 2.5% 97.5% n_eff Rhat - ## (Intercept) 0.318 0.290 -0.254 0.887 926 1.001 - ## x1 -0.461 0.114 -0.683 -0.237 4428 0.999 - ## sigma [1|group] 1.337 0.261 0.911 1.924 2112 1.002 - ## - ## Detection (logit-scale): - ## Estimate SD 2.5% 97.5% n_eff Rhat - ## (Intercept) 0.383 0.0607 0.265 0.503 4539 1 - ## x2 0.586 0.0591 0.470 0.704 4963 1 - ## - ## LOOIC: 2267.291 - ## Runtime: 27.948 sec - -Examine residuals for occupancy and detection submodels (following -[Wright et al. 2019](https://doi.org/10.1002/ecy.2703)). Each panel -represents a draw from the posterior distribution. - -``` r -plot(fm) -``` - -![](README_figs/README-resids-1.png) - -Assess model goodness-of-fit with a posterior predictive check, using -the MacKenzie-Bailey chi-square test: - -``` r -fm_fit <- gof(fm, draws=500) -plot(fm_fit) -``` - -![](README_figs/README-gof-1.png) - -Look at the marginal effect of `x2` on detection: - -``` r -plot_effects(fm, "det") -``` - -![](README_figs/README-marginal-1.png) - -A more detailed vignette for the package is available -[here](https://kenkellner.com/blog/ubms-vignette.html). +A detailed vignette for the package is available [here](https://cran.r-project.org/web/packages/ubms/vignettes/ubms.html). diff --git a/README_figs/README-gof-1.png b/README_figs/README-gof-1.png deleted file mode 100644 index 139a11b..0000000 Binary files a/README_figs/README-gof-1.png and /dev/null differ diff --git a/README_figs/README-marginal-1.png b/README_figs/README-marginal-1.png deleted file mode 100644 index b02f284..0000000 Binary files a/README_figs/README-marginal-1.png and /dev/null differ diff --git a/README_figs/README-resids-1.png b/README_figs/README-resids-1.png deleted file mode 100644 index 9ac07b5..0000000 Binary files a/README_figs/README-resids-1.png and /dev/null differ