find_peaks | R Documentation |
These functions find peaks (maxima) and valleys (minima) in a
numeric vector, using a user selectable span and global and local size
thresholds, returning a logical
vector.
find_peaks(
x,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
span = 3,
strict = FALSE,
na.rm = FALSE
)
find_valleys(
x,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
span = 3,
strict = FALSE,
na.rm = FALSE
)
x |
numeric vector. |
global.threshold |
numeric A value belonging to class |
local.threshold |
numeric A value belonging to class |
local.reference |
character One of |
threshold.range |
numeric vector If of length 2 or a longer vector
|
span |
odd positive integer A peak is defined as an element in a
sequence which is greater than all other elements within a moving window of
width |
strict |
logical flag: if |
na.rm |
logical indicating whether |
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
.
A vector of logical values of the same length as x
. Values
that are TRUE correspond to local peaks in vector x
and can be used
to extract the rows corresponding to peaks from a data frame.
The default for parameter strict
is FALSE
in functions
find_peaks()
and find_valleys()
, while it is
strict = TRUE
in peaks
.
peaks
.
Other peaks and valleys functions:
find_spikes()
# lynx is a time.series object
lynx_num.df <-
try_tibble(lynx,
col.names = c("year", "lynx"),
as.numeric = TRUE) # years -> as numeric
which(find_peaks(lynx_num.df$lynx, span = 5))
which(find_valleys(lynx_num.df$lynx, span = 5))
lynx_num.df[find_peaks(lynx_num.df$lynx, span = 5), ]
lynx_num.df[find_peaks(lynx_num.df$lynx, span = 51), ]
lynx_num.df[find_peaks(lynx_num.df$lynx, span = NULL), ]
lynx_num.df[find_peaks(lynx_num.df$lynx,
span = 15,
global.threshold = 2/3), ]
lynx_num.df[find_peaks(lynx_num.df$lynx,
span = 15,
global.threshold = I(4000)), ]
lynx_num.df[find_peaks(lynx_num.df$lynx,
span = 15,
local.threshold = 0.5), ]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.