knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "tools/README-" )
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.
library(jmbr) library(mbr)
# 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) analysis <- reanalyse(analysis) coef(analysis, simplify = TRUE) 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)
To install from GitHub
install.packages("devtools") devtools::install_github("poissonconsulting/jmbr")
citation(package = "jmbr")
Please report any issues.
Pull requests are always welcome.
Please note that the jmbr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.