Search code examples
rtime-seriesthreshold

How to detect sign change (eg, positive to negative) in time series data?


I have data of fluxes over time and would like to obtain a list of times when the flux changed from negative to positive or vice versa. This seems like a simple thing so it probably has a simple answer but I haven't been able to find one with my search terms.

Sample data:

day=c(1,2,3,5,10,20)
flux=c(-2,-4,1,-2,4,11)

I'd like to get something like a vector crossover_times= (2.5, 4, 7.5) to indicate the interpolated days that the change occurred and, ideally, also information indicating which way the sign change went, e.g. sign_changes =(positive, negative, positive).

I could, of course, write a loop to iterate through the data but I'm guessing that R has one or more helpful functions for this. Any suggestions?


Solution

  • diff(sign(flux)) will be non-zero at cross-overs and will have the sign of the crossing:

    updn <- c(0, diff(sign(flux)))
    ix <- which(updn != 0)
    
    (day[ix] + day[ix-1])/2
    ## [1] 2.5 4.0 7.5
    
    sign(updn)[ix]
    ## [1]  1 -1  1
    

    UPDATED: Added sign of crossing. Improvements.