roll_hampel | R Documentation |
Fast, center-aligned hampel filter using C++/Rcpp.
The Hampel filter is a robust outlier detector using Median Absolute Deviation (MAD).
Additional performance gains can be achieved by skipping increment
values between calculations.
roll_hampel(x, n, increment = 1)
x |
an R numeric vector |
n |
integer window size |
increment |
integer shift to use when sliding the window to the next location |
Unlike the version in the pracma package, this version does not return the corrected timeseries. Instead, it returns a vector of values that can be tested against different threshold values. Higher values in the return are associated with a higher likelihood that the associated point is an outlier when compared with its neighbors. Outliers can be picked out by comparing the return values against some threshold as seen in the example.
Also unlike the pracma version, n
is interpreted as the full window length
and will be increased by one if necessary to have a window representing an odd number of indices.
A vector of values of the same length as x
.
A pure R version of the filter is found in the pracma package.
roll_median
a <- sin(0.1*seq(100))
a[20] <- 50
b <- roll_hampel(a,10)
threshold <- 6
which(b > threshold)
## Not run:
require(microbenchmark)
require(pracma)
microbenchmark(hampel(a,10), roll_hampel(a,10), times=10)
# Unit: microseconds
# expr min lq median uq max neval
# hampel(a, 10) 7610.688 7784.374 8037.4035 9453.928 16176.535 10
# roll_hampel(a, 10) 36.530 37.443 58.7165 65.418 90.403 10
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.