R/find.area.R

Defines functions find.area

Documented in find.area

#' find area under the curve
#'
#' @param y
#' @param x
#' @param mode
#' @param config
#'
#' @return
#' @export
#'
#' @examples
find.area <- function(y, x = seq_along(y), mode = 'fit.smooth', config) {
    # Ver 3.3.4

    curve <- fit.curve(y, mode = mode, config = config)

    if(config$area.method == 'full.peak') {
        valleys <- get.2valleys(curve)
        if(anyNothing(valleys)) return(N)
        min.x <- min(valleys)
        max.x <- max(valleys)
        min.y <- curve[min.x]
        max.y <- curve[max.x]
    } else if (config$ area.method == 'half.peak') {
        valley <- get.valley(curve)
        peak <- get.peak(curve)
        if(is.nothing(valley)|is.nothing(peak)) return(NULL)
        min.x <- ifelse0(valley < peak, valley, peak)
        max.x <- ifelse0(valley < peak, peak, valley)
        min.y <- curve[valley]
        max.y <- min.y
    }
    area <- NULL

    # VER 3.4.2
    baseline <- get.baseline(x, min.x, max.x, min.y, max.y)
    area <- sum((y - baseline)*diff(x))
    if (is.null(area)) {
        cat(sprintf('area fit failed, no area found!'))
        return(NULL)
    }
    return(area)
}
yanxianUCSB/yxhelper documentation built on April 20, 2020, 4:09 p.m.