tests/testthat/test-simulate-flip-til-failure.R

test_that("summarize_clusters", {
  inf_params <- c("beta_0" = -2, "beta_1" = 1)
  smear_pos_prob <- .8
  max_size <- 30
  K <- 5

  out <- simulate_flip_til_failure(K = K,
                                   inf_params = inf_params,
                                   smear_pos_prob = smear_pos_prob,
                                   max_size = max_size)

  out2 <- summarize_clusters(out)
  expect_true(nrow(out2) >= 1)


})



test_that("simulate_flip_til_failure", {
  inf_params <- c("beta_0" = -2, "beta_1" = 1)
  smear_pos_prob <- .8
  max_size <- 30
  K <- 5

  out <- simulate_flip_til_failure(K = K,
                          inf_params = inf_params,
                          smear_pos_prob = smear_pos_prob,
                          max_size = max_size)
  expect_equal(length(unique(out$cluster_id)), K)

})




test_that("simulate_flip_til_failure", {

  cluster_id <- 1
  inf_params <- c("beta_0" = -2, "beta_1" = 1)
  smear_pos_prob <- 1
  max_size <- 5

  out <- flip_til_failure(cluster_id = cluster_id,
                                   inf_params = inf_params,
                                   smear_pos_prob = smear_pos_prob,
                                   max_size = max_size)
  expect_true(nrow(out) <= max_size)

  #######################
  ######################### High chance of infeaciton
  cluster_id <- 1
  inf_params <- c("beta_0" = 2, "beta_1" = 1)
  smear_pos_prob <- .5
  max_size <- 5

  out <- flip_til_failure(cluster_id = cluster_id,
                                   inf_params = inf_params,
                                   smear_pos_prob = smear_pos_prob,
                                   max_size = max_size)
  expect_true(nrow(out) <= max_size)
  expect_true(out$censored[1])

  ## A regular one
  cluster_id <- 1
  inf_params <- c("beta_0" = -2, "beta_1" = 1.5)
  smear_pos_prob <- .5
  max_size <- 25

  out <- flip_til_failure(cluster_id = cluster_id,
                                   inf_params = inf_params,
                                   smear_pos_prob = smear_pos_prob,
                                   max_size = max_size)


})


test_that("generation infection", {
  cluster_id <- 1
  p_pos <- .3
  p_neg <- 0.00001
  smear_pos_prob <- .4
  gen <- 2
  ng <- 3
  prev_gen <- data.frame(smear = c(0, 1, 0),
                         person_id = paste0("C", cluster_id, "-G", gen,
                                            "-N", 1:ng) )
  cluster_size <- 3
  max_size <- 10

  out <- generation_infection(cluster_id = cluster_id,
                              p_pos = p_pos,
                              p_neg = p_neg,
                              smear_pos_prob = smear_pos_prob,
                              gen = gen,
                              prev_gen = prev_gen,
                              cluster_size = cluster_size,
                              max_size = max_size)
  expect_equal(length(out), 3)



  ############################
  cluster_id <- 1
  p_pos <- .3
  p_neg <- 0.000001
  smear_pos_prob <- .4
  gen <- 2
  ng <- 3
  prev_gen <- data.frame(smear = c(0, 0, 0),
                         person_id = paste0("C", cluster_id, "-G", gen,
                                                         "-N", 1:ng))
  cluster_size <- 3
  max_size <- 10

  out <- generation_infection(cluster_id = cluster_id,
                              p_pos = p_pos,
                              p_neg = p_neg,
                              smear_pos_prob = smear_pos_prob,
                              gen = gen,
                              prev_gen = prev_gen,
                              cluster_size = cluster_size,
                              max_size = max_size)
  expect_true(is.null(out$cur_gen))

  #######################
  cluster_id <- 1
  p_pos <- 1
  p_neg <- 0.000001
  smear_pos_prob <- 1
  gen <- 2
  ng <- 1
  prev_gen <- data.frame(smear = c(1),
                         person_id = paste0("C", cluster_id, "-G", gen,
                                            "-N", 1:ng))
  cluster_size <- 3
  max_size <- 10

  out <- generation_infection(cluster_id = cluster_id,
                              p_pos = p_pos,
                              p_neg = p_neg,
                              smear_pos_prob = smear_pos_prob,
                              gen = gen,
                              prev_gen = prev_gen,
                              cluster_size = cluster_size,
                              max_size = max_size)

  expect_equal(nrow(out$cur_gen), max_size - cluster_size)
  #######################
  cluster_id <- 1
  p_pos <- 1
  p_neg <- 0.000001
  smear_pos_prob <- 1
  gen <- 2
  ng <- 2
  prev_gen <- data.frame(smear = c(0,1),
                         person_id = paste0("C", cluster_id, "-G", gen,
                                            "-N", 1:ng))
  cluster_size <- 3
  max_size <- 10

  out <- generation_infection(cluster_id = cluster_id,
                              p_pos = p_pos,
                              p_neg = p_neg,
                              smear_pos_prob = smear_pos_prob,
                              gen = gen,
                              prev_gen = prev_gen,
                              cluster_size = cluster_size,
                              max_size = max_size)
  expect_equal(nrow(out$cur_gen), max_size - cluster_size)
  expect_equal(unique(out$cur_gen$inf_id), prev_gen$person_id[2])
  expect_equal(out$n_inf, c(0, 7))

})
skgallagher/TBornotTB documentation built on April 21, 2020, 1:19 p.m.