Nothing
#' @keywords internal
build_freq_by_class <- function(class_num_vec, freq_vec, K) {
fac <- factor(class_num_vec, levels = 0:K)
sums <- tapply(as.numeric(freq_vec), fac, sum, na.rm = TRUE)
vec <- as.numeric(sums)
vec[is.na(vec)] <- 0
stats::setNames(vec, as.character(0:K))
}
#' Czermainski infection intensity index
#'
#' Calculates the generalized infection intensity index proposed by
#' Czermainski (1999) for observations classified into ordered disease
#' classes.
#'
#' Let F0, ..., FJ denote the absolute frequencies in J + 1 ordered
#' disease classes, from absence of disease (F0) to maximum disease
#' intensity (FJ). For j = 1, ..., J, the conditional frequency is
#'
#' \deqn{f_j = (F_j + ... + F_J) / (F_{j-1} + F_j + ... + F_J).}
#'
#' Let p\{j-1,J\} denote the cumulative relative frequency from class
#' j - 1 through class J:
#'
#' \deqn{p_{j-1,J} = (F_{j-1} + ... + F_J) / n.}
#'
#' The weights k_j are defined so that their cumulative values satisfy
#'
#' \deqn{k_1 + ... + k_j = asin(sqrt(j/J)) / (pi/2).}
#'
#' The angular quantity omega is calculated as
#'
#' \deqn{omega = sum_{j=1}^J k_j sqrt(p_{j-1,J}) asin(sqrt(f_j)),}
#'
#' and the infection intensity index is
#'
#' \deqn{I = sin^2(omega).}
#'
#' Calculations are performed in radians. Therefore, when
#' `return_omega = TRUE`, the returned value of omega is in radians.
#'
#' The order of `freq_by_class` is essential: frequencies must be
#' supplied from the lowest disease class (F0) to the highest (FJ).
#'
#' @param freq_by_class Numeric vector of non-negative absolute frequencies
#' ordered from class 0 (absence of disease) to class J (highest disease
#' intensity).
#' @param return_omega Logical. If `FALSE` (default), returns only the
#' infection intensity index I. If `TRUE`, returns both omega and I.
#'
#' @return A numeric infection intensity index between 0 and 1. If
#' `return_omega = TRUE`, a named numeric vector containing `omega`
#' (in radians) and `I`.
#'
#' @references
#' Czermainski, A. B. C. (1999). Generalizacao de um indice de intensidade
#' de infeccao em experimentos de avaliacao de doencas em plantas.
#' \emph{Pesquisa Agropecuaria Brasileira}, 34(9), 1545-1555.
#' \doi{10.1590/S0100-204X1999000900004}
#'
#' @examples
#' freq <- c(19, 9, 4, 5, 6, 7)
#'
#' amaral_czermainski_I(freq)
#'
#' amaral_czermainski_I(freq, return_omega = TRUE)
#'
#' @export
amaral_czermainski_I <- function(freq_by_class, return_omega = FALSE) {
F <- as.numeric(freq_by_class)
if (length(F) < 2L)
stop("Czermainski requires at least two classes (F0 and F1).")
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.")
J <- length(F) - 1L
# Conditional frequencies f_j, j = 1,...,J
f <- vapply(seq_len(J), function(j) {
num <- sum(F[(j + 1L):(J + 1L)])
den <- sum(F[j:(J + 1L)])
if (den == 0) 0 else num / den
}, numeric(1))
# Cumulative probabilities p_(j-1,J)
# p_(0,J) = 1; for j >= 2, p_(j-1,J) is the
# proportion of observations in classes j,...,J.
p_prev <- vapply(seq_len(J), function(j) {
if (j == 1L) {
1
} else {
sum(F[j:(J + 1L)]) / n
}
}, numeric(1))
# Czermainski weights k_j.
# In radians, cumulative k_1+...+k_j =
# asin(sqrt(j/J)) / (pi/2).
cumulative_k <- asin(sqrt(seq_len(J) / J)) / (pi / 2)
k <- c(cumulative_k[1L], diff(cumulative_k))
omega <- sum(
k * sqrt(p_prev) * asin(sqrt(f))
)
I <- sin(omega)^2
if (isTRUE(return_omega)) {
c(omega = omega, I = I)
} else {
unname(I)
}
}
#' @keywords internal
compute_DI_group_cz <- function(df_grp, K) {
v <- build_freq_by_class(df_grp$class_num, df_grp$freq, K)
amaral_czermainski_I(v)
}
#' @keywords internal
compute_DI_group <- function(df_grp, K,
index = c("czermainski", "mckinney"),
scores = NULL) {
index <- match.arg(index)
v <- build_freq_by_class(
df_grp$class_num,
df_grp$freq,
K
)
switch(
index,
czermainski = amaral_czermainski_I(v),
mckinney = mckinney_I(v, scores = scores)
)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.