[R-SIG-Finance] How to compare two asynchroneous xts time series?

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 22 14:44:36 CEST 2009


The dyn package facilitates regression of time series
by automatically intersecting the components.  It
currently does not work with xts but its easy to
convert those to zoo just prior to performing the
regression. Just preface lm with dyn$ as shown.
It also works with glm and other regression functions
that are sufficiently similar to lm.

> Lines1 <- "2008-06-02 09:00:00,5007.0
+ 2008-06-02 09:01:00,5010.0
+ 2008-06-02 09:02:00,5014.0
+ 2008-06-02 09:03:00,5012.5
+ 2008-06-02 09:04:00,5013.5
+ 2008-06-02 09:05:00,5009.5
+ 2008-06-02 09:06:00,5007.0
+ 2008-06-02 09:07:00,5006.5
+ 2008-06-02 09:08:00,5008.5
+ 2008-06-02 09:09:00,5004.5"
>
> Lines2 <- "2008-06-02 09:01:00,7115.0
+ 2008-06-02 09:03:00,7117.0
+ 2008-06-02 09:05:00,7111.0
+ 2008-06-02 09:07:00,7107.0
+ 2008-06-02 09:09:00,7102.5"
>
> library(xts)
> z1 <- read.zoo(textConnection(Lines1), sep = ",", tz = "")
> x1 <- as.xts(z1)
> z2 <- read.zoo(textConnection(Lines2), sep = ",", tz = "")
> x2 <- as.xts(z2)
>
> # convert xts to zoo
> Z1 <- zoo(coredata(x1), time(x1))
> Z2 <- zoo(coredata(x2), time(x2))
> library(dyn)
> dyn$lm(Z2 ~ Z1)

Call:
lm(formula = dyn(Z2 ~ Z1))

Coefficients:
(Intercept)           Z1
  -2120.912        1.843



On Mon, Jun 22, 2009 at 8:23 AM, anass<anass.mouhsine at gmail.com> wrote:
> Hi again,
>
> Suppose the problem evolves a little.
> I would like to know how to perform a regression on two xts objects with
> different lengths.
> Let's take the same example:
> series1
>
> 2008-06-02 09:00:00 5007.0
> 2008-06-02 09:01:00 5010.0
> 2008-06-02 09:02:00 5014.0
> 2008-06-02 09:03:00 5012.5
> 2008-06-02 09:04:00 5013.5
> 2008-06-02 09:05:00 5009.5
> 2008-06-02 09:06:00 5007.0
> 2008-06-02 09:07:00 5006.5
> 2008-06-02 09:08:00 5008.5
> 2008-06-02 09:09:00 5004.5
>
> Series2
>
> 2008-06-02 09:01:00 7115.0
> 2008-06-02 09:03:00 7117.0
> 2008-06-02 09:05:00 7111.0
> 2008-06-02 09:07:00 7107.0
> 2008-06-02 09:09:00 7102.5
>
>
>>lm(Series2~Series1)
> Error in model.frame.default(formula = Series2 ~ Series1, drop.unused.levels
> = TRUE) :
>  variable lengths differ (found for 'Series1')
>
> While performing the regression the usual way, I've got the error described
> before.
>
> Any thoughts?
>
> Anass
>
>
>
> On Thu, Jun 11, 2009 at 5:57 PM, spencerg <spencer.graves at prodsyse.com>wrote:
>
>>     One could also smooth the two series first, then compare the smooths.
>>  This could be used to interpolate missing values to provide a larger sample
>> size for tradition tools.
>>
>>     The "fda" package has a number of tools for doing this kind of thing.
>>  Learning about this package will soon get easier with the scheduled
>> appearance in July of Ramsay, Hooker and Graves (2009) Functional Data
>> Analysis with R and Matlab (Springer).  The "fda" package contains script
>> files to reproduce all but one of the 78 figures in the book.  The script
>> files are available now, and can be found as follows:
>>
>> > system.file('scripts', package='fda')
>> [1] "C:/Users/sgraves/R/R-2.9.0/library/fda/scripts"
>> > dir(system.file('scripts', package='fda'))
>> <snip> [17] "fdarm-ch01.R"            "fdarm-ch02.R"          [19]
>> "fdarm-ch03.R"            "fdarm-ch04.R"          [21] "fdarm-ch05.R"
>>      "fdarm-ch06.R"          [23] "fdarm-ch07.R"            "fdarm-ch08.R"
>>        [25] "fdarm-ch09.R"            "fdarm-ch10.R"          [27]
>> "fdarm-ch11.R"            "pda.fd.test.R"
>>
>>     Hope this helps.     Spencer
>>
>>
>> Anass Mouhsine wrote:
>>
>>> Thanks Joshua
>>>
>>> A
>>>
>>> Joshua Ulrich wrote:
>>>
>>>> On Thu, Jun 11, 2009 at 9:16 AM, anass<anass.mouhsine at gmail.com> wrote:
>>>>
>>>>
>>>>> Thx guys,
>>>>>
>>>>> well the error I got is elswhere and I thought it was due to the
>>>>> asynchrone
>>>>> timeseries.
>>>>>
>>>>> let's assume that I got the intraday ratio, what I do is the following
>>>>>
>>>>> d<-index(to.daily(ratio))
>>>>> for (i in 1:length(d)){
>>>>>  tstart<-paste(d[i], "09:00:00")
>>>>>  tend<-paste(d[i],"17:00:00")
>>>>> ts_ratio<-ratio[tstart:tend]
>>>>>
>>>>>
>>>>
>>>> This line is incorrect.  I don't believe the ":" sequence operator in
>>>> package:base is defined for character strings (see ?":").  This is
>>>> what is causing an error.  What you probably intended is this:
>>>> ts_ratio <- ratio[paste(tstart,tend,sep="::")]
>>>>
>>>>
>>>>
>>>>> #
>>>>> # other operations
>>>>> #
>>>>> }
>>>>>
>>>>> and I have this error
>>>>> Error in tstart:tend : argument NA / NaN
>>>>>
>>>>> So I assume if it is not due to asynchrone series, it is due to the
>>>>> subsetting.
>>>>> Does xts objects accept this kind of subset?
>>>>>
>>>>>
>>>>> On Thu, Jun 11, 2009 at 4:05 PM, Jeff Ryan <jeff.a.ryan at gmail.com>
>>>>> wrote:
>>>>>
>>>>>
>>>>>> Anass,
>>>>>>
>>>>>> xts and zoo automatically align series via "merge" when performing Ops
>>>>>> methods (+/-*...etc)
>>>>>>
>>>>>> This is what you want in most cases.
>>>>>>
>>>>>> See ?merge.xts, ?xts, ?merge.zoo and ?Ops.zoo
>>>>>>
>>>>>> HTH
>>>>>> Jeff
>>>>>>
>>>>>> On Thu, Jun 11, 2009 at 9:00 AM, Joshua Ulrich<josh.m.ulrich at gmail.com
>>>>>> >
>>>>>> wrote:
>>>>>>
>>>>>>
>>>>>>> Hi Anass,
>>>>>>>
>>>>>>> Can you provide an example of what you're trying to do and what error
>>>>>>> you are receiving?  I'm able to run the code below without error.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> require(xts)
>>>>>>>>
>>>>>>>> Lines <-
>>>>>>>>
>>>>>>>>
>>>>>>> + "2008-06-02 09:00:00,5007.0
>>>>>>> + 2008-06-02 09:01:00,5010.0
>>>>>>> + 2008-06-02 09:02:00,5014.0
>>>>>>> + 2008-06-02 09:03:00,5012.5
>>>>>>> + 2008-06-02 09:04:00,5013.5
>>>>>>> + 2008-06-02 09:05:00,5009.5
>>>>>>> + 2008-06-02 09:06:00,5007.0
>>>>>>> + 2008-06-02 09:07:00,5006.5
>>>>>>> + 2008-06-02 09:08:00,5008.5
>>>>>>> + 2008-06-02 09:09:00,5004.5"
>>>>>>>
>>>>>>>
>>>>>>>> one <- read.zoo(textConnection(Lines),sep=',',FUN=as.POSIXct)
>>>>>>>> one <- as.xts(one)
>>>>>>>>
>>>>>>>> Lines <-
>>>>>>>>
>>>>>>>>
>>>>>>> + "2008-06-02 09:01:00,7115.0
>>>>>>> + 2008-06-02 09:03:00,7117.0
>>>>>>> + 2008-06-02 09:05:00,7111.0
>>>>>>> + 2008-06-02 09:07:00,7107.0
>>>>>>> + 2008-06-02 09:09:00,7102.5"
>>>>>>>
>>>>>>>
>>>>>>>> two <- read.zoo(textConnection(Lines),sep=',',FUN=as.POSIXct)
>>>>>>>> two <- as.xts(two)
>>>>>>>>
>>>>>>>> one/two
>>>>>>>>
>>>>>>>>
>>>>>>>                          e1
>>>>>>> 2008-06-02 09:01:00 0.7041462
>>>>>>> 2008-06-02 09:03:00 0.7042996
>>>>>>> 2008-06-02 09:05:00 0.7044719
>>>>>>> 2008-06-02 09:07:00 0.7044463
>>>>>>> 2008-06-02 09:09:00 0.7046111
>>>>>>>
>>>>>>>
>>>>>>>> two/one
>>>>>>>>
>>>>>>>>
>>>>>>>                         e1
>>>>>>> 2008-06-02 09:01:00 1.420160
>>>>>>> 2008-06-02 09:03:00 1.419850
>>>>>>> 2008-06-02 09:05:00 1.419503
>>>>>>> 2008-06-02 09:07:00 1.419555
>>>>>>> 2008-06-02 09:09:00 1.419223
>>>>>>>        Best,
>>>>>>> Joshua
>>>>>>> --
>>>>>>> http://www.fosstrading.com
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Thu, Jun 11, 2009 at 6:11 AM, Anass
>>>>>>> Mouhsine<anass.mouhsine at gmail.com> wrote:
>>>>>>>
>>>>>>>
>>>>>>>> Hi all,
>>>>>>>>
>>>>>>>> Suppose I have two xts time series with asynchroneous time index.
>>>>>>>> I would like for example to calculate a ratio of those two series,
>>>>>>>> but
>>>>>>>> I
>>>>>>>> don't know how to get an intersection of the two indices in order to
>>>>>>>> avoid
>>>>>>>> errors.
>>>>>>>>
>>>>>>>> an example of the data is the following
>>>>>>>>
>>>>>>>> series1
>>>>>>>>
>>>>>>>> 2008-06-02 09:00:00 5007.0
>>>>>>>> 2008-06-02 09:01:00 5010.0
>>>>>>>> 2008-06-02 09:02:00 5014.0
>>>>>>>> 2008-06-02 09:03:00 5012.5
>>>>>>>> 2008-06-02 09:04:00 5013.5
>>>>>>>> 2008-06-02 09:05:00 5009.5
>>>>>>>> 2008-06-02 09:06:00 5007.0
>>>>>>>> 2008-06-02 09:07:00 5006.5
>>>>>>>> 2008-06-02 09:08:00 5008.5
>>>>>>>> 2008-06-02 09:09:00 5004.5
>>>>>>>>
>>>>>>>> Series2
>>>>>>>>
>>>>>>>> 2008-06-02 09:01:00 7115.0
>>>>>>>> 2008-06-02 09:03:00 7117.0
>>>>>>>> 2008-06-02 09:05:00 7111.0
>>>>>>>> 2008-06-02 09:07:00 7107.0
>>>>>>>> 2008-06-02 09:09:00 7102.5
>>>>>>>>
>>>>>>>> Any idea?
>>>>>>>>
>>>>>>>> Thanks in advance
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> R-SIG-Finance at stat.math.ethz.ch mailing list
>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>>>>> -- Subscriber-posting only.
>>>>>>>> -- If you want to post, subscribe first.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> R-SIG-Finance at stat.math.ethz.ch mailing list
>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>>>> -- Subscriber-posting only.
>>>>>>> -- If you want to post, subscribe first.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> --
>>>>>> Jeffrey Ryan
>>>>>> jeffrey.ryan at insightalgo.com
>>>>>>
>>>>>> ia: insight algorithmics
>>>>>> www.insightalgo.com
>>>>>>
>>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> En toda ocasion, disfruta de la vida
>>>>>
>>>>>
>>>>>
>>>>
>>>> Best,
>>>> Joshua
>>>> --
>>>> http://www.fosstrading.com
>>>>
>>>>
>>>>
>>> _______________________________________________
>>> R-SIG-Finance at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>> -- Subscriber-posting only.
>>> -- If you want to post, subscribe first.
>>>
>>>
>>
>
>
> --
>
> Every man has his own destiny: the only imperative is to follow it, to
> accept it, no matter where it leads him.(H.Miller)
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list