R/ADstat.R

Defines functions ADstat

Documented in ADstat

#' The Anderson–Darling (AD) statistic for the two-parameter exponential distribution.
#'
#' @description
#' The Anderson–Darling statistic, called the AD statistic, measures the weighted distance between
#' an empirical cumulative distribution function (ECDF) and a theoretical cumulative distribution function (CDF).
#'
#' @param x      a data vector (e.g., failure time, waiting time, etc.)
#' @param theta  location parameter, where \eqn{\theta > 0}.
#' @param beta   scale parameter, where \eqn{\beta > 0} and \eqn{rate=1/\beta}.
#'
#' @import stats
#'
#' @returns      A value of the AD statistic.
#'
#' @references D'Agostino, R. B., & Stephens, M. A. (Eds.). (1986). Goodness-of-fit techniques. Marcel Dekker. (ISBN 0-8247-8705-6).
#'
#' @export
#'
#' @examples
#' # Proschan (1963, Table I) has given some samples of data,
#' # consisting of intervals between failures of air conditioning equipment in aircraft
#' data <- c(12,21,26,27,29,29,48,57,59,70,74,153,326,386,502)
#' theta_hat <- min(data)           #estimate theta
#' beta_hat <- mean(data)-min(data) #estimate beta
#' ADstat(data, theta_hat, beta_hat)
#'
ADstat <- function(x, theta, beta){
  n <- length(x); Xs <- sort(x);  L <- numeric()
  for(j in 1:n){
    p1 <- ptpexp(Xs[j],theta,beta)
    #Avoid log(0) or log(1) issues with slight numerical adjustments
    p1 <- pmin(pmax(p1, 1e-10), 1 - 1e-10)
    p2 <- ptpexp(Xs[n+1-j],theta,beta)
    #Avoid log(0) or log(1) issues with slight numerical adjustments
    p2 <- pmin(pmax(p2, 1e-10), 1 - 1e-10)
    L[j] <- ((2*j)-1)*(log(p1) + log(1-p2))
  }
  AD <- -n-(1/n)*sum(L)
  return(AD)
}

Try the twopexp package in your browser

Any scripts or data that you put into this service are public.

twopexp documentation built on July 17, 2026, 9:07 a.m.