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
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]]
Hi John, You seem to have 1569 days of data, so perhaps you can get around your axis problem like this: plot(Nile,xaxt="n",xlab="Week") ... axis(1,at=seq(0,200,50),labels=seq(0,200,50)*7) (untested) Jim On Thu, Mar 7, 2019 at 10:46 AM Sparks, John <jspark4 at uic.edu> 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 > > 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]] > > ______________________________________________ > R-help at 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.
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 isUnfortunately, 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]] > >
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.
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)
________________________________
From: Achim Zeileis <Achim.Zeileis at uibk.ac.at>
Sent: Wednesday, March 6, 2019 6:11 PM
To: Sparks, John
Cc: r-help at 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]]