# 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)
PELT.reg.norm(yx,pen=4*log(400)) # returns optimal number as 2 and the locations as c(100,249)
PELT.reg.norm(yx,pen=4*log(400),nprune=TRUE) # returns vector showing how many points are kept during pruing at each iteration of the method
# 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)
PELT.reg.norm(yx,pen=4*log(400)) # returns 400 to show no changepoint were found
PELT.reg.norm(yx,pen=4*log(200),nprune=TRUE) # notice how the number of points kept it steadily increasing compared to the previous example where it almost resets when a true change has been found
Run the code above in your browser using DataLab