R/set_critical_value.r

Defines functions set_critical_value

Documented in set_critical_value

#' Set outlier critical value
#'
#' Set outlier critical value using the Ljung algorithm as given in
#' Ljung, G. M. (1993). On outlier detection in time series. 
#' Journal of Royal Statistical Society B 55, 559-567.
#' 
#'
#' @param number_observations number of observations tested for outliers  
#' @param cv_alpha alpha for critical value
#' @return outlier critical value generated by the algorithm given in Ljung (1993). 
#'         The critical value in X-13 is different as it is adjusted to allow for smaller values 
#'         to approximate the normal distribution.
#' @examples
#' this_critical_value <- set_critical_value(12, 0.025)
#' @export
set_critical_value <- function(number_observations, cv_alpha = 0.01) {
    # Brian Monsell Version 1.4 3/29/2021
    pmod <- 2 - sqrt(1 + cv_alpha)
    acv <- sqrt(2 * log(number_observations))
    bcv <- acv - (log(log(number_observations)) + log(2 * 2 * pi))/(2 * acv)
    xcv <- -log(-0.5 * log(pmod))
    setcvl <- (xcv/acv) + bcv
    return(setcvl)
}
bcmonsell/airutilities documentation built on May 16, 2022, 3:23 p.m.