tests/testthat/test-jags.parallel.R

test_that("fewer cores than chains", {
  model <- "model {
    for (i in 1:n) {
      y[i] ~ dnorm(x[i] * beta, 1)
    }
    beta ~ dnorm(0, 1)
  }"
  model_file <- tempfile()
  writeLines(model, model_file)
  data <- list(
    n = 10,
    x = rnorm(10),
    y = rnorm(10)
  )
  tmp <- capture.output(
    out <- jags.parallel(
      data,
      parameters.to.save = "beta",
      model.file = model_file,
      n.chains = 4,
      n.cluster = 2
    )
  )
  last_values_1 <- unlist(out$BUGSoutput$last.values)
  expect_equal(length(unique(last_values_1)), 4L)
  expect_true(inherits(out, "rjags"))
  expect_true(inherits(out$BUGSoutput, "bugs"))
  tmp <- capture.output(
    out <- jags.parallel(
      data,
      parameters.to.save = "beta",
      model.file = model_file,
      n.chains = 4,
      n.cluster = 2
    )
  )
  last_values_2 <- unlist(out$BUGSoutput$last.values)
  expect_true(inherits(out, "rjags"))
  expect_true(inherits(out$BUGSoutput, "bugs"))
  expect_equal(last_values_1, last_values_2)
})

Try the R2jags package in your browser

Any scripts or data that you put into this service are public.

R2jags documentation built on May 29, 2024, 11:47 a.m.