# Example of multiple changes in regression at 100,250 in simulated data with zero-mean normal errors
set.seed(1)
x=1:400
y=c(0.01*x[1:100],3.5-0.02*x[101:250],-15+0.05*x[251:400])
ynoise=y+rnorm(400,0,0.2)
yx=cbind(ynoise,1,x)
binseg.reg.norm(yx,Q=5,pen=4*log(400)) # returns optimal number as 2 and the locations as c(100,250)
binseg.reg.norm(yx,Q=1,pen=4*log(400)) # returns optimal number as 1 as this is the maximum number of changepoints it can find. If you get the maximum number, you need to increase Q until this is not the case.
# Example no change in regression
set.seed(10)
x=1:400
y=0.01*x
ynoise=y+rnorm(400,0,0.2)
yx=cbind(ynoise,1,x)
binseg.reg.norm(yx,Q=5,pen=4*log(400)) # returns optimal number as 0
Run the code above in your browser using DataLab