I've been using R for a while, but now find myself needing to understand time-series objects for a short course I am teaching. I am putting together some daily financial datasets for illustration, but having some trouble in aggregating the data to months or weeks. I am getting a "cannot change frequency from 1 to 12" message. I am not sure that aggregation is where I want to go anyway. Assuming for the moment that I had one full year of data, I would like the observations to relate to real months with various numbers of days, not simply 1/12th of a year. It is unclear to me how sophisticated the aggregator is in this regard. I have V&R but it seems there may be some SPlus/R differences here. Any suggestions for documents that addresses these issues would be wonderful. Thanks.> ibm <- get.hist.quote('ibm',start='2003-01-01')trying URL `http://chart.yahoo.com/table.csv?s=ibm&a=11&b=31&c=2002&d=8&e=28&f=2003&g=d&q=q&y=0&z=ibm&x=.csv' Content type `application/octet-stream' length unknown opened URL ........ downloaded 8990 bytes time series starts 2002-12-30 time series ends 2003-09-25 Just to check what I have....> attributes(ibm)$dim [1] 270 4 $dimnames $dimnames[[1]] NULL $dimnames[[2]] [1] "Open" "High" "Low" "Close" $tsp [1] 37621 37890 1 $class [1] "mts" "ts" Attempting to pull monthly means .....> aggregate(ibm,12,mean)Error in aggregate.ts(ibm, 12, mean) : cannot change frequency from 1 to 12 I thought perhaps I had a non-integer number of months in the data which may be causing problems.> length(ibm)[1] 1080> 1080/12[1] 90>Alternatively...making the data coordinate to real months...> ibm <- get.hist.quote('ibm',start='2003-01-01',end='2003-08-31')trying URL `http://chart.yahoo.com/table.csv?s=ibm&a=11&b=31&c=2002&d=7&e=30&f=2003&g=d&q=q&y=0&z=ibm&x=.csv' Content type `application/octet-stream' length unknown opened URL ....... downloaded 8082 bytes time series starts 2002-12-30 time series ends 2003-08-28> aggregate(ibm,12,mean)Error in aggregate.ts(ibm, 12, mean) : cannot change frequency from 1 to 12
Financial data, as you point out, is generally irregular, which is in essence what prompted the devlopment of the irregular time-series (its) package, which is posted on CRAN (v0.1.2 posted today, incidentally). In the its class, the time-stamps of the rows of a matrix are represented using the POSIXct class, so irregularity can be represented accurately. Inspection of get.hist.quote() shows that it would be quite straightforward to adapt it to return an 'its'. I would do so myself, but have no spare time in the short term to do this (and I'm behind a proxy/firewall which seems to complicate matters). Giles> -----Original Message----- > From: michaell taylor [mailto:mt at michaelltaylor.com] > Sent: 30 September 2003 16:51 > To: R-help at stat.math.ethz.ch > Subject: [R] ts - unit conversion > > > > I've been using R for a while, but now find myself needing to > understand > time-series objects for a short course I am teaching. I am putting > together some daily financial datasets for illustration, but > having some > trouble in aggregating the data to months or weeks. I am getting a > "cannot change frequency from 1 to 12" message. > > I am not sure that aggregation is where I want to go anyway. Assuming > for the moment that I had one full year of data, I would like the > observations to relate to real months with various numbers of > days, not > simply 1/12th of a year. It is unclear to me how sophisticated the > aggregator is in this regard. > > I have V&R but it seems there may be some SPlus/R differences > here. Any > suggestions for documents that addresses these issues would be > wonderful. Thanks. > > > ibm <- get.hist.quote('ibm',start='2003-01-01') > trying URL > `http://chart.yahoo.com/table.csv?s=ibm&a=11&b=31&c=2002&d=8&e > =28&f=2003&g=d&q=q&y=0&z=ibm&x=.csv' > Content type `application/octet-stream' length unknown > opened URL > ........ > downloaded 8990 bytes > > time series starts 2002-12-30 > time series ends 2003-09-25 > > Just to check what I have.... > > > attributes(ibm) > $dim > [1] 270 4 > > $dimnames > $dimnames[[1]] > NULL > > $dimnames[[2]] > [1] "Open" "High" "Low" "Close" > > > $tsp > [1] 37621 37890 1 > > $class > [1] "mts" "ts" > > Attempting to pull monthly means ..... > > > aggregate(ibm,12,mean) > Error in aggregate.ts(ibm, 12, mean) : cannot change > frequency from 1 to > 12 > > I thought perhaps I had a non-integer number of months in the > data which > may be causing problems. > > > length(ibm) > [1] 1080 > > 1080/12 > [1] 90 > > > > Alternatively...making the data coordinate to real months... > > ibm <- get.hist.quote('ibm',start='2003-01-01',end='2003-08-31') > trying URL > `http://chart.yahoo.com/table.csv?s=ibm&a=11&b=31&c=2002&d=7&e > =30&f=2003&g=d&q=q&y=0&z=ibm&x=.csv' > Content type `application/octet-stream' length unknown > opened URL > ....... > downloaded 8082 bytes > > time series starts 2002-12-30 > time series ends 2003-08-28 > > aggregate(ibm,12,mean) > Error in aggregate.ts(ibm, 12, mean) : cannot change > frequency from 1 to > 12 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >********************************************************************** This is a commercial communication from Commerzbank AG.\ \ T...{{dropped}}
Change the line in get.hist.quote that pastes the url together so that g=d (i.e. daily) is replaced with g=m (i.e. monthly). That will cause monthly data to be downloaded. With this new get.hist.quote try this: ibm <- get.hist.quote("ibm") ibm <- ts( ibm[!is.na(rowSums(ibm)),], end=c(2003,8), freq=12 ) --- On Tue 09/30, michaell taylor < mt at michaelltaylor.com > wrote: I've been using R for a while, but now find myself needing to understand time-series objects for a short course I am teaching. I am putting together some daily financial datasets for illustration, but having some trouble in aggregating the data to months or weeks. I am getting a "cannot change frequency from 1 to 12" message. I am not sure that aggregation is where I want to go anyway. Assuming for the moment that I had one full year of data, I would like the observations to relate to real months with various numbers of days, not simply 1/12th of a year. It is unclear to me how sophisticated the aggregator is in this regard. I have V&R but it seems there may be some SPlus/R differences here. Any suggestions for documents that addresses these issues would be wonderful. Thanks.> ibm <- get.hist.quote('ibm',start='2003-01-01')trying URL `http://chart.yahoo.com/table.csv?s=ibm&a=11&b=31&c=2002&d=8&e=28&f=2003&g=d&q=q&y=0&z=ibm&x=.csv' Content type `application/octet-stream' length unknown opened URL ........ downloaded 8990 bytes time series starts 2002-12-30 time series ends 2003-09-25 Just to check what I have....> attributes(ibm)$dim [1] 270 4 $dimnames $dimnames[[1]] NULL $dimnames[[2]] [1] "Open" "High" "Low" "Close" $tsp [1] 37621 37890 1 $class [1] "mts" "ts" Attempting to pull monthly means .....> aggregate(ibm,12,mean)Error in aggregate.ts(ibm, 12, mean) : cannot change frequency from 1 to 12 I thought perhaps I had a non-integer number of months in the data which may be causing problems.> length(ibm)[1] 1080> 1080/12[1] 90>Alternatively...making the data coordinate to real months...> ibm <- get.hist.quote('ibm',start='2003-01-01',end='2003-08-31')trying URL `http://chart.yahoo.com/table.csv?s=ibm&a=11&b=31&c=2002&d=7&e=30&f=2003&g=d&q=q&y=0&z=ibm&x=.csv' Content type `application/octet-stream' length unknown opened URL ....... downloaded 8082 bytes time series starts 2002-12-30 time series ends 2003-08-28> aggregate(ibm,12,mean)Error in aggregate.ts(ibm, 12, mean) : cannot change frequency from 1 to 12 _______________________________________________ No banners. No pop-ups. No kidding. Introducing My Way - http://www.myway.com