tests/testthat/test-wald.R

test_that("wald_interaction testa conjuntamente os coeficientes de interação", {

  set.seed(123)

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

  d$.parcela_id <- interaction(
    d$block,
    d$trat,
    drop = TRUE
  )

  d$DI_ang <- 0.5 +
    0.05 * d$time +
    ifelse(d$trat == "B", 0.10, 0) +
    ifelse(d$trat == "C", -0.05, 0) +
    ifelse(d$trat == "B", 0.04 * d$time, 0) +
    ifelse(d$trat == "C", -0.03 * d$time, 0) +
    rnorm(nrow(d), 0, 0.05)

  fit <- geepack::geeglm(
    DI_ang ~ trat * time,
    id = .parcela_id,
    data = d,
    corstr = "independence",
    family = gaussian()
  )

  w <- infectionIndex:::wald_interaction(fit)

  expect_s3_class(w, "data.frame")
  expect_equal(w$test, "Wald")
  expect_equal(w$term, "interaction")
  expect_equal(w$df, 2L)

  expect_true(is.finite(w$statistic))
  expect_true(w$statistic >= 0)

  expect_true(is.finite(w$p.value))
  expect_true(w$p.value >= 0)
  expect_true(w$p.value <= 1)
})



test_that("wald_interaction tests only temporal interactions in factorial models", {

  d <- expand.grid(
    cultivar = factor(c("C1", "C2")),
    produto = factor(c("P1", "P2")),
    rep = 1:4,
    time = 1:4,
    KEEP.OUT.ATTRS = 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
  )


  set.seed(456)
  d$DI_ang <- d$DI_ang + rnorm(nrow(d), 0, 0.01)

  fit <- geepack::geeglm(
    DI_ang ~ cultivar * produto * time,
    id = unit,
    data = d,
    corstr = "independence",
    family = gaussian()
  )

  w <- infectionIndex:::wald_interaction(
    fit,
    factors = c("cultivar", "produto"),
    time = "time"
  )

  expect_s3_class(w, "data.frame")
  expect_equal(w$test, "Wald")
  expect_equal(w$term, "interaction")

  # cultivar:produto is not part of the temporal Wald test.
  # For a 2 x 2 factorial with numeric time, the tested coefficients are:
  # cultivar:time, produto:time and cultivar:produto:time.
  expect_equal(w$df, 3L)

  expect_true(is.finite(w$statistic))
  expect_true(w$statistic >= 0)
  expect_true(is.finite(w$p.value))
  expect_true(w$p.value >= 0)
  expect_true(w$p.value <= 1)
}) 

test_that("wald_interaction handles singular covariance without crashing", {

  set.seed(789)

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

  d$.parcela_id <- interaction(
    d$block,
    d$trat,
    drop = TRUE
  )

  # Resposta determinística: ausência deliberada de variação residual.
  d$DI_ang <- with(
    d,
    0.30 +
      0.05 * time +
      0.10 * (trat == "B") +
      0.20 * (trat == "C") +
      0.02 * (trat == "B") * time +
      0.04 * (trat == "C") * time
  )

  fit <- geepack::geeglm(
    DI_ang ~ trat * time,
    id = .parcela_id,
    data = d,
    corstr = "independence",
    family = gaussian()
  )

  w <- infectionIndex:::wald_interaction(fit)

  expect_s3_class(w, "data.frame")
  expect_equal(w$test, "Wald")
  expect_equal(w$term, "interaction")
  expect_equal(w$df, 2L)

  expect_true(is.na(w$statistic))
  expect_true(is.na(w$p.value))
  expect_equal(w$reason, "near-zero model dispersion")
})

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.