Search code examples
rbreakpointslinear-regression

Breakpoints with constant intercept


I'm trying to find a structural break in my time series using the breakpoints() function (in the strucchange package). My goal is to find where is "knot" in my dataset. I'm looking for a procedure which would test all possible knots and choose the one who minimize an information criterion such AIC or BIC. breakpoints() does a good job but I would like to draw a continuous piecewise linear function. This, I would like the intercept to be the same before and after the breakpoint. Is anyone aware of a function or an option to do this ?

On the picture below, the red line is the true model and the blue line is fitted using breakpoints(). I would like a procedure which would fit the true model (no jump at the breakpoint).

See my gist file to reproduce this example.

enter image description here


Solution

  • The 'strucchange' package seems designed to return discontinuous results. You may want to look at packages that are designed the way you imagine the result to be structured. The 'segmented' package is one such.

    require(segmented)
    out.lm<-lm(y~date3,data=df)
    o<-segmented(out.lm, seg.Z= ~date3, psi=list(date3=c(-10)),
         control=seg.control(display=FALSE))
     slope(o)
    #$date3
    #          Est. St.Err. t value CI(95%).l CI(95%).u
    #slope1  0.3964  0.1802   2.199   0.03531    0.7574
    #slope2 -1.6970  0.1802  -9.418  -2.05800   -1.3360
    
    str(fitted(o))
    # Named num [1:60] 1.94 2.34 2.74 3.13 3.53 ...
    # - attr(*, "names")= chr [1:60] "1" "2" "3" "4" ...
    plot(y ~ date3, data=df)
    lines(fitted(o) ~ date3, data=df)
    

    enter image description here