tests/testthat/test-plots.R

test_that("heatmap_di does not require block or unit", {

  d <- data.frame(
    trat = factor(rep(c("A", "B"), each = 3)),
    time = rep(1:3, 2),
    DI = c(0.10, 0.20, 0.30, 0.20, 0.35, 0.50)
  )

  p <- infectionIndex:::heatmap_di(d)

  expect_s3_class(p, "ggplot")
})


test_that("heatmap_di accepts explicit experimental units without block", {

  d <- data.frame(
    unit = rep(c("A1", "A2", "B1", "B2"), each = 3),
    trat = factor(rep(c("A", "A", "B", "B"), each = 3)),
    time = rep(1:3, 4),
    DI = c(
      0.10, 0.20, 0.30,
      0.12, 0.22, 0.32,
      0.20, 0.35, 0.50,
      0.22, 0.37, 0.52
    )
  )

  p <- infectionIndex:::heatmap_di(d)

  expect_s3_class(p, "ggplot")

  built <- ggplot2::ggplot_build(p)
  expect_equal(nrow(built$data[[1]]), 6L)
})


test_that("violin_auc preserves legacy treatment behavior", {

  dat <- data.frame(
    trat = factor(rep(c("A", "B"), each = 4)),
    AUC = c(1.0, 1.2, 1.1, 1.3, 2.0, 2.2, 2.1, 2.3)
  )

  sum_tbl <- summary_auc_by_trat(dat)

  p <- violin_auc(
    dat,
    summary_tbl = sum_tbl,
    progress_metric = "audpc"
  )

  expect_s3_class(p, "ggplot")
  expect_equal(p$labels$x, "Treatment")
  expect_equal(p$labels$y, "AUDPC")
  expect_equal(levels(p$data$.factor_group), c("A", "B"))
  expect_equal(nrow(p$data), 8L)
})


test_that("violin_auc supports factorial treatment combinations", {

  dat <- expand.grid(
    cultivar = factor(c("C1", "C2")),
    produto = factor(c("P1", "P2")),
    rep = 1:4,
    KEEP.OUT.ATTRS = FALSE
  )

  dat$AUC <- with(
    dat,
    1 +
      0.5 * (cultivar == "C2") +
      0.3 * (produto == "P2") +
      0.05 * rep
  )

  dat <- dat[
    !(dat$cultivar == "C2" &
      dat$produto == "P1" &
      dat$rep == 4),
  ]

  sum_tbl <- summary_auc_by_trat(
    dat,
    factors = c("cultivar", "produto")
  )

  p <- violin_auc(
    dat,
    summary_tbl = sum_tbl,
    progress_metric = "audps",
    factors = c("cultivar", "produto")
  )

  expect_s3_class(p, "ggplot")
  expect_equal(p$labels$x, "cultivar × produto")
  expect_equal(p$labels$y, "AUDPS")

  expect_equal(
    levels(p$data$.factor_group),
    c("C1 × P1", "C1 × P2", "C2 × P1", "C2 × P2")
  )

  expect_equal(nrow(p$data), 15L)

  expect_equal(
    as.integer(table(p$data$.factor_group)),
    c(4L, 4L, 3L, 4L)
  )

  expect_equal(
    names(table(p$data$.factor_group)),
    c("C1 × P1", "C1 × P2", "C2 × P1", "C2 × P2")
  )
}) 


test_that("heatmap_di supports factorial treatment structure", {

  d <- expand.grid(
    cultivar = factor(c("C1", "C2")),
    produto = factor(c("P1", "P2")),
    time = 1:4,
    rep = 1:2,
    KEEP.OUT.ATTRS = FALSE
  )

  d$DI <- with(
    d,
    0.10 +
      0.05 * (cultivar == "C2") +
      0.03 * (produto == "P2") +
      0.04 * time
  )

  p <- heatmap_di(
    d,
    factors = c("cultivar", "produto")
  )

  expect_s3_class(p, "ggplot")
  expect_equal(p$labels$x, "Time")
  expect_equal(p$labels$y, "cultivar")
  expect_equal(p$labels$fill, "Mean DI")
  expect_equal(nrow(p$data), 16L)
  expect_s3_class(p$facet, "FacetWrap")

  expect_equal(
    sort(unique(as.character(p$data$cultivar))),
    c("C1", "C2")
  )

  expect_equal(
    sort(unique(as.character(p$data$produto))),
    c("P1", "P2")
  )
}) 


test_that("plot_gee_predictions preserves legacy treatment behavior", {

  pred <- expand.grid(
    trat = factor(c("A", "B")),
    time = 1:3,
    KEEP.OUT.ATTRS = FALSE
  )

  pred$DI_hat <- seq(0.10, 0.60, length.out = nrow(pred))
  pred$DI_lwr <- pmax(0, pred$DI_hat - 0.05)
  pred$DI_upr <- pmin(1, pred$DI_hat + 0.05)

  p <- plot_gee_predictions(pred)

  expect_s3_class(p, "ggplot")
  expect_equal(p$labels$x, "Time")
  expect_equal(p$labels$y, "DI (0–1)")
  expect_equal(p$labels$colour, "Treatment")
  expect_equal(p$labels$fill, "Treatment")
  expect_equal(nrow(p$data), 6L)
  expect_s3_class(p$facet, "FacetNull")
})


test_that("plot_gee_predictions supports factorial treatment structure", {

  pred <- expand.grid(
    cultivar = factor(c("C1", "C2")),
    produto = factor(c("P1", "P2")),
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE
  )

  pred$DI_hat <- seq(0.10, 0.80, length.out = nrow(pred))
  pred$DI_lwr <- pmax(0, pred$DI_hat - 0.05)
  pred$DI_upr <- pmin(1, pred$DI_hat + 0.05)

  p <- plot_gee_predictions(
    pred,
    factors = c("cultivar", "produto")
  )

  expect_s3_class(p, "ggplot")
  expect_equal(p$labels$x, "Time")
  expect_equal(p$labels$y, "DI (0–1)")
  expect_equal(p$labels$colour, "cultivar")
  expect_equal(p$labels$fill, "cultivar")
  expect_equal(nrow(p$data), 16L)
  expect_s3_class(p$facet, "FacetWrap")

  expect_equal(
    sort(unique(as.character(p$data$cultivar))),
    c("C1", "C2")
  )

  expect_equal(
    sort(unique(as.character(p$data$produto))),
    c("P1", "P2")
  )
}) 

Try the infectionIndex package in your browser

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

infectionIndex documentation built on Dec. 4, 2026, 5:10 p.m.