R/gee_di.R

Defines functions gee_di

Documented in gee_di

#' Fit generalized estimating equation models for disease intensity
#'
#' Fits generalized estimating equation (GEE) models to angular-transformed
#' disease intensity. Compares a model without treatment-by-time interactions
#' against a model including these interactions. Supports single-factor and
#' factorial experimental designs.
#'
#' The experimental unit is identified by `unit`; if absent, it is derived
#' from `block` and the combination of factors specified in `factors`.
#'
#' @param data_longo Long-format data frame with `time`, `DI`, `DI_clip`,
#'   `DI_ang`, and columns named in `factors`. Must also contain `unit` or
#'   `block` to identify experimental units.
#' @param corstr Working correlation structure: `"ar1"`, `"exchangeable"`,
#'   `"independence"`, or `"unstructured"`.
#' @param do_anova If `TRUE`, compares treatment-by-time interactions.
#'   When the standard model comparison is unavailable, uses the package's
#'   Wald test.
#' @param factors Character vector naming treatment-factor columns.
#'   The default `"trat"` preserves the single-factor interface.
#'   For factorial designs, for example, use `c("cultivar", "produto")`.
#'
#' @return Invisibly returns a list containing `gee0` (model without
#'   treatment-by-time interactions), `gee1` (model with interactions),
#'   `comp` (comparison result when `do_anova = TRUE`, otherwise `NULL`),
#'   and `predictions` (prediction grid). When Wald inference is numerically
#'   unreliable, such as under near-zero dispersion, its statistic and
#'   p-value may be returned as `NA` rather than a spurious inference.
#' @export
gee_di <- function(data_longo,
                   corstr = c('ar1','exchangeable','independence','unstructured'),
                   do_anova = TRUE,
                   factors = 'trat') {

  required <- c(factors, 'time')
  missing_cols <- setdiff(required, names(data_longo))

  if (length(missing_cols) > 0L) {
    stop(
      'Missing required column(s): ',
      paste(missing_cols, collapse = ', '),
      call. = FALSE
    )
  }

  corstr_val <- match.arg(
    tolower(corstr),
    c('ar1','exchangeable','independence','unstructured')
  )

  di_tbl <- data_longo

  if (!all(c('DI','DI_clip','DI_ang') %in% names(di_tbl))) {
    stop(
      'For direct use of gee_di(), calculate DI/DI_clip/DI_ang first or use run_di_pipeline().',
      call. = FALSE
    )
  }

  gee_data <- prepare_gee_data(di_tbl, factors = factors)

  formula0 <- build_factorial_formula(
    factors = factors,
    response = 'DI_ang',
    time = 'time',
    include_factorial_time = FALSE
  )

  formula1 <- build_factorial_formula(
    factors = factors,
    response = 'DI_ang',
    time = 'time',
    include_factorial_time = TRUE
  )

  gee0 <- geepack::geeglm(
    formula0,
    id = .parcela_id,
    data = gee_data,
    corstr = corstr_val,
    family = stats::gaussian()
  )

  gee1 <- geepack::geeglm(
    formula1,
    id = .parcela_id,
    data = gee_data,
    corstr = corstr_val,
    family = stats::gaussian()
  )

  res <- list(
    gee0 = gee0,
    gee1 = gee1,
    comp = if (isTRUE(do_anova)) {
      safe_anova(gee0, gee1, test = 'Wald', factors = factors, time = 'time')
    } else {
      NULL
    },
    predictions = make_pred_grid(di_tbl, gee1, factors = factors)
  )

  invisible(res)
}

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.