R/mckinney.R

Defines functions mckinney_I

Documented in mckinney_I

#' McKinney disease index
#'
#' Calculates the disease index described by McKinney from absolute
#' frequencies distributed among ordered disease classes.
#'
#' The index is calculated as
#'
#' \deqn{I = sum(F_j x_j) / (n x_max),}
#'
#' where F_j is the absolute frequency in class j, x_j is the score
#' assigned to that class, n is the total number of observations, and
#' x_max is the maximum score.
#'
#' If `scores = NULL`, equally spaced scores from 0 to J are used.
#' The returned index is expressed on a 0 to 1 scale rather than as
#' a percentage.
#'
#' @param freq_by_class Numeric vector of non-negative absolute frequencies
#'   ordered from the lowest to the highest disease class.
#' @param scores Optional numeric vector containing the scores assigned to
#'   the disease classes. It must have the same length as
#'   `freq_by_class`. If `NULL`, scores 0:J are used.
#'
#' @return A numeric disease index between 0 and 1.
#'
#' @references
#' McKinney, H. H. (1923). Influence of soil temperature and moisture
#' on infection of wheat seedlings by Helminthosporium sativum.
#' \emph{Journal of Agricultural Research}, 26, 195--217.
#' \url{https://babel.hathitrust.org/cgi/pt?id=uc1.b3610175}
#'
#' @examples
#' freq <- c(19, 9, 4, 5, 6, 7)
#' mckinney_I(freq)
#'
#' mckinney_I(
#'   c(10, 5, 3, 2, 1),
#'   scores = c(0, 0.75, 1, 2, 3)
#' )
#'
#' @export
mckinney_I <- function(freq_by_class, scores = NULL) {
  F <- as.numeric(freq_by_class)

  if (length(F) < 2L)
    stop("McKinney requires at least two classes.")

  if (anyNA(F))
    stop("There are NA values in class frequencies.")

  if (any(!is.finite(F)))
    stop("Class frequencies must be finite.")

  if (any(F < 0))
    stop("Class frequencies must be non-negative.")

  n <- sum(F)

  if (n <= 0)
    stop("The sum of class frequencies must be greater than zero.")

  if (is.null(scores)) {
    scores <- seq.int(0, length(F) - 1L)
  } else {
    scores <- as.numeric(scores)

    if (length(scores) != length(F))
      stop("scores must have the same length as freq_by_class.")

    if (anyNA(scores) || any(!is.finite(scores)))
      stop("scores must contain only finite non-missing values.")

    if (any(scores < 0))
      stop("scores must be non-negative.")

    if (is.unsorted(scores, strictly = FALSE))
      stop("scores must be ordered from lowest to highest.")
  }

  x_max <- max(scores)

  if (x_max <= 0)
    stop("The maximum score must be greater than zero.")

  I <- sum(F * scores) / (n * x_max)

  unname(I)
}

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.