[R] strucchange Graph By Week and xts error

Achim Zeileis Ach|m@Ze||e|@ @end|ng |rom u|bk@@c@@t
Thu Mar 7 02:06:16 CET 2019


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

> Thanks to Achim's direction I now have a re-producible example.
>
> The code below creates a ts object.  The x scale of the last graph runs 
> from 0 to 700.

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.

> So just need a way to get that scale to show the weeks (or some summary 
> of them).
>
> Thanks a bunch.
> --JJS
>
>
> library(strucchange)
> library(xts)
> library(lubridate)
>
> #rm(list=ls())
>
>
> data("Nile")
> class(Nile)
> plot(Nile)
> bp.nile <- breakpoints(Nile ~ 1)
> ci.nile <- confint(bp.nile, breaks = 1)
> lines(ci.nile)
>
>
> dfNile<-data.frame(as.numeric(Nile))
> dfNile$week<-seq(ymd('2012-01-01'),ymd('2013-11-30'),by='weeks')
> tsNile <- as.xts(x = dfNile[, -2], order.by = dfNile$week)
> tsNile<-as.ts(tsNile)
>
>
> plot(tsNile)
> bp.tsNile <- breakpoints(tsNile ~ 1)
> ci.tsNile <- confint(bp.tsNile, breaks = 1)
> lines(ci.tsNile)

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.

>
>
>
>
> ________________________________
> From: Achim Zeileis <Achim.Zeileis using uibk.ac.at>
> Sent: Wednesday, March 6, 2019 6:11 PM
> To: Sparks, John
> Cc: r-help using r-project.org
> Subject: Re: [R] strucchange Graph By Week and xts error
>
> On Thu, 7 Mar 2019, Sparks, John wrote:
>
>> Hi R Helpers,
>>
>> I am doing some work at identifying change points in time series data.
>> A very nice example is given in the R Bloggers post
>>
>> https://www.r-bloggers.com/a-look-at-strucchange-and-segmented/
>>
>> The data for the aswan dam in that example is yearly.  My data is
>> weekly.  I ran the code switching the data for the analysis to my data
>> and it worked, but the scale of the line chart is not sensible.  I have
>> 225 weekly observations and the x-axis of the line graph shows numbers
>> from 0 to over 1500.  The information on the ts object is
>
> Unfortunately, breakpoints() can only deal automatically with "ts" time
> series not with zoo/xts/... So either you can squeeze your data onto a
> regular "ts" grid which may work in the case of weekly data. Or you need
> to handle the time index "by hand". See
>
> https://stackoverflow.com/questions/43243548/strucchange-not-reporting-breakdates/43267082#43267082
>
> for an example for this.
>
> As for the as.xts() error below. This is because dfNile[, -2] is still a
> "ts" object and then as.xts() sets up "order.by" for you.
>
> Either you use xts() rather than as.xts() or you make the first column in
> the data.frame "numeric" rather than "ts", e.g., by starting the
> transformation with:
>
> dfNile<-data.frame(as.numeric(Nile))
>
>
>> Start=1
>> End=1569
>> Frequency=0.1428...
>>
>> I can't share the data because it is proprietary.
>>
>> Wanting to be a good member of the list, I attempted to put weekly
>> increments on the Nile data so I could reproduce the x axis of the chart
>> with the axis scale that I am seeing.  Unfortunately, in doing so I got
>> another error that I don't understand.
>>
>>
>>
>> library(strucchange)
>> library(lubridate)
>> library(xts)
>>
>> # example from R-Blog runs fine
>> data(???Nile???)
>> plot(Nile)
>> bp.nile <- breakpoints(Nile ~ 1)
>> ci.nile <- confint(bp.nile, breaks = 1)
>> lines(ci.nile)
>>
>> #problem comes in here
>> dfNile<-data.frame(Nile)
>> dfNile$week<-seq(ymd('2012-01-01'),ymd('2013-11-30'),by='weeks')
>> tsNile<-as.xts(x=dfNile[,-2],order.by=dfNile$week)
>>
>> Error in xts(x.mat, order.by = order.by, frequency = frequency(x), ...) :
>>  formal argument "order.by" matched by multiple actual arguments
>>
>>
>> Can somebody help me to put together the ts object with weeks so that I can demonstrate the problem with the scale on the x-axis and then try to get some help with that original problem?
>>
>> Much appreciated.
>> --John Sparks
>>
>>
>>
>>
>>
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list