Detecting one or more inflection points in a simple numeric vector

前端 未结 1 1379
粉色の甜心
粉色の甜心 2021-02-10 17:51

All,

I\'m looking for a reliable, unsupervised way to detect change points in a relatively short vector. Consider the following two examples:

v1 = c(0.         


        
相关标签:
1条回答
  • 2021-02-10 18:00

    So, this is a simple solution. You can modify the parameters to give you back different (more/fewer, sensitive/insensitive) inflection points (or areas, in the case of your data).

    plot(v2, type="l", col="darkblue", lwd=2)
    # v2 <- smooth(v2, kind="3")  # optional
    lines(v2, lwd=1, col="red")
    d2 <- diff(v2)
    d2 <- d2>0
    d2 <- d2*2 -1 
    k <- 5
    cutoff <- 10
    scores <- sapply(k:(length(d2)-k), FUN=function(i){
      score <- abs(mean(-d2[ i-1:k ], na.rm=T) + mean(d2[ i+0:k ], na.rm=T))
    })
    
    
    scores <- sapply(k:(length(v2)-k), FUN=function(i){
      left <- (v2[sapply(i-1:k, max, 1) ]<v2[i])*2-1
      right <- (v2[sapply(i+1:k, min, length(v2)) ]<v2[i])*2-1
    
      score <- abs(sum(left) + sum(right))
    })
    
    inflections <- (k:(length(v2)-k))[scores>=cutoff]
    
    plot(v2, type="l")
    abline(v=inflections, col="red", lwd=3)
    print(inflections) #  6 11 18 25 32 (missed 51, if you make cutoff=8 it'll catch it...)
    
    0 讨论(0)
提交回复
热议问题