[R] Aligning time series
Matthew Johnson
mcooganj at gmail.com
Mon May 14 22:33:23 CEST 2012
Very useful, however as i will be aligning large datasets, is it
possible to query a variable for the lead / lag for each series? So i
can more 'automatically' align?
ideally the lag is stored in d$align or something
Best
mj
Sent from my iPad
On 14/05/2012, at 9:36 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> 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