From ab4d07eb875ea72b577fe9f1bb5d6faf31464ed6 Mon Sep 17 00:00:00 2001 From: martyn Date: Mon, 5 Jan 2026 09:55:11 +0000 Subject: [PATCH] Fixes for JAGS 5.0.0 --- DESCRIPTION | 2 +- R/run_rjags.R | 8 +++++++- inst/tinytest/test_autojags.R | 17 +++++++++++++---- inst/tinytest/test_run_rjags.R | 30 ++++++++++++++++++------------ 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7d6902f..c589cbc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -13,7 +13,7 @@ Imports: graphics, grDevices, parallel, - rjags (>= 3-13), + rjags (>= 4-17), stats, utils Suggests: knitr, markdown, tinytest diff --git a/R/run_rjags.R b/R/run_rjags.R index 0b107f4..db12e13 100644 --- a/R/run_rjags.R +++ b/R/run_rjags.R @@ -281,7 +281,13 @@ set.factories <- function(factories){ set.modules <- function(modules,DIC){ #Load/unload appropriate modules (besides dic) - called.set <- c('basemod','bugs',modules) + + default.set <- c('basemod', 'bugs') + if (rjags::jags.version() >= as.numeric_version("5")) { + #deviance monitor moved to the 'diag' module so load by default + default.set <- c(default.set, 'diag') + } + called.set <- c(default.set, modules) current.set <- rjags::list.modules() load.set <- called.set[!called.set%in%current.set] diff --git a/inst/tinytest/test_autojags.R b/inst/tinytest/test_autojags.R index 61ff997..1b78c4f 100644 --- a/inst/tinytest/test_autojags.R +++ b/inst/tinytest/test_autojags.R @@ -71,10 +71,19 @@ nul <- capture.output( # Runs three updates of 10 iterations each expect_true(grepl("Note: ALL iterations", nul[6])) -expect_true(grepl("Update 3", nul[10])) -expect_true(nul[11] == "") -# All are combined to yield 30 total iterations in each chain -expect_equal(coda::niter(out$samples), 30) +if (rjags::jags.version() >= as.numeric_version("5")) { + expect_true(grepl("Update 2", nul[9])) + expect_true(nul[10] == "") + # All are combined to yield 20 total iterations in each chain + expect_equal(coda::niter(out$samples), 20) + +} else { + expect_true(grepl("Update 3", nul[10])) + expect_true(nul[11] == "") + # All are combined to yield 30 total iterations in each chain + expect_equal(coda::niter(out$samples), 30) +} + if(at_home){ ref <- readRDS("autojags_ref_alliter.Rds") diff --git a/inst/tinytest/test_run_rjags.R b/inst/tinytest/test_run_rjags.R index 65d05da..b42f217 100644 --- a/inst/tinytest/test_run_rjags.R +++ b/inst/tinytest/test_run_rjags.R @@ -1,22 +1,28 @@ # load_modules loads and unloads correctly + +if (rjags::jags.version() < as.numeric_version("5")) { + default.modules <- c('basemod','bugs') + test.factories <- 1:2 +} else { + default.modules <- c('basemod','bugs','diag') # diag provides deviance monitor + test.factories <- c(6,1) # change in precedence of bugs::binomSlice factory +} + jagsUI:::set.modules(NULL, FALSE) -expect_equal(rjags::list.modules(),c('basemod','bugs')) +expect_equal(rjags::list.modules(),default.modules) jagsUI:::set.modules('glm', FALSE) -expect_equal(rjags::list.modules(),c('basemod','bugs','glm')) +expect_equal(rjags::list.modules(),c(default.modules,'glm')) jagsUI:::set.modules(NULL, FALSE) -expect_equal(rjags::list.modules(),c('basemod','bugs')) +expect_equal(rjags::list.modules(),default.modules) jagsUI:::set.modules(NULL, DIC=TRUE) -expect_equal(rjags::list.modules(),c('basemod','bugs','dic')) - +expect_equal(rjags::list.modules(),c(default.modules,'dic')) # load_factories works correctly jagsUI:::set.factories(NULL) -expect_equal(rjags::list.factories('sampler')[1:2,2], c(TRUE, TRUE)) -jagsUI:::set.factories(c('bugs::BinomSlice sampler FALSE', - 'bugs::RW1 sampler FALSE')) -expect_equal(rjags::list.factories('sampler')[1:2,2], c(FALSE, FALSE)) -jagsUI:::set.factories(c('bugs::BinomSlice sampler TRUE', - 'bugs::RW1 sampler TRUE')) -expect_equal(rjags::list.factories('sampler')[1:2,2], c(TRUE, TRUE)) +expect_equal(rjags::list.factories('sampler')[test.factories,2], c(TRUE, TRUE)) +jagsUI:::set.factories(c('bugs::BinomSlice sampler FALSE', 'bugs::RW1 sampler FALSE')) +expect_equal(rjags::list.factories('sampler')[test.factories,2], c(FALSE, FALSE)) +jagsUI:::set.factories(c('bugs::BinomSlice sampler TRUE', 'bugs::RW1 sampler TRUE')) +expect_equal(rjags::list.factories('sampler')[test.factories,2], c(TRUE, TRUE)) expect_error(jagsUI:::set.factories(c('bad bad'))) expect_error(jagsUI:::set.factories(c('bugs::fake sampler FALSE')))