[R-SIG-Finance] timeSeries 2 zoo convert
Joshua Ulrich
josh.m.ulrich at gmail.com
Tue Apr 17 19:56:02 CEST 2012
On Tue, Apr 17, 2012 at 12:46 PM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
> Thanks for the reply. The problem is that when I try to plot the zoo
> object, I get numbers instead of dates on the x-axis. I manages to
> correct it with the following function:
>
> ConvertTimeFormat <- function(x) {
> require(zoo)
> require(timeDate)
> temp<-(as.zoo(as.timeSeries(x))
> index(temp)<-as.Date(index(temp))
> return(temp)
> }
>
> Is this a "good" way to do it?
>
You create a timeSeries object, only to convert it to zoo, then
convert the index from timeDate to Date. Why convert to timeSeries at
all, when you essentially destroy all the timeSeries information with
later conversions? Seems very unnecessary, but you didn't say what
type of object is being passed to your function...
Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
R/Finance 2012: Applied Finance with R
www.RinFinance.com
> On 17 April 2012 20:39, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>> On Tue, Apr 17, 2012 at 12:27 PM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
>>> Hello,
>>>
>>> What is the "best" way to convert an timeSeries object to .zoo?
>>>
>> Generally, as.zoo is preferred.
>>
>>> Say I have the following timeSeries object:
>>>
>>>> is(x)
>>> [1] "timeSeries" "structure" "vector"
>>>> head(x)
>>> GMT
>>> DAAA
>>> 1983-01-03 11.77
>>> 1983-01-04 11.79
>>> 1983-01-05 11.79
>>> 1983-01-06 11.74
>>> 1983-01-07 11.74
>>> 1983-01-10 11.75
>>>>
>>>
>>> When I try to convert it to zoo, I can not retain the dates info. I tried:
>>>
>>> a<-as.Date(index( x ))
>>> xz<-as.zoo(x)
>>> index(xz)<-a
>>>
>>> but dates change.
>>>
>>> head(xz)
>>> DAAA
>>> 1970-01-02 11.77
>>> 1970-01-03 11.79
>>> 1970-01-04 11.79
>>> 1970-01-05 11.74
>>> 1970-01-06 11.74
>>> 1970-01-07 11.75
>>>
>> That's because you changed the dates. Look at the output of:
>>
>> R> index(x)
>> [1] 1 2 3 4 5 6 7 8 9 10
>>
>> There's no index.timeSeries method, so index.default is dispatched.
>> xz <- as.zoo(x) should be sufficient.
>>
>>
>>> This used to work with last year's libraries but it seems it doesn't anymore:
>>>
>>> a<-index(as.zoo(x))
>>> xz<-as.zoo(x)
>>> xnew<-aggregate(xz, as.Date, identity)
>>>
>>> Error in as.Date.default(index(x)) :
>>> do not know how to convert 'index(x)' to class "Date"
>>>
>> This is different from your example above. Further, this works for me
>> on R-2.15.0 with timeSeries_2130.92, timeDate_2131.00, and zoo_1.7-7.
>> Your R version is a year old and timeDate and zoo are a couple minor
>> versions behind. Maybe updating will solve your problem?
>>
>>>
>>> Any help, extremely welcome.
>>>
>>> thanks in advance,
>>> Costas
>>>
>>>
>>>> sessionInfo()
>>> R version 2.13.0 (2011-04-13)
>>> Platform: i386-pc-mingw32/i386 (32-bit)
>>>
>>> locale:
>>> [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
>>> States.1252
>>> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>>> [5] LC_TIME=English_United States.1252
>>>
>>> attached base packages:
>>> [1] graphics grDevices datasets stats utils methods base
>>>
>>> other attached packages:
>>> [1] fImport_2110.79 timeSeries_2130.92
>>> [3] timeDate_2130.93 fMarkovSwitching_1.0
>>> [5] Rdonlp2_0.3-1 rcom_2.2-3.1
>>> [7] rscproxy_1.3-1 quantmod_0.3-17
>>> [9] TTR_0.21-0 Defaults_1.1-1
>>> [11] PerformanceAnalytics_1.0.3.2 xts_0.8-2
>>> [13] zoo_1.7-4
>>>
>>> loaded via a namespace (and not attached):
>>> [1] grid_2.13.0 lattice_0.19-33 tools_2.13.0
>>>>
>>>
>>>
>>> --
>>>
>>> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>>> |c|o|s|t|a|s|@|v|o|r|l|o|w|.|o|r|g|
>>> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>>>
>>
>>
>> --
>> Joshua Ulrich | FOSS Trading: www.fosstrading.com
>>
>> R/Finance 2012: Applied Finance with R
>> www.RinFinance.com
>
>
>
> --
>
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> |c|o|s|t|a|s|@|v|o|r|l|o|w|.|o|r|g|
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
More information about the R-SIG-Finance
mailing list