[R] Aligning time series

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 14 13:36:39 CEST 2012


On Mon, May 14, 2012 at 5:09 AM, Matthew Johnson <mcooganj at gmail.com> wrote:
> Sir,
>
> I have large data sets of economic indicators and would like to align
> them to a reference series - say the unemployment rate or industrial
> production.
>
> Is there a canned routine that returns the optimal lead / lag
> according to some (or a variety) of algorithims? not all series will
> be of the same length, however i would like to constrain the matching
> such that each element in the reference series matches to only one
> element in the comparison series.
>
> I do not mind if some data extend forward (in the case of series that
> lead the reference series), or back (in the case of laggers or series
> that are longer than the reference series).
>
> As a toy example, say my data set is constructed as follows:
>
> Index <- seq(0, 2*pi, length=100)
>
> With the reference series:
>
> S1 <- sin(index)
>
> And the series that i want to align to S1 are:
>
> S2 <- 2(cos(index) + runif(10)/100)
> S3 <- 0.5(sin(index +pi/4) + runif(10)/100)
> S4 <- sin(index + pi/3) + runif(10)/100
> S4[1:25] <- NA
>
> In this case, I am looking for a function that tells me by how many
> periods i ought to advance the three series (S2, S3 and S4) to
> maximise their relationship with S1 - though it is not always the case
> that the reference series will lead.

Check out the dtw in the dtw package:

library(dtw)
d <- dtw(S1, S2)
d$index1
d$index2

?dtw


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list