I'm trying to do a moving average (similar to roll_mean in RcppRoll
) except that, at each window, I'd like to trim the outliers (e.g. just take the 5th-95th percentile of values).
As an example, given a rolling window of
v <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
And given that I'd like the 10th-90th percentile of values, I should get an answer of 5.5
(1 and 10 will be excluded, and a mean is taken on the rest of the values (2 to 9).
I'm unfortunately not able to use functions like RcppRoll::roll_mean
for this, since the trimming needs to be done at each rolling window.
I was able to do this by feeding a custom mean function to zoo::rollapply
- but it's simply working too slowly for my use case (>1e6 rows).
I've looked at various packages that support rolling functions (e.g. RcppRoll
, zoo
, TTR
, caTools
, roll
etc.) but none of them seem to support this trimming functionality.
I'm thinking of resorting to using Rcpp to build a custom, fast roll function, but am relatively unfamiliar with that framework. Not sure if there are better solutions.
Any help will be appreciated here.