tests/testthat/test-auc.R

test_that("auc_by_parcela calcula corretamente a regra trapezoidal", {

  d <- data.frame(
    block = factor(c(1, 1, 1, 1, 2, 2, 2, 2)),
    trat  = factor(rep("A", 8)),
    time  = rep(1:4, 2),
    DI    = c(
      0.2, 0.4, 0.6, 0.8,
      0.1, 0.3, 0.5, 0.7
    )
  )

  res <- infectionIndex:::auc_by_parcela(d)

  # Parcela 1:
  # (0.2+0.4)/2 + (0.4+0.6)/2 + (0.6+0.8)/2 = 1.5
  # Parcela 2:
  # (0.1+0.3)/2 + (0.3+0.5)/2 + (0.5+0.7)/2 = 1.2

  expect_equal(nrow(res), 2L)
  expect_true(all(res$n_pts == 4L))
  expect_equal(res$AUC, c(1.5, 1.2), tolerance = 1e-12)
})


test_that("auc_by_parcela ordena corretamente os tempos", {

  d <- data.frame(
    block = factor(rep(1, 4)),
    trat = factor(rep("A", 4)),
    time = c(4, 1, 3, 2),
    DI = c(0.8, 0.2, 0.6, 0.4)
  )

  res <- infectionIndex:::auc_by_parcela(d)

  expect_equal(res$AUC, 1.5, tolerance = 1e-12)
  expect_equal(res$n_pts, 4L)
})


test_that("summary_auc_by_trat calcula resumo por tratamento", {

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

  res <- infectionIndex:::summary_auc_by_trat(a)

  expect_equal(res$n, c(3L, 3L))
  expect_equal(res$mean, c(2, 4), tolerance = 1e-12)
  expect_equal(res$sd, c(1, 2), tolerance = 1e-12)
  expect_equal(
    res$se,
    c(1 / sqrt(3), 2 / sqrt(3)),
    tolerance = 1e-12
  )
  expect_true(all(res$lwr < res$mean))
  expect_true(all(res$upr > res$mean))
})


test_that("test_auc_robust retorna Kruskal-Wallis e comparacoes pareadas", {

  a <- data.frame(
    block = factor(rep(1:4, 3)),
    trat = factor(rep(c("A", "B", "C"), each = 4)),
    AUC = c(
      1.0, 1.1, 1.2, 1.3,
      2.0, 2.1, 2.2, 2.3,
      3.0, 3.1, 3.2, 3.3
    )
  )

  res <- infectionIndex:::test_auc_robust(a)

  expect_s3_class(res$kruskal, "htest")
  expect_true(is.finite(res$kruskal$p.value))
  expect_true(res$kruskal$p.value >= 0)
  expect_true(res$kruskal$p.value <= 1)

  expect_s3_class(res$pairs, "data.frame")
  expect_true(all(c("trat1", "trat2", "p_adj") %in% names(res$pairs)))
  expect_equal(nrow(res$pairs), 3L)
  expect_true(all(res$pairs$p_adj >= 0 & res$pairs$p_adj <= 1))
})

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.