[R] ts - unit conversion

michaell taylor mt at michaelltaylor.com
Tue Sep 30 17:50:27 CEST 2003


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




More information about the R-help mailing list