R/helpers_auc.R

Defines functions select_progress_metric disease_progress_by_parcela audps_by_parcela test_auc_robust summary_auc_by_trat auc_by_parcela

Documented in audps_by_parcela

#' @keywords internal
auc_by_parcela <- function(di_tbl, factors = 'trat') {

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

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

  df <- add_parcela_id(di_tbl, factors = factors) |>
    dplyr::filter(is.finite(DI), is.finite(time)) |>
    dplyr::arrange(.unit_id, time)

  trapz <- function(x, y) {
    if (length(x) < 2L) return(NA_real_)
    sum((utils::head(y, -1) + utils::tail(y, -1)) * diff(x) / 2)
  }

  id_cols <- unique(c(
    intersect(c('block', 'unit'), names(df)),
    factors
  ))

  df |>
    dplyr::group_by(.unit_id) |>
    dplyr::summarize(
      dplyr::across(
        tidyselect::all_of(id_cols),
        dplyr::first
      ),
      n_pts = dplyr::n(),
      AUC = trapz(time, DI),
      .groups = 'drop'
    ) |>
    dplyr::select(-.unit_id)
}
#' @keywords internal
summary_auc_by_trat <- function(auc_tbl, factors = 'trat') {

  required <- c(factors, 'AUC')
  missing_cols <- setdiff(required, names(auc_tbl))

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

  auc_tbl <- normalize_factors(
    auc_tbl,
    factors = factors
  )

  auc_tbl |>
    dplyr::group_by(
      dplyr::across(dplyr::all_of(factors))
    ) |>
    dplyr::summarize(
      n    = dplyr::n(),
      mean = mean(AUC, na.rm = TRUE),
      sd   = stats::sd(AUC, na.rm = TRUE),
      se   = sd / sqrt(n),
      lwr  = mean + stats::qt(0.025, df = pmax(n - 1, 1)) * se,
      upr  = mean + stats::qt(0.975, df = pmax(n - 1, 1)) * se,
      .groups = 'drop'
    ) |>
    dplyr::arrange(
      dplyr::across(dplyr::all_of(factors))
    )
}

test_auc_robust <- function(auc_tbl) {
  stopifnot(all(c("trat","AUC") %in% names(auc_tbl)))
  auc_tbl <- dplyr::filter(auc_tbl, is.finite(AUC))
  kw <- stats::kruskal.test(AUC ~ trat, data = auc_tbl)
  pw <- stats::pairwise.wilcox.test(auc_tbl$AUC, auc_tbl$trat, p.adjust.method = "holm", exact = FALSE)
  pairs_df <- NULL
  if (!is.null(pw$p.value)) {
    m <- pw$p.value
    pairs_df <- data.frame(
      trat1 = rep(rownames(m), times = ncol(m)),
      trat2 = rep(colnames(m), each  = nrow(m)),
      p_adj = as.vector(m),
      row.names = NULL
    )
    pairs_df <- tidyr::drop_na(pairs_df)
  }
  list(kruskal = kw, pairs = pairs_df)
}

#' Calculate AUDPS by experimental unit
#'
#' Calculates the Area Under the Disease Progress Stairs (AUDPS)
#' according to Simko and Piepho (2012), using disease intensity
#' values on the 0--1 scale.
#'
#' @param di_tbl Data frame containing trat, time, DI and either an
#'   explicit unit column or block for deriving experimental units.
#' @return Data frame with one AUDPS value per experimental unit.
#' @keywords internal
audps_by_parcela <- function(di_tbl, factors = 'trat') {

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

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

  df <- add_parcela_id(di_tbl, factors = factors) |>
    dplyr::filter(is.finite(DI), is.finite(time)) |>
    dplyr::arrange(.unit_id, time)

  calc_audps <- function(x, y) {
    n <- length(x)
    if (n < 2L) return(NA_real_)

    auc <- sum(
      (utils::head(y, -1) + utils::tail(y, -1)) * diff(x) / 2
    )

    D <- max(x) - min(x)

    auc + ((y[1] + y[n]) / 2) * (D / (n - 1))
  }

  id_cols <- unique(c(
    intersect(c('block', 'unit'), names(df)),
    factors
  ))

  df |>
    dplyr::group_by(.unit_id) |>
    dplyr::summarize(
      dplyr::across(
        tidyselect::all_of(id_cols),
        dplyr::first
      ),
      n_pts = dplyr::n(),
      AUDPS = calc_audps(time, DI),
      .groups = 'drop'
    ) |>
    dplyr::select(-.unit_id)
}
#' @keywords internal
disease_progress_by_parcela <- function(di_tbl, factors = 'trat') {

  pc <- auc_by_parcela(di_tbl, factors = factors) |>
    dplyr::mutate(AUDPC = AUC)

  ps <- audps_by_parcela(di_tbl, factors = factors)

  id_cols <- unique(c(
    intersect(c('block', 'unit'), names(pc)),
    factors
  ))

  ps <- ps |>
    dplyr::select(
      tidyselect::all_of(id_cols),
      AUDPS
    )

  dplyr::left_join(
    pc,
    ps,
    by = id_cols
  )
}
#' @keywords internal
select_progress_metric <- function(progress_tbl,
                                   metric = c("audpc", "audps")) {

  metric <- match.arg(metric)

  stopifnot(all(c("AUDPC", "AUDPS") %in% names(progress_tbl)))

  selected <- switch(
    metric,
    audpc = progress_tbl$AUDPC,
    audps = progress_tbl$AUDPS
  )

  progress_tbl$AUC <- selected
  progress_tbl
}

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.