On Mon, May 14, 2012 at 5:09 AM, Matthew Johnson <mcoog...@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 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.