rminima

Finding local maxima and minima in R


I'm trying to create a function to find a "maxima" and "minima". I have the following data:

  y
  157
  144
   80
  106
  124
   46
  207
  188
  190
  208
  143
  170
  162
  178
  155
  163
  162
  149
  135
  160
  149
  147
  133
  146
  126
  120
  151
   74
  122
  145
  160
  155
  173
  126
  172
   93

I have tried this function to find "maxima"

localMaxima <- function(x) {
  # Use -Inf instead if x is numeric (non-integer)
  y <- diff(c(-.Machine$integer.max, x)) > 0L
  rle(y)$lengths
  y <- cumsum(rle(y)$lengths)
  y <- y[seq.int(1L, length(y), 2L)]
  if (x[[1]] == x[[2]]) {
    y <- y[-1]
  }
  y
}

maks <- localMaxima(x)

And funtion to find "minima"

localMinima <- function(x) {
      # Use -Inf instead if x is numeric (non-integer)
      y <- diff(c(.Machine$integer.max, x)) > 0L
      rle(y)$lengths
      y <- cumsum(rle(y)$lengths)
      y <- y[seq.int(1L, length(y), 2L)]
      if (x[[1]] == x[[2]]) {
        y <- y[-1]
      }
      y
    }

    mins <- localMinima(x)

And the result is not 100% right

maks = 1  5  7 10 12 14 16 20 24 27 31 33 35
mins = 3  6  8 11 13 15 19 23 26 28 32 34 36

The result should

maks = 5  7 10 12 14 16 20 24 27 31 33 35
mins = 3  6  8 11 13 15 19 23 26 28 32 34

Finding local maxima and minima in R comes close, but doesn't quite fit.

How can I fix this?

Thanks you very much


Solution

  • You could define two functions like the below which produce the vectors you need:

    library(data.table)
    #shift lags or leads a vector by a certain amount defined as the second argument
    #the default is to lag a vector.
    #The rationale behind the below code is that each local minimum's adjucent
    #values will be greater than itself. The opposite is true for a local 
    #maximum. I think this is what you are trying to achieve and one way to do 
    #it is the following code
    maximums <- function(x) which(x - shift(x, 1) > 0  & x - shift(x, 1, type='lead') > 0)
    minimums <- function(x) which(x - shift(x, 1) < 0  & x - shift(x, 1, type='lead') < 0)
    

    Output:

    > maximums(y)
     [1]  5  7 10 12 14 16 20 24 27 31 33 35
    > minimums(y)
     [1]  3  6  8 11 13 15 19 23 26 28 32 34