tests/testthat/test-sampler.R

context("mcmc sampler")
test_that("testing mcmc sampler", {
  data <- tempo_simulate(n = 30, t = 100,
                         censor_intensity = 3,
                         beta_mu = c(-5, 0.5, -1, 0.3),
                         random_betas = NULL,
                         beta_sd = NULL,
                         covariates = NULL,
                         n_group = NULL,
                         rho = NULL)

  draws <- tempo_mcmc(
    y = data$y,
    covariates = data$covariates,
    beta_start = c(-5, 0, 0, 0),
    n_iter = 100,
    n_burnin = 10,
    n_workers = 2,
    n_chains = 3,
    pp_checks = NA
  )
  expect_equal(dim(draws[[1]]), c(100, 4)) # 4 covariates, 100 iters

  draws2 <- tempo_mcmc(
    y = data$y,
    covariates = data$covariates,
    beta_start = c(-5, 0, 0, 0),
    n_iter = 100,
    n_burnin = 10,
    group_ids = round(runif(30, 1, 3)),
    random_effects = c("intercept", "covariate2"),
    sd_eps_start = c(0.1, 0.1)
  )
  expect_equal(dim(draws2[[1]]), c(100, 7))

  expect_error(
    tempo_mcmc(
      y = data$y,
      covariates = data$covariates,
      beta_start = c(-5, 0, 0, 0),
      n_iter = 100,
      n_burnin = 10,
      group_ids = round(runif(30, 1, 3)),
      random_effects = c("intercept", "covariate1",
                         "covariate2", "covariate3"),
      sd_eps_start = c(0.1, 0.1, 0.1, 0.1),
      correlated =  FALSE,
      n_chains = 1
    ),
    NA
  )

  expect_error(
    tempo_mcmc(
      y = data$y,
      covariates = data$covariates,
      beta_start = c(-5, 0, 0, 0),
      n_iter = 100,
      n_burnin = 10,
      group_ids = round(runif(30, 1, 3)),
      random_effects = c("intercept"),
      sd_eps_start = c(0.1),
      correlated =  FALSE,
      monitor_random_effects = TRUE
    ),
    NA
  )

  expect_error(
    tempo_mcmc(
      y = data$y,
      covariates = data$covariates,
      beta_start = c(-5, 0, 0, 0),
      n_iter = 100,
      n_burnin = 10,
      group_ids = round(runif(30, 1, 3)),
      random_effects = c("intercept", "covariate1", "covariate2"),
      sd_eps_start = c(0.1, 0.1, 0.1),
      correlated =  FALSE
    ),
    NA
  )
  expect_error(
    tempo_mcmc(
      y = data$y,
      covariates = data$covariates,
      beta_start = c(-5, 0, 0, 0),
      n_iter = 100,
      n_burnin = 10,
      group_ids = round(runif(30, 1, 3)),
      random_effects = c("intercept", "covariate1", "covariate2"),
      sd_eps_start = c(0.1, 0.1, 0.1),
      correlated =  TRUE,
      monitor_random_effects = TRUE
    ),
    NA
  )


  ## Test some error/warning throw for tempo_derive.R and diag functions
  ## (put here so new samplers don't need to be run)
  expect_error(tempo_derive(draws2, quantity = "pdf", obs_idx = 3))
  expect_error(tempo_calc_group_betas(draws2))
  expect_warning(tempo_psrf(draws2))
  expect_error(tempo_dic(draws2))
  expect_error(tempo_calc_group_betas(draws))
})
vlandau/tempo documentation built on March 18, 2020, 12:04 a.m.