0

I am trying to identify local maxima in a data set and am using the following code that I found from LyzandeR:

library(data.table) 
maximums <- function(x) which(x - shift(x, 1) > 0  & x - shift(x, 1, type='lead') > 0) 
maximums(data$Y)

Doing so gives me the correct maxima values for non-noisy data, but for noisy data it incorrectly (for my purposes, it's working as written) identifies small fluctuations as independent peaks. I've been unable to determine how to extend the range for the maximum, i.e. in order for a point to be considered a maximum, it needs to be greater than, say, 5 adjacent values. Is that possible given this code?

Thank you!

GeorgeSBF
  • 33
  • 1
  • 7
  • The data is noisy, and the peaks will be fit after being identified. Therefore in order for it to identify the peaks properly, it needs to determine the local maxima in a given range, not every individual local maxima as some of those are not significant, but merely noise. – GeorgeSBF Oct 05 '16 at 21:35
  • have you googled "R find peaks window"? Are any of these hits useful? http://stats.stackexchange.com/questions/22974/how-to-find-local-peaks-valleys-in-a-series-of-data ; http://stats.stackexchange.com/questions/36309/how-do-i-find-peaks-in-a-dataset ; http://stackoverflow.com/questions/10892803/calculate-peak-values-in-a-plot-using-r – Ben Bolker Oct 05 '16 at 21:53
  • Thank you Ben, but I was specifically wondering if it was possible to do it using the above code. I have a (much messier) version with which it is easy to define the range, but I would've preferred using the above code as it is neater (and I'm writing the code for my supervisor to use, not me). Thanks though! – GeorgeSBF Oct 05 '16 at 22:00

0 Answers0