Fernando
Fernando

Reputation: 7905

Shift time series

I have 2 weekly time-series, which show a small correlation (~0.33). How can i 'shift in time' one of these series, so that i can check if there's a greater correlation in the data?

Example data:

x = textConnection('1530.2 1980.9 1811 1617 1585.4 1951.8 2146.6 1605 1395.2 1742.6 2206.5 1839.4 1699.1 1665.9 2144.7 2189.1 1718.4 1615.5 2003.3 2267.6 1772.1 1635.2 1836 2261.8 1799.1 1634.9 1638.6 2056.5 2201.4 1726.8 1586.4 1747.9 1982 1695.2 1624.9 1652.4 2011.9 1788.8 1568.4 1540.7 1866.1 2097.3 1601.3 1458.6 1424.4 1786.9 1628.4 1467.4 1476.2 1823 1736.7 1482.7 1334.2 1871.9 1752.9 1471.6 1583.2 1601.4 1987.7 1649.6 1530.9 1547.1 2165.2 1852 1656.9 1605.2 2184.6 1972 1617.6 1491.1 1709.5 2042.2 1667.1 1542.6 1497.6 2090.5 1816.8 1487.5 1468.2 2228.5 1889.9 1690.8 1395.7 1532.8 1934.4 1557.1 1570.6 1453.2 1669.6 1782 1526.1 1411 1608.1 1740.5 1492.3 1477.8 1102.6 1366.1 1701.1 1500.6 1403.2 1787.2 1776.6 1465.3 1429.5')
x = scan(x)

y = textConnection('29.8 22.6 26 24.8 28.9 27.3 26 29.2 28.2 23.9 24.5 23.6 21.1 22 20.7 19.9 22.8 25 21.6 19.1 27.2 23.7 24.2 22.4 25.5 25.4 23.4 24.7 27.4 23.4 25.8 28.8 27.7 23.7 22.9 29.4 22.6 28.6 22.2 27.6 26.2 26.2 29.8 31.5 24.5 28.7 25.9 26.9 25.9 30.5 30.5 29.4 29.3 31.4 30 27.9 28.5 26.4 29.5 28.4 25.1 24.6 21.1 23.6 20.5 23.7 25.3 20.2 23.4 21.1 23.1 24.6 20.7 20.7 26.9 24.1 24.7 25.8 26.7 26 28.9 29.5 27.4 22.1 31.6 25 27.4 30.4 28.9 27.4 22.5 28.4 28.7 31.1 29.3 28.3 30.6 28.6 26 26.2 26.2 26.7 25.6 31.5 30.9')
y = scan(y)

I'm using R with dtw package, but i'm not familiar with these kind of algorithms. Thanks for any help!

Upvotes: 0

Views: 795

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174853

You could try the ccf() function in base R. This estimates the cross-correlation function of the two time series.

For example, using your data (see below if interested in how I got the data you pasted into your Question into R objects x and y)

xyccf <- ccf(x, y)

yielding

> xyccf

Autocorrelations of series ‘X’, by lag

   -17    -16    -15    -14    -13    -12    -11    -10     -9     -8     -7 
 0.106  0.092  0.014  0.018  0.011  0.029 -0.141 -0.153 -0.107 -0.141 -0.221 
    -6     -5     -4     -3     -2     -1      0      1      2      3      4 
-0.274 -0.175 -0.277 -0.176 -0.217 -0.253 -0.339 -0.274 -0.267 -0.330 -0.278 
     5      6      7      8      9     10     11     12     13     14     15 
-0.184 -0.120 -0.200 -0.156 -0.184 -0.062 -0.076 -0.117 -0.048  0.015 -0.016 
    16     17 
-0.038 -0.029

and this plot

enter image description here

To interpret this, when the lag is positive, y is leading x whereas when the lag is negative x is leading y.

Reading your data into R...

x <- scan(text = "1530.2 1980.9 1811 1617 1585.4 1951.8 2146.6 1605 1395.2 1742.6
                  2206.5 1839.4 1699.1 1665.9 2144.7 2189.1 1718.4 1615.5 2003.3
                  2267.6 1772.1 1635.2 1836 2261.8 1799.1 1634.9 1638.6 2056.5
                  2201.4 1726.8 1586.4 1747.9 1982 1695.2 1624.9 1652.4 2011.9
                  1788.8 1568.4 1540.7 1866.1 2097.3 1601.3 1458.6 1424.4 1786.9
                  1628.4 1467.4 1476.2 1823 1736.7 1482.7 1334.2 1871.9 1752.9
                  1471.6 1583.2 1601.4 1987.7 1649.6 1530.9 1547.1 2165.2 1852
                  1656.9 1605.2 2184.6 1972 1617.6 1491.1 1709.5 2042.2 1667.1
                  1542.6 1497.6 2090.5 1816.8 1487.5 1468.2 2228.5 1889.9 1690.8
                  1395.7 1532.8 1934.4 1557.1 1570.6 1453.2 1669.6 1782 1526.1
                  1411 1608.1 1740.5 1492.3 1477.8 1102.6 1366.1 1701.1 1500.6
                   1403.2 1787.2 1776.6 1465.3 1429.5")

y <- scan(text = "29.8 22.6 26 24.8 28.9 27.3 26 29.2 28.2 23.9 24.5 23.6 21.1 22
                  20.7 19.9 22.8 25 21.6 19.1 27.2 23.7 24.2 22.4 25.5 25.4 23.4
                  24.7 27.4 23.4 25.8 28.8 27.7 23.7 22.9 29.4 22.6 28.6 22.2 27.6
                  26.2 26.2 29.8 31.5 24.5 28.7 25.9 26.9 25.9 30.5 30.5 29.4 29.3
                  31.4 30 27.9 28.5 26.4 29.5 28.4 25.1 24.6 21.1 23.6 20.5 23.7
                  25.3 20.2 23.4 21.1 23.1 24.6 20.7 20.7 26.9 24.1 24.7 25.8 26.7
                  26 28.9 29.5 27.4 22.1 31.6 25 27.4 30.4 28.9 27.4 22.5 28.4 28.7
                  31.1 29.3 28.3 30.6 28.6 26 26.2 26.2 26.7 25.6 31.5 30.9")

Upvotes: 2

Related Questions