An R package to facilitate analyses using JAGS
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R
inst
man
tests
tools
.Rbuildignore
.gitignore
.travis.yml devel version to see if fix travis package 'rjags' was installed by a… Oct 10, 2018
CONDUCT.md
DESCRIPTION
LICENSE
LICENSE.md
NAMESPACE
README.Rmd
README.md
jmbr.Rproj

README.md

lifecycle Travis-CI Build Status codecov License: MIT DOI

jmbr

Introduction

jmbr (pronounced jimber) is an R package to facilitate analyses using Just Another Gibbs Sampler (JAGS).

It is part of the mbr family of packages.

Demonstration

library(jmbr)
# define model in JAGS language
model <- model("model {
  alpha ~ dnorm(0, 10^-2)
  beta1 ~ dnorm(0, 10^-2)
  beta2 ~ dnorm(0, 10^-2)
  beta3 ~ dnorm(0, 10^-2)

  log_sAnnual ~ dnorm(0, 10^-2)
  log(sAnnual) <- log_sAnnual

  for(i in 1:nAnnual) {
    bAnnual[i] ~ dnorm(0, sAnnual^-2)
  }

  for (i in 1:length(Pairs)) {
    log(ePairs[i]) <- alpha + beta1 * Year[i] + beta2 * Year[i]^2 + beta3 * Year[i]^3 + bAnnual[Annual[i]]
    Pairs[i] ~ dpois(ePairs[i])
  }
}")

# add R code to calculate derived parameters
model <- update_model(model, new_expr = "
for (i in 1:length(Pairs)) {
  log(prediction[i]) <- alpha + beta1 * Year[i] + beta2 * Year[i]^2 + beta3 * Year[i]^3 + bAnnual[Annual[i]]
}")

# define data types and center year
model <- update_model(model, 
  select_data = list("Pairs" = integer(), "Year*" = integer(), Annual = factor()),
  derived = "sAnnual",
  random_effects = list(bAnnual = "Annual"))

data <- bauw::peregrine
data$Annual <- factor(data$Year)

set_analysis_mode("report")

# analyse
analysis <- analyse(model, data = data)
#> # A tibble: 1 x 8
#>       n     K nchains niters nthin   ess  rhat converged
#>   <int> <int>   <int>  <int> <int> <int> <dbl> <lgl>    
#> 1    40     5       3    500     1    10  4.62 FALSE
analysis <- reanalyse(analysis)
#> # A tibble: 1 x 8
#>       n     K nchains niters nthin   ess  rhat converged
#>   <int> <int>   <int>  <int> <int> <int> <dbl> <lgl>    
#> 1    40     5       3    500     2    54  1.10 FALSE

coef(analysis)
#> # A tibble: 5 x 7
#>   term        estimate     sd  zscore   lower   upper pvalue
#>   <S3: term>     <dbl>  <dbl>   <dbl>   <dbl>   <dbl>  <dbl>
#> 1 alpha         4.26   0.0429  99.4    4.18    4.34   0.0007
#> 2 beta1         1.19   0.0759  15.8    1.06    1.35   0.0007
#> 3 beta2        -0.0189 0.0321  -0.601 -0.0889  0.0397 0.579 
#> 4 beta3        -0.272  0.0394  -6.92  -0.349  -0.198  0.0007
#> 5 log_sAnnual  -2.23   0.332   -6.84  -3.11   -1.74   0.0007

plot(analysis)

# make predictions by varying year with other predictors including the random effect of Annual held constant
year <- predict(analysis, new_data = "Year")

# plot those predictions
library(ggplot2)

ggplot(data = year, aes(x = Year, y = estimate)) +
  geom_point(data = bauw::peregrine, aes(y = Pairs)) +
  geom_line() +
  geom_line(aes(y = lower), linetype = "dotted") +
  geom_line(aes(y = upper), linetype = "dotted") +
  expand_limits(y = 0)

Installation

To install from GitHub

install.packages("devtools")
devtools::install_github("poissonconsulting/jmbr")

or the Poisson drat Repository

install.packages("drat")
drat::addRepo("poissonconsulting")
install.packages("jmbr")

Citation


To cite jmbr in publications use:

  Joe Thorley (2018) jmbr: Analyses Using JAGS. doi:
  https://doi.org/10.5281/zenodo.1162355.

A BibTeX entry for LaTeX users is

  @Misc{,
    author = {Joe Thorley},
    year = {2018},
    title = {jmbr: Analyses Using JAGS},
    doi = {https://doi.org/10.5281/zenodo.1162355},
  }

Please also cite JAGS.

Contribution

Please report any issues.

Pull requests are always welcome.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Inspiration