Suppose the graph of (x,y) is first almost linear, then relatively flat, then almost linear again. How to find the middle flat regime?

# diff1 saves the difference of current value and predicted value using existing points from the left side

k<-length(x)

diff1<-c()

for(i in c(2:k)) {

tx<-x[1:i]

ty<-y[1:i]

tfitted<-glm(ty ~ tx)

diff1<-c(diff1, residuals(tfitted)[i])

}

diff1<-c(diff1[1],diff1)

# diff2 saves the difference of current value and predicted value using existing points from the right side

diff2<-c()

for(i in c((k-1):1)) {

tx<-x[i:k]

ty<-y[i:k]

tfitted<-glm(ty ~ tx)

diff2<-c(residuals(tfitted)[1], diff2)

}

diff2<-c(diff2,diff2[k-1])

## No comments:

Post a Comment