Thursday, January 31, 2013

Stretching time series with R

Say you have three time series of different lengths:

x1 - 104
x2 - 67
x3 - 47

You would like to calculate the mean of them at the same moments, which is difficult for their different sizes.

What you can do is to stretch the shorter time series vectors, so that their lengths are equal to the longest of the vectors - here 104.

For that you need to multiply selected items from shorter vectors. You start with the average difference in length of a two vectors - the longer and shorter.

However, the average may be a rational number, such as 0.333 or 2.718. In such a case, the number of repetitions of particular items will differ. Hence, to retain the characteristic of the time series being stretched, you may randomize the length of repetitions.

As a result you get three new time series vectors (one unchanged and two stretched) with equal lengths.

Now you can easily compute their average at each point in time:

[ R code ]

No comments: