tests/testthat/test-gee-cluster.R

test_that("GEE usa parcela block x trat como cluster longitudinal", {
  skip_if_not_installed("geepack")

  d <- expand.grid(
    block = factor(paste0("b", 1:3)),
    trat = factor(c("A", "B")),
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE
  )
  d <- d[order(d$block, d$trat, d$time), ]
  d$DI <- seq(0.20, 0.80, length.out = nrow(d))
  d$DI_clip <- d$DI
  d$DI_ang <- asin(sqrt(d$DI_clip))

  z <- gee_di(d, corstr = "independence", do_anova = FALSE)
  ids <- z$gee1$id

  expect_equal(length(unique(ids)), 6L)
  expect_equal(max(as.integer(table(ids))), 4L)
})

test_that("identificador de parcela combina repeticao e tratamento", {
  d <- expand.grid(
    block = factor(paste0("b", 1:3)),
    trat = factor(c("A", "B")),
    time = 1:4,
    KEEP.OUT.ATTRS = FALSE
  )
  z <- infectionIndex:::add_parcela_id(d)
  expect_equal(nlevels(z$.parcela_id), 6L)
  expect_true(all(table(z$.parcela_id) == 4L))
})


test_that("prepare_gee_data e robusto a ordem das linhas", {

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

  set.seed(123)
  d <- d[sample(nrow(d)), ]

  preparado <- infectionIndex:::prepare_gee_data(d)

  expect_equal(nrow(preparado), 72L)
  expect_equal(length(unique(preparado$.parcela_id)), 18L)

  tamanhos <- table(preparado$.parcela_id)
  expect_true(all(tamanhos == 4L))

  por_parcela <- split(preparado$time, preparado$.parcela_id)
  expect_true(all(vapply(
    por_parcela,
    function(x) identical(as.numeric(x), c(1, 2, 3, 4)),
    logical(1)
  )))
})


test_that("explicit unit identifies experimental units without block", {

  dat <- data.frame(
    unit = c(
      "A1", "A1",
      "A2", "A2",
      "B1", "B1",
      "B2", "B2"
    ),
    trat = c(
      "A", "A",
      "A", "A",
      "B", "B",
      "B", "B"
    ),
    time = c(1, 2, 1, 2, 1, 2, 1, 2)
  )

  out <- prepare_gee_data(dat)

  expect_equal(nlevels(out$.unit_id), 4L)
  expect_equal(
    levels(out$.unit_id),
    c("A1", "A2", "B1", "B2")
  )

  expect_identical(out$.unit_id, out$.parcela_id)
  expect_false("block" %in% names(out))
})


test_that("block-treatment rule remains backward compatible", {

  dat <- data.frame(
    block = c(1, 1, 1, 1, 2, 2, 2, 2),
    trat  = c("A", "A", "B", "B", "A", "A", "B", "B"),
    time  = c(1, 2, 1, 2, 1, 2, 1, 2)
  )

  out <- prepare_gee_data(dat)

  expect_equal(nlevels(out$.unit_id), 4L)
  expect_identical(out$.unit_id, out$.parcela_id)

  expect_equal(
    as.character(out$.unit_id),
    c("1.A", "1.A", "1.B", "1.B",
      "2.A", "2.A", "2.B", "2.B")
  )
})


test_that("explicit unit cannot belong to multiple treatments", {

  dat <- data.frame(
    unit = c("U1", "U1", "U1", "U1"),
    trat = c("A", "A", "B", "B"),
    time = c(1, 2, 1, 2)
  )

  expect_error(
    prepare_gee_data(dat),
    "only one combination.*U1"
  )
})



test_that("factorial block design derives unit from block and treatment factors", {
  d <- expand.grid(
    block = 1:3,
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    time = 1:2
  )

  z <- prepare_gee_data(
    d,
    factors = c("cultivar", "produto")
  )

  expect_equal(length(unique(z$.unit_id)), 12L)
  expect_true(all(table(z$.unit_id) == 2L))

  unit_map <- unique(
    z[c("block", "cultivar", "produto", ".unit_id")]
  )

  expect_equal(nrow(unit_map), 12L)
  expect_equal(
    length(unique(interaction(
      unit_map$block,
      unit_map$cultivar,
      unit_map$produto,
      drop = TRUE
    ))),
    12L
  )
} )

test_that("explicit unit defines factorial longitudinal clusters", {
  d <- expand.grid(
    unit = paste0("U", 1:8),
    time = 1:3
  )

  design <- data.frame(
    unit = paste0("U", 1:8),
    cultivar = rep(c("C1", "C2"), each = 4),
    produto = rep(rep(c("P1", "P2"), each = 2), 2)
  )

  d <- merge(d, design, by = "unit", sort = FALSE)

  z <- prepare_gee_data(
    d,
    factors = c("cultivar", "produto")
  )

  expect_equal(length(unique(z$.unit_id)), 8L)
  expect_true(all(table(z$.unit_id) == 3L))
  expect_equal(
    as.character(z$.unit_id),
    rep(levels(z$.unit_id), each = 3)
  )
  times_by_unit <- tapply(
    z$time,
    z$.unit_id,
    paste,
    collapse = ","
  )

  expect_true(
    all(times_by_unit == "1,2,3")
  )
} )

test_that("explicit unit cannot change factorial treatment combination", {
  d <- data.frame(
    unit = c("U1", "U1", "U2", "U2"),
    cultivar = c("C1", "C1", "C2", "C2"),
    produto = c("P1", "P2", "P1", "P1"),
    time = c(1, 2, 1, 2)
  )

  expect_error(
    add_parcela_id(
      d,
      factors = c("cultivar", "produto")
    ),
    "more than one combination.*U1"
  )
} ) 

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.