tests/testthat/test-audps.R

test_that("AUDPS matches manual calculation", {
  x <- data.frame(
    block = factor(1),
    trat = factor("A"),
    time = c(0, 2, 4, 6),
    DI = c(0.10, 0.20, 0.40, 0.60)
  )

  got <- audps_by_parcela(x)$AUDPS
  expect_equal(got, 2.6, tolerance = 1e-14)
})

test_that("AUDPS equals AUDPC plus endpoint correction", {
  x <- data.frame(
    block = factor(1),
    trat = factor("A"),
    time = c(0, 2, 4, 6),
    DI = c(0.10, 0.20, 0.40, 0.60)
  )

  audpc <- auc_by_parcela(x)$AUC
  audps <- audps_by_parcela(x)$AUDPS
  correction <- ((0.10 + 0.60) / 2) * (6 / 3)

  expect_equal(audpc, 1.9, tolerance = 1e-14)
  expect_equal(audps, audpc + correction, tolerance = 1e-14)
})

test_that("AUDPS is calculated independently by experimental unit", {
  x <- data.frame(
    block = factor(c(1,1,1, 2,2,2)),
    trat = factor(rep("A", 6)),
    time = rep(c(0, 1, 2), 2),
    DI = c(0.1,0.2,0.3, 0.2,0.4,0.6)
  )

  got <- audps_by_parcela(x)
  expect_equal(nrow(got), 2L)
  expect_true(all(is.finite(got$AUDPS)))
})

test_that("AUDPS returns NA with fewer than two observations", {
  x <- data.frame(
    block = factor(1),
    trat = factor("A"),
    time = 0,
    DI = 0.25
  )

  expect_true(is.na(audps_by_parcela(x)$AUDPS))
})

test_that("zero disease produces zero AUDPS", {
  x <- data.frame(
    block = factor(rep(1, 4)),
    trat = factor(rep("A", 4)),
    time = c(0, 1, 2, 3),
    DI = rep(0, 4)
  )

  expect_equal(audps_by_parcela(x)$AUDPS, 0)
})

test_that("AUDPS supports unequal assessment intervals", {
  x <- data.frame(
    block = factor(1),
    trat = factor("A"),
    time = c(0, 2, 5, 9),
    DI = c(0.10, 0.20, 0.40, 0.60)
  )

  audpc <- auc_by_parcela(x)$AUC
  audps <- audps_by_parcela(x)$AUDPS

  expected_audpc <-
    ((0.10 + 0.20) / 2) * 2 +
    ((0.20 + 0.40) / 2) * 3 +
    ((0.40 + 0.60) / 2) * 4

  expected_audps <- expected_audpc +
    ((0.10 + 0.60) / 2) * (9 / 3)

  expect_equal(audpc, expected_audpc, tolerance = 1e-14)
  expect_equal(audps, expected_audps, tolerance = 1e-14)
})

test_that("progress metric selector uses AUDPC", {
  x <- data.frame(
    block = factor(1),
    trat = factor("A"),
    n_pts = 4L,
    AUC = 1.9,
    AUDPC = 1.9,
    AUDPS = 2.6
  )

  got <- select_progress_metric(x, "audpc")

  expect_equal(got$AUC, 1.9)
  expect_equal(got$AUDPC, 1.9)
  expect_equal(got$AUDPS, 2.6)
})

test_that("progress metric selector uses AUDPS without changing original metrics", {
  x <- data.frame(
    block = factor(1),
    trat = factor("A"),
    n_pts = 4L,
    AUC = 1.9,
    AUDPC = 1.9,
    AUDPS = 2.6
  )

  got <- select_progress_metric(x, "audps")

  expect_equal(got$AUC, 2.6)
  expect_equal(got$AUDPC, 1.9)
  expect_equal(got$AUDPS, 2.6)
})

test_that("AUDPC remains the default progress metric", {
  x <- data.frame(
    AUDPC = 1.9,
    AUDPS = 2.6
  )

  got <- select_progress_metric(x)
  expect_equal(got$AUC, got$AUDPC)
})

test_that("progress metric selector rejects unknown metric", {
  x <- data.frame(
    AUDPC = 1.9,
    AUDPS = 2.6
  )

  expect_error(
    select_progress_metric(x, "unknown"),
    "arg"
  )
})

test_that("violin plot labels selected progress metric", {

  x <- data.frame(
    trat = factor(rep(c("A", "B"), each = 3)),
    AUC = c(1, 2, 3, 2, 3, 4)
  )

  p_pc <- violin_auc(x, progress_metric = "audpc")
  p_ps <- violin_auc(x, progress_metric = "audps")

  expect_equal(p_pc$labels$y, "AUDPC")
  expect_equal(p_ps$labels$y, "AUDPS")
})


test_that("disease progress supports explicit experimental units without block", {

  dat <- data.frame(
    unit = rep(c("A1", "A2", "B1", "B2"), each = 3),
    trat = rep(c("A", "A", "B", "B"), each = 3),
    time = rep(1:3, 4),
    DI = c(
      0.1, 0.2, 0.3,
      0.2, 0.3, 0.4,
      0.3, 0.4, 0.5,
      0.4, 0.5, 0.6
    )
  )

  out <- disease_progress_by_parcela(dat)

  expect_equal(nrow(out), 4L)
  expect_true("unit" %in% names(out))
  expect_false("block" %in% names(out))

  expect_equal(
    as.character(out$unit),
    c("A1", "A2", "B1", "B2")
  )

  expect_equal(
    out$AUDPC,
    c(0.4, 0.6, 0.8, 1.0),
    tolerance = 1e-12
  )

  expect_equal(
    out$AUDPS,
    c(0.6, 0.9, 1.2, 1.5),
    tolerance = 1e-12
  )

  expect_equal(out$AUC, out$AUDPC)
})


test_that("disease progress preserves block and explicit unit", {

  dat <- data.frame(
    block = rep(c(1, 2), each = 6),
    unit = rep(c("A1", "A2", "B1", "B2"), each = 3),
    trat = rep(c("A", "A", "B", "B"), each = 3),
    time = rep(1:3, 4),
    DI = c(
      0.1, 0.2, 0.3,
      0.2, 0.3, 0.4,
      0.3, 0.4, 0.5,
      0.4, 0.5, 0.6
    )
  )

  out <- disease_progress_by_parcela(dat)

  expect_equal(nrow(out), 4L)
  expect_true(all(c("block", "unit", "trat") %in% names(out)))

  expect_equal(
    as.character(out$unit),
    c("A1", "A2", "B1", "B2")
  )

  expect_equal(
    out$block,
    c(1, 1, 2, 2)
  )

  expect_equal(
    out$AUDPC,
    c(0.4, 0.6, 0.8, 1.0),
    tolerance = 1e-12
  )

  expect_equal(
    out$AUDPS,
    c(0.6, 0.9, 1.2, 1.5),
    tolerance = 1e-12
  )
})



test_that("disease progress supports factorial factors with explicit unit", {
  d <- expand.grid(
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    rep = 1:3,
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE,
    stringsAsFactors = FALSE
  )

  d$unit <- interaction(
    d$cultivar, d$produto, d$rep,
    drop = TRUE
  )

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

  out <- disease_progress_by_parcela(
    d,
    factors = c("cultivar", "produto")
  )

  expect_equal(nrow(out), 12L)
  expect_equal(length(unique(out$unit)), 12L)
  expect_false("block" %in% names(out))
  expect_true(all(c("unit", "cultivar", "produto") %in% names(out)))
  expect_true(all(table(out$cultivar, out$produto) == 3L))
  expect_true(all(out$n_pts == 4L))
  expect_equal(out$AUC, out$AUDPC)
  expect_true(all(is.finite(out$AUDPC)))
  expect_true(all(is.finite(out$AUDPS)))
})


test_that("disease progress derives factorial units from block and factors", {
  d <- expand.grid(
    block = 1:4,
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE,
    stringsAsFactors = FALSE
  )

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

  out <- disease_progress_by_parcela(
    d,
    factors = c("cultivar", "produto")
  )

  expect_equal(nrow(out), 16L)
  expect_equal(length(unique(out$block)), 4L)
  expect_false("unit" %in% names(out))
  expect_true(all(c("block", "cultivar", "produto") %in% names(out)))
  expect_true(all(table(out$cultivar, out$produto) == 4L))
  expect_true(all(table(out$block, out$cultivar, out$produto) == 1L))
  expect_true(all(out$n_pts == 4L))
  expect_equal(out$AUC, out$AUDPC)
  expect_true(all(is.finite(out$AUDPC)))
  expect_true(all(is.finite(out$AUDPS)))
}) 

test_that("summary_auc_by_trat 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)
  )

  res <- summary_auc_by_trat(dat)

  expect_equal(nrow(res), 2L)
  expect_equal(res$n, c(4L, 4L))
  expect_equal(res$mean, c(1.15, 2.15))
  expect_equal(as.character(res$trat), c("A", "B"))
})

test_that("summary_auc_by_trat summarizes factorial 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),
  ]

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

  expect_equal(nrow(res), 4L)
  expect_equal(sum(res$n), 15L)

  n_by_comb <- setNames(
    res$n,
    paste(res$cultivar, res$produto, sep = "_")
  )

  expect_equal(
    n_by_comb[c("C1_P1", "C1_P2", "C2_P1", "C2_P2")],
    c(C1_P1 = 4L, C1_P2 = 4L, C2_P1 = 3L, C2_P2 = 4L)
  )

  expect_true(
    all(c("cultivar", "produto", "n", "mean",
          "sd", "se", "lwr", "upr") %in% names(res))
  )
}) 

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.