diff options
author | Ken Kellner <ken@kenkellner.com> | 2023-12-07 16:05:30 -0500 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2023-12-07 16:05:30 -0500 |
commit | d7b75922602170e5240b9cfab12fd8cc11c8d23a (patch) | |
tree | a06068abb2dbe85dc1b5ca380b8b18b5bbd16cb7 | |
parent | 79265cea70a8d8278bcd5b2405292988477c247b (diff) |
More autojags tests
-rw-r--r-- | R/autojags.R | 2 | ||||
-rw-r--r-- | inst/tinytest/autojags_ref_alliter.Rds | bin | 0 -> 34974 bytes | |||
-rw-r--r-- | inst/tinytest/test_autojags.R | 90 | ||||
-rw-r--r-- | inst/tinytest/test_mcmc_tools.R | 18 |
4 files changed, 109 insertions, 1 deletions
diff --git a/R/autojags.R b/R/autojags.R index 74da81d..035f9c9 100644 --- a/R/autojags.R +++ b/R/autojags.R @@ -7,7 +7,7 @@ autojags <- function(data,inits=NULL,parameters.to.save,model.file,n.chains,n.ad stop("The seed argument is no longer supported, use set.seed() instead", call.=FALSE) } if(n.chains<2) stop('Number of chains must be >1 to calculate Rhat.') - if((max.iter < n.burnin) & verbose){ + if(max.iter < n.burnin){ old_warn <- options()$warn options(warn=1) warning('Maximum iterations includes burn-in and should be larger than burn-in.') diff --git a/inst/tinytest/autojags_ref_alliter.Rds b/inst/tinytest/autojags_ref_alliter.Rds Binary files differnew file mode 100644 index 0000000..b85d544 --- /dev/null +++ b/inst/tinytest/autojags_ref_alliter.Rds diff --git a/inst/tinytest/test_autojags.R b/inst/tinytest/test_autojags.R index d8b6b84..5753cc8 100644 --- a/inst/tinytest/test_autojags.R +++ b/inst/tinytest/test_autojags.R @@ -53,3 +53,93 @@ expect_inherits(out, "jagsUIbasic") expect_equal(coda::varnames(out$samples), c("alpha","beta", "sigma", paste0("mu[",1:16,"]"),"deviance")) expect_equal(names(out), c("samples", "model")) + +# Save all iterations---------------------------------------------------------- +set.seed(123) +params <- c('alpha','beta','sigma', 'mu') + +nul <- capture.output( + out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.burnin=50, + iter.increment=10, n.thin = 1, verbose=TRUE, save.all.iter=TRUE)) + +# 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) +ref <- readRDS("autojags_ref_alliter.Rds") +out$mcmc.info$elapsed.mins <- ref$mcmc.inf$elapsed.mins +expect_identical(out[-c(17,18,21)], ref[-c(17,18,21)]) + +# Parallel---------------------------------------------------------- +at_home <- identical( Sys.getenv("AT_HOME"), "TRUE" ) +if(at_home){ + set.seed(123) + nul <- capture.output( + out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.burnin=50, + iter.increment=10, n.thin = 2, verbose=TRUE, parallel=TRUE, save.all.iter=FALSE)) + # Runs two updates of 40 iterations each + expect_true(grepl("Update 13", nul[18])) + expect_equal(nul[19], "") + + ref <- readRDS("autojags_ref.Rds") + + out$mcmc.info$elapsed.mins <- ref$mcmc.inf$elapsed.mins + expect_identical(out[-c(17,18,20:22)], ref[-c(17,18,20:22)]) + + # Save all iter + set.seed(123) + params <- c('alpha','beta','sigma', 'mu') + + nul <- capture.output( + out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.burnin=50, + iter.increment=10, n.thin = 1, verbose=TRUE, parallel=TRUE, save.all.iter=TRUE)) + expect_true(grepl("Update 3", nul[10])) + expect_equal(nul[11], "") + + expect_equal(coda::niter(out$samples), 30) + ref <- readRDS("autojags_ref_alliter.Rds") + expect_identical(out[-c(17,18,20:22)], ref[-c(17,18,20:22)]) +} + +# test.Rhat-------------------------------------------------------------------- +samples <- readRDS('coda_samples.Rds') + +rhats <- sapply(1:coda::nvar(samples), function(i) coda::gelman.diag(samples[,i], + autoburnin=FALSE)$psrf[1]) + +# None above 1.1 +expect_false(any(rhats > 1.1)) +expect_false(jagsUI:::test.Rhat(samples, 1.1, params.omit=NULL, verbose=FALSE)) + +# Sigma is above 1.05 +expect_true(any(rhats > 1.05)) +expect_true(jagsUI:::test.Rhat(samples, 1.05, params.omit=NULL, verbose=FALSE)) + +nul <- capture.output(jagsUI:::test.Rhat(samples, 1.05, params.omit=NULL, verbose=TRUE)) +expect_true(grepl("sigma", nul[1])) + +# Exclude sigma +expect_false(jagsUI:::test.Rhat(samples, 1.05, params.omit="sigma", verbose=FALSE)) + +# Test input errors------------------------------------------------------------ +expect_error(out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.burnin=50, + iter.increment=10, n.thin = 2, verbose=FALSE, seed=123)) + +expect_error(out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 1, n.adapt = 100, n.burnin=50, + iter.increment=10, n.thin = 2, verbose=FALSE)) + +expect_warning(out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.burnin=50, + iter.increment=10, n.thin = 2, verbose=FALSE, max.iter=40)) + +nul <- capture.output(out <- autojags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.burnin=0, + iter.increment=10, n.thin = 2, verbose=TRUE, max.iter=5)) +expect_equal(nul[8], "Maximum iterations reached.") diff --git a/inst/tinytest/test_mcmc_tools.R b/inst/tinytest/test_mcmc_tools.R index 8e5f9cb..73871f9 100644 --- a/inst/tinytest/test_mcmc_tools.R +++ b/inst/tinytest/test_mcmc_tools.R @@ -89,3 +89,21 @@ expect_equal(get_inds('kappa',params_raw), params_raw <- 'alpha' expect_warning(test <- get_inds('alpha',params_raw)[1,1]) expect_true(is.na(test)) + +# test that bind.mcmc works correctly------------------------------------------ +cs1 <- readRDS('coda_samples.Rds') +cs2 <- readRDS('coda_samples.Rds') + +iter_increment <- coda::niter(cs1) * coda::thin(cs1) +test <- jagsUI:::bind.mcmc(cs1, cs2, stats::start(cs1), iter_increment) +expect_equal(coda::niter(test), 60) +expect_equal(coda::varnames(test), coda::varnames(cs1)) +expect_equal(stats::start(cs1), stats::start(test)) +expect_equal(stats::end(test), stats::end(cs1) + iter_increment) + +comb <- rbind( + rbind(as.matrix(cs1[[1]]), as.matrix(cs2[[1]])), + rbind(as.matrix(cs1[[2]]), as.matrix(cs2[[2]])), + rbind(as.matrix(cs1[[3]]), as.matrix(cs2[[3]])) +) +expect_identical(comb, as.matrix(test)) |