diff options
author | Ken Kellner <ken@kenkellner.com> | 2023-12-02 19:59:49 -0500 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2023-12-02 19:59:49 -0500 |
commit | 991ed72c18ce8103e00fea29280835855a1870eb (patch) | |
tree | 2ac28cfc0b58722c96cbcbe893ddcf02300a5981 | |
parent | 8553f9fd5b4628265e165b0868d7576b9b7734b0 (diff) |
jags() test
-rw-r--r-- | inst/tinytest/longley_reference_fit.Rds | bin | 0 -> 237657 bytes | |||
-rw-r--r-- | inst/tinytest/test_jags.R | 53 |
2 files changed, 53 insertions, 0 deletions
diff --git a/inst/tinytest/longley_reference_fit.Rds b/inst/tinytest/longley_reference_fit.Rds Binary files differnew file mode 100644 index 0000000..328069b --- /dev/null +++ b/inst/tinytest/longley_reference_fit.Rds diff --git a/inst/tinytest/test_jags.R b/inst/tinytest/test_jags.R new file mode 100644 index 0000000..f7cec7a --- /dev/null +++ b/inst/tinytest/test_jags.R @@ -0,0 +1,53 @@ +set.seed(123) + +data(longley) +data <- list(gnp=longley$GNP, employed=longley$Employed, n=nrow(longley)) + +modfile <- tempfile() +writeLines(" +model{ + for (i in 1:n){ + employed[i] ~ dnorm(mu[i], tau) + mu[i] <- alpha + beta*gnp[i] + } + alpha ~ dnorm(0, 0.00001) + beta ~ dnorm(0, 0.00001) + sigma ~ dunif(0,1000) + tau <- pow(sigma,-2) +}", con=modfile) + +inits <- function(){ + list(alpha=rnorm(1,0,1),beta=rnorm(1,0,1),sigma=runif(1,0,3)) +} +params <- c('alpha','beta','sigma', 'mu') + +out <- jags(data = data, inits = inits, parameters.to.save = params, + model.file = modfile, n.chains = 3, n.adapt = 100, n.iter = 1000, + n.burnin = 500, n.thin = 2, verbose=FALSE) + +ref <- readRDS("longley_reference_fit.Rds") + +# Remove time/date based elements +out$mcmc.info$elapsed.mins <- ref$mcmc.inf$elapsed.mins +expect_identical(out[-c(17,18,21)], ref[-c(17,18,21)]) + +# Plots +pdf(NULL) +tp <- traceplot(out, ask=FALSE) +dev.off() +expect_true(is.null(tp)) + +pdf(NULL) +dp <- densityplot(out, ask=FALSE) +dev.off() +expect_true(is.null(dp)) + +pdf(NULL) +wp <- whiskerplot(out, "mu") +dev.off() +expect_true(is.null(wp)) + +pdf(NULL) +pp <- pp.check(out, "alpha", "beta") +dev.off() +expect_equal(pp, 0) |