tests/testthat/test-gee-di.R

test_that("gee_di preserva clusters longitudinais com linhas embaralhadas", {

  set.seed(321)

  d <- expand.grid(
    block = factor(1:6),
    trat = factor(c("A", "B", "C")),
    time = 1:4
  )

  # Resposta angular sintetica, mantendo valores no dominio esperado.
  d$DI <- stats::runif(nrow(d), 0.15, 0.85)
  d$DI_clip <- pmin(pmax(d$DI, 1e-6), 1 - 1e-6)
  d$DI_ang <- asin(sqrt(d$DI_clip))

  # Embaralhamento deliberado das linhas.
  d <- d[sample(seq_len(nrow(d))), , drop = FALSE]

  res <- infectionIndex::gee_di(
    d,
    corstr = "independence"
  )

  expect_type(res, "list")
  expect_true(all(c(
    "gee0", "gee1", "comp", "predictions"
  ) %in% names(res)))

  # 6 blocos x 3 tratamentos = 18 parcelas.
  expect_equal(
    length(res$gee1$geese$clusz),
    18L
  )

  # Cada parcela possui exatamente quatro tempos.
  expect_true(all(res$gee1$geese$clusz == 4L))

  # Predicoes: 3 tratamentos x 4 tempos.
  expect_equal(nrow(res$predictions), 12L)
  expect_true(all(is.finite(res$predictions$DI_hat)))
  expect_true(all(
    res$predictions$DI_hat >= 0 &
    res$predictions$DI_hat <= 1
  ))
})


test_that("gee_di suporta unit explicita sem block", {

  set.seed(321)

  d <- data.frame(
    unit = rep(paste0("U", 1:18), each = 4),
    trat = factor(
      rep(rep(c("A", "B", "C"), each = 6), each = 4)
    ),
    time = rep(1:4, 18)
  )

  d$DI <- stats::runif(nrow(d), 0.15, 0.85)
  d$DI_clip <- pmin(pmax(d$DI, 1e-6), 1 - 1e-6)
  d$DI_ang <- asin(sqrt(d$DI_clip))

  # Embaralhamento deliberado das linhas.
  d <- d[sample(seq_len(nrow(d))), , drop = FALSE]

  res <- infectionIndex::gee_di(
    d,
    corstr = "independence"
  )

  expect_type(res, "list")
  expect_false("block" %in% names(d))

  # 18 unidades experimentais explicitas.
  expect_equal(
    length(res$gee1$geese$clusz),
    18L
  )

  # Cada unidade possui exatamente quatro tempos.
  expect_true(all(res$gee1$geese$clusz == 4L))

  # Predicoes: 3 tratamentos x 4 tempos.
  expect_equal(nrow(res$predictions), 12L)
  expect_true(all(is.finite(res$predictions$DI_hat)))
  expect_true(all(
    res$predictions$DI_hat >= 0 &
    res$predictions$DI_hat <= 1
  ))
})


test_that("gee_di supports factorial treatment structure", {
  d <- expand.grid(
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    rep = 1:4,
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE,
    stringsAsFactors = FALSE
  )

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

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

  d$DI <- sin(d$DI_ang)^2
  d$DI_clip <- pmin(pmax(d$DI, 1e-6), 1 - 1e-6)

  res <- gee_di(
    d,
    corstr = "independence",
    do_anova = FALSE,
    factors = c("cultivar", "produto")
  )

  expect_equal(
    attr(stats::terms(res$gee0), "term.labels"),
    c("cultivar", "produto", "time", "cultivar:produto")
  )

  expect_equal(
    attr(stats::terms(res$gee1), "term.labels"),
    c(
      "cultivar", "produto", "time",
      "cultivar:produto",
      "cultivar:time",
      "produto:time",
      "cultivar:produto:time"
    )
  )

  expect_equal(nrow(res$gee1$data), 64L)
  expect_equal(length(unique(res$gee1$id)), 16L)
  expect_true(all(table(res$gee1$id) == 4L))

  expect_equal(nrow(res$predictions), 16L)
  expect_true(
    all(c(
      "cultivar", "produto", "time",
      "theta_hat", "DI_hat", "DI_lwr", "DI_upr"
    ) %in% names(res$predictions))
  )

  expect_true(
    all(table(
      res$predictions$cultivar,
      res$predictions$produto
    ) == 4L)
  )

  expect_null(res$comp)
}) 


test_that("gee_di performs global temporal Wald test for factorial models", {
  d <- expand.grid(
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    rep = 1:4,
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE,
    stringsAsFactors = FALSE
  )

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

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


  # Pequena variabilidade experimental evita dispersao residual nula
  # neste teste, que representa o caso inferencial regular.
  set.seed(456)
  d$DI_ang <- d$DI_ang + stats::rnorm(nrow(d), 0, 0.01)

  d$DI <- sin(d$DI_ang)^2
  d$DI_clip <- pmin(pmax(d$DI, 1e-6), 1 - 1e-6)

  res <- suppressMessages(
    gee_di(
      d,
      corstr = "independence",
      do_anova = TRUE,
      factors = c("cultivar", "produto")
    )
  )

  expect_s3_class(res$comp, "data.frame")
  expect_equal(res$comp$test, "Wald")
  expect_equal(res$comp$term, "interaction")
  expect_equal(res$comp$df, 3L)
  expect_true(is.finite(res$comp$statistic))
  expect_true(res$comp$statistic >= 0)
  expect_true(res$comp$p.value >= 0)
  expect_true(res$comp$p.value <= 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.