tests/testthat/test-single_chain.R

test_that("Single chains work as expected", {
  testthat::skip_if_not_installed("MEMSS")
  data("Machines", package = "MEMSS")

  suppressWarnings(capture.output(
    m_machines_1 <- stanova_lmer(score ~ Machine + (Machine|Worker),
                                 data=Machines, chains = 1, iter = 200)
  ))
  ar <- stanova_samples(m_machines_1, return = "array")
  expect_is(ar, "list")
  expect_length(ar, 2)
  expect_is(ar$`(Intercept)`, "array")
  expect_length(dim(ar$`(Intercept)`), 3)
  expect_equal(dim(ar$Machine), c(100, 3, 1))

  ar2 <- stanova_samples(m_machines_1, return = "array", dimension_chain = 2)
  expect_equal(dim(ar2$Machine), c(100, 1, 3))

  ma <- stanova_samples(m_machines_1, return = "matrix")
  expect_is(ma, "list")
  expect_length(ma, 2)
  expect_is(ma$`(Intercept)`, "matrix")
  expect_length(dim(ma$`(Intercept)`), 2)

  df <- stanova_samples(m_machines_1, return = "data.frame")
  expect_is(df$`(Intercept)`, "data.frame")
  expect_equal(df[["(Intercept)"]]$Chain, rep(1, nrow(df[["(Intercept)"]])))
  expect_equal(df[["(Intercept)"]]$Iteration, seq_len(nrow(df[["(Intercept)"]])))
  expect_equal(df[["(Intercept)"]]$Draw, seq_len(nrow(df[["(Intercept)"]])))
  expect_is(df$Machine, "data.frame")
  expect_equal(df[["Machine"]]$Chain, rep(1, nrow(df[["Machine"]])))
  expect_equal(df[["Machine"]]$Iteration, rep(seq_len(nrow(df[["(Intercept)"]])), 3))
  expect_equal(df[["Machine"]]$Draw, rep(seq_len(nrow(df[["(Intercept)"]])), 3))

})
bayesstuff/stanova documentation built on June 9, 2021, 6:18 p.m.