tests/testthat/test-qic.R

test_that("compare_gee_corstr avalia as quatro estruturas de correlacao", {

  set.seed(123)

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

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

  res <- compare_gee_corstr(d, criterion = "QIC")

  expect_s3_class(res$comparison, "data.frame")

  expect_setequal(
    res$comparison$corstr,
    c("independence", "exchangeable", "ar1", "unstructured")
  )

  expect_true(all(c(
    "QIC", "QICu", "CIC", "QICC",
    "converged", "error", "selected"
  ) %in% names(res$comparison)))

  expect_equal(res$criterion, "QIC")
  expect_equal(res$tolerance, 1e-8)
})


test_that("compare_gee_corstr usa parcela block x trat como cluster", {

  set.seed(321)

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

  d$DI_ang <- 0.5 +
    0.04 * d$time +
    rnorm(nrow(d), 0, 0.05)

  res <- compare_gee_corstr(d, criterion = "QIC")

  clus <- res$fits$independence$geese$clusz

  expect_equal(length(clus), 18L)
  expect_true(all(clus == 4L))
})


test_that("criterio selecionado corresponde ao menor valor dentro da tolerancia", {

  set.seed(456)

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

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

  res <- compare_gee_corstr(
    d,
    criterion = "QIC",
    tolerance = 1e-8
  )

  tab <- res$comparison
  valid <- tab$converged & is.finite(tab$QIC)
  minimo <- min(tab$QIC[valid])

  esperado <- valid & abs(tab$QIC - minimo) <= 1e-8

  expect_equal(tab$selected, esperado)
})


test_that("compare_gee_corstr 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 +
      0.005 * (cultivar == "C2") * (produto == "P2") * time
  )

  res <- compare_gee_corstr(
    d,
    factors = c("cultivar", "produto"),
    criterion = "QIC"
  )

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

  expect_equal(res$factors, c("cultivar", "produto"))

  expect_setequal(
    res$comparison$corstr,
    c("independence", "exchangeable", "ar1", "unstructured")
  )

  expect_true(all(res$comparison$converged))
  valid <- res$comparison$converged &
    is.finite(res$comparison$QIC)

  expect_true(any(valid))

  best <- min(res$comparison$QIC[valid])

  expected_selected <- valid &
    abs(res$comparison$QIC - best) <= res$tolerance

  expect_equal(
    res$comparison$selected,
    expected_selected
  )

  for (fit in res$fits) {
    expect_false(is.null(fit))

    expect_equal(
      attr(stats::terms(fit), "term.labels"),
      attr(stats::terms(res$formula), "term.labels")
    )

    expect_equal(length(fit$geese$clusz), 16L)
    expect_true(all(fit$geese$clusz == 4L))
  }
}) 

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.