Reputation: 5366
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.
Upvotes: 1
Views: 2236
Reputation: 263301
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)
Upvotes: 2
Reputation: 57686
A continuous piecewise linear fit is also called a linear spline, and can be fit with bs
in the splines
package (comes with base R).
lm(y ~ bs(x, deg=1, df, knots), ...)
The breakpoints are called knots, and you have to specify them via either the knots
argument or the df
argument (which chooses knots based on the quantiles of x
).
You can also do it manually; linear splines are particularly simple to code up.
lm(y ~ x + pmax(0, x - k1) + pmax(0, x - k2), ...)
where k1
and k2
are the knots. Add more to taste.
Upvotes: 0