Skip to content

How to compare two asynchroneous xts time series?

10 messages · Anass Mouhsine, Jeff Ryan, anass +3 more

#
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
#
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.
+ "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"
+ "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"
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
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:
#
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:

  
    
#
On Thu, Jun 11, 2009 at 9:16 AM, anass<anass.mouhsine at gmail.com> wrote:
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="::")]
Best,
Joshua
--
http://www.fosstrading.com
#
Thanks Joshua

A
Joshua Ulrich 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:
10 days later
#
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.
+ 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"
+ 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"
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:
#
Another way is to explicitly do the merge yourself
in which case you can do it all in xts:

lm(x2 ~ x1, merge(x1, x2))

On Mon, Jun 22, 2009 at 8:44 AM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote: