[R] strucchange Graph By Week and xts error

Achim Zeileis Ach|m@Ze||e|@ @end|ng |rom u|bk@@c@@t
Thu Mar 7 01:11:44 CET 2019


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]]
>
>



More information about the R-help mailing list