[R] ts - unit conversion

Gabor Grothendieck ggrothendieck at myway.com
Tue Sep 30 18:53:36 CEST 2003



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




More information about the R-help mailing list