Skip to content
Prev 378824 / 398502 Next

strucchange Graph By Week and xts error

On Thu, 7 Mar 2019, Sparks, John wrote:

            
Yes, and hence breakpoints() re-uses that scaling. As I wrote in my 
previous mail you either have to squeeze your data into a regular grid of 
52 weekly observations per year or you have to keep track of the time 
index yourself. See below.
If you want to use your own non-ts time scale, you can use "xts" (as you 
do above) or "zoo" (as I do below) or keep thing in a plain "data.frame" 
(or similar). Then you just have to index the times with the breakpoints 
or their confidence intervals respectively:

## zoo series
x <- zoo(Nile, seq(ymd('2012-01-01'),ymd('2013-11-30'),by='weeks'))

## breakpoints and confidence intervals
bp <- breakpoints(x ~ 1)
ci <- confint(bp, breaks = 1)

## map time index
cix <- time(x)[ci$confint]

## visualize
plot(x)
abline(v = cix[2], lty = 2)
arrows(cix[1], min(x), cix[3], min(x),
   col = 2, angle = 90, length = 0.05, code = 3)

Above, the $confint is a vector. If it is a matrix (due to more than one 
breakpoint) the code needs to be tweaked to make cix also a matrix and 
then use cix[,i] rather than cix[i] for i = 1, 2, 3.