R/CMstat.R

Defines functions CMstat

Documented in CMstat

#' The Cramér-von Mises statistic for the two-parameter exponential distribution.
#'
#' @description
#' The Cramér-von Mises statistic measures the total squared distance between
#' an empirical distribution function (ECDF) and a reference 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 Cramér-von Mises 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
#' CMstat(data, theta_hat, beta_hat)
#'
CMstat <- function(x,theta,beta){
  Xs<-sort(x); n<-length(Xs); S<-numeric()
  for(j in 1:n){
    p <- 1 - exp(-(Xs[j] - theta) / beta)
    #Avoid log(0) or log(1) issues with slight numerical adjustments
    p <- pmin(pmax(p, 1e-10), 1 - 1e-10)
    S[j] <- ( p-((2*j-1)/(2*n)) )^2
  }
  CM <- sum(S) + (1/(12*n))
  return(CM)
}

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.