As find_valleys, stat_peaks and stat_valleys
call find_peaks to search for peaks or valleys, this description
applies to all four functions.
Function find_peaks is a wrapper built onto function
peaks from splus2R, adds support for peak
height thresholds and handles span = NULL and non-finite (including
NA) values differently than splus2R::peaks. Instead of giving an
error when na.rm = FALSE and x contains NA values,
NA values are replaced with the smallest finite value in x.
span = NULL is treated as a special case and selects max(x).
Passing `strict = TRUE` ensures that multiple global and within window
maxima are ignored, and can result in no peaks being returned.#'
Two tests make it possible to ignore irrelevant peaks. One test
(global.threshold) is based on the absolute height of the peaks and
can be used in all cases to ignore globally low peaks. A second test
(local.threshold) is available when the window defined by `span`
does not include all observations and can be used to ignore peaks that are
not locally prominent. In this second approach the height of each peak is
compared to a summary computed from other values within the window of width
equal to span where it was found. In this second case, the reference
value used within each window containing a peak is given by
local.reference. Parameter threshold.range determines how the
bare numeric values passed as argument to global.threshold
and local.threshold are scaled. The default, NULL uses the
range of x. Thresholds for ignoring too small peaks are applied
after peaks are searched for, and threshold values can in some cases result
in no peaks being found. If either threshold is not available (NA)
the returned value is a NA vector of the same length as x.
The local.threshold argument is used as is when
local.reference is "median" or "farthest", i.e., the
same distance between peak and reference is used as cut-off irrespective of
the value of the reference. In cases when the prominence of peaks is
positively correlated with the baseline, a local.threshold that
increases together with increasing computed within window median or
farthest value applies apply a less stringent height requirement in regions
with overall low height. In this case, natural logarithm or square root
weighting can be requested with `local.reference` arguments `"median.log"`,
`"farthest.log"`, `"median.sqrt"`, and `"farthest.sqrt"` as arguments for
local.reference.