tests/testthat/test-simulate_outbreak.R

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

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

  #################
  ##Start at an outsider
  #################
  start_at_outsider <- TRUE
  inf_params <- c("beta_0" = -2, "beta_1" = 1.5)
  smear_pos_prob <- .5
  max_size <- 10
  K <- 10

  out <- simulate_outbreak(K = K,
                                   inf_params = inf_params,
                                   smear_pos_prob = smear_pos_prob,
                                   max_size = max_size,
                                   start_at_outsider = start_at_outsider)
  expect_true(length(unique(out$cluster_id)) <= K)

  ## ###############
  start_at_outsider <- TRUE
  keep_zero_clusters <- TRUE
  inf_params <- c("beta_0" = -2, "beta_1" = 1.5)
  smear_pos_prob <- .5
  max_size <- 10
  K <- 10

  out <- simulate_outbreak(K = K,
                                   inf_params = inf_params,
                                   smear_pos_prob = smear_pos_prob,
                                   max_size = max_size,
                           start_at_outsider = start_at_outsider,
                           keep_zero_clusters = keep_zero_clusters)
  expect_true(length(unique(out$cluster_id)) == K)






  
})


test_that("move_up_gen", {
    id <- c("C1-G1-N1",
           "C1-G2-N1")
    out <- move_up_gen(id)
    exp_out <-   c("C1-G0-N1",
                   "C1-G1-N1")
    expect_equal(out, exp_out)
    #############
    id <- c("C1-G1-N1",
            NA)
    out <- move_up_gen(id)
    exp_out <-   c("C1-G0-N1",
                   NA)
    expect_equal(out, exp_out)

})


test_that("format_to_outsider", {
  df <- data.frame(cluster_id = c(1, 1),
      person_id = c("C1-G1-N1",
                  "C1-G2-N1"),
      smear = c(1, -1),
      gen = c(1, 2),
      inf_id = c(NA, "C1-G1-N1"),
      n_inf = c(1, 0),
      cluster_size = c(2, 2),
      cluster_pos = c(1, 1),
      censored = FALSE,
                   stringsAsFactors = FALSE)
  exp_out <- data.frame(cluster_id = c(1),
                    person_id = c("C1-G2-N1"),
                    smear = c(-1),
                    gen = c(1),
                    inf_id = c(NA),
                    n_inf = c(0),
                    cluster_size = c(1),
                    cluster_pos = c(0),
                    censored = FALSE,
                    stringsAsFactors = FALSE)
  out <- format_to_outsider(df)
  expect_equal(out$cluster_pos, exp_out$cluster_pos)
  expect_equal(out$cluster_size, exp_out$cluster_size)
  #####################################################
  df <- data.frame(cluster_id = c(1, 1, 2, 2, 2, 2),
                   person_id = c("C1-G1-N1",
                                 "C1-G2-N1",
                                 "C2-G1-N1",
                                 "C2-G2-N1",
                                 "C2-G2-N2",
                                 "C2-G3-N1"),
                   smear = c(1, -1,
                             1, 1, -1, 1),
                   gen = c(1, 2,
                           1, 2, 2, 3),
                   inf_id = c(NA, "C1-G1-N1",
                              NA, "C2-G1-N1", "C2-G1-N1", "C2-G2-N2"),
                   n_inf = c(1, 0,
                             2, 0, 1, 0),
                   cluster_size = c(2, 2,
                                    4, 4, 4, 4),
                   cluster_pos = c(1, 1,
                                   3, 3, 3, 3),
                   censored = FALSE,
                   stringsAsFactors = FALSE)
  exp_out <- data.frame(cluster_id = c(1, 2, 2, 2),
                        person_id = c("C1-G1-N1",
                                      "C2-G1-N1",
                                      "C2-G1-N2",
                                      "C2-G2-N1"),
                        smear = c(-1,
                                  1, -1, 1),
                        gen = c(1,
                                1, 1, 2),
                        inf_id = c(NA,
                                   NA, NA,  "C2-G1-N2"),
                        n_inf = c(0,
                                  0, 1, 0),
                        cluster_size = c(1,
                                         3, 3, 3),
                        cluster_pos = c(0,
                                        2, 2, 2),
                        censored = FALSE,
                        stringsAsFactors = FALSE)
  out <- format_to_outsider(df)
  expect_equal(out$cluster_pos, exp_out$cluster_pos)
  expect_equal(out$cluster_size, exp_out$cluster_size)

################################################3
   df <- data.frame(cluster_id = c(1, 2),
      person_id = c("C1-G1-N1",
                  "C2-G1-N1"),
      smear = c(1, -1),
      gen = c(1, 1),
      inf_id = c(NA, NA),
      n_inf = c(0, 0),
      cluster_size = c(1, 1),
      cluster_pos = c(0, 0),
      censored = FALSE,
      stringsAsFactors = FALSE)
   exp_out <- data.frame(cluster_id = c(1, 2),
                        person_id = c("C1-G0-N1",
                                      "C2-G0-N1"),
                        smear = c(1,
                                  -1),
                        gen = c(0, 0),
                        inf_id = as.character(c(NA,
                                   NA)),
                        n_inf = c(0,
                                  0),
                        cluster_size = c(0,
                                         0),
                        cluster_pos = as.integer(c(1,
                                        0)),
                        censored = FALSE,
                        stringsAsFactors = FALSE)

  out <- format_to_outsider(df,
                            keep_zero_clusters = TRUE)

  expect_equal(out$gen, exp_out$gen)
  ######################33
   df <- data.frame(cluster_id = c(1, 2, 2),
      person_id = c("C1-G1-N1",
                    "C2-G1-N1",
                    "C2-G2-N1"),
      smear = c(1, -1, -1),
      gen = c(1, 1, 2),
      inf_id = c(NA, NA, "C2-G1-N1"),
      n_inf = c(0, 1, 0),
      cluster_size = c(1, 2, 2),
      cluster_pos = c(1, 0, 0),
      censored = FALSE,
      stringsAsFactors = FALSE)
  
   exp_out <- data.frame(cluster_id = c(1, 2),
                        person_id = c("C1-G0-N1",
                                      "C2-G1-N1"),
                        smear = c(1,
                                  -1),
                        gen = c(0, 1),
                        inf_id = as.character(c(NA,
                                   NA)),
                        n_inf = c(0,
                                  0),
                        cluster_size = c(0,
                                         1),
                        cluster_pos = as.integer(c(1,
                                        0)),
                        censored = FALSE,
                        stringsAsFactors = FALSE)

  out <- format_to_outsider(df,
                            keep_zero_clusters = TRUE)

  expect_equal(out$gen, exp_out$gen)
})
skgallagher/TBornotTB documentation built on April 21, 2020, 1:19 p.m.