You are on page 1of 11

Cross-correlation

Leading indicator TS

Cross-correlation

How can we study the relationship between 2 or more


time series?

In the relationship between two time series (yt and xt),


the series yt may be related to past lags of the x-series

The cross correlation function (CCF) is helpful for identifying lags


of the x-variable that might be useful predictors of yt

Cross-correlation

The CCF is defined as the set of correlations between xt+h


and yt for h = 0, 1, 2, p

A negative value for h is a correlation between the x-variable at


a time before t and the y-variable at time t

For instance, consider h = 2


The CCF value would give the correlation between xt-2 and yt

When one or more xt+h are predictors of yt , and h (the


sig. lag) is negative, is sometimes said that x leads y
When one or more xt+h are predictors yt, and h is positive,
it is sometimes said that x lags y

Cross-correlation

In some problems, the goal may be to identify which


variable is leading and which is lagging

In some problems, well assume the x-variable is a leading


variable of the y-variable

We will want to use values of the x-variable to predict future


values of y

CCF in R

The CCF command is:

> ccf(x-variable name, y-variable name)

If you wish to specify how many lags to show, add that


number as an argument of the command

> ccf(x,y, 50)


gives the CCF for values of h = 0, 1, , 50

Example in R

Monthly river flow at 2 points along the Snake River in ID

> hydro<-read.csv("F:/R/river.csv", h=T, sep=",")


> hydro1<-ts(hydro$BRUNEUNAT, start=c(1917), end=c(1994), freq=1)

> hydro2<-ts(hydro$RIRIENAT, start=c(1917), end=c(1994), freq=1)


> plot(hydro1, ylim=c(0,800), ylab="Annual River Flow")
> lines(hydro2, lty=2, col="red")

Example in R

Do flow rates at one point peak before the other? How


far in advance?

Example in R
> ccf(hydro1, hydro2)

Example in R
> ccfvalues<-ccf(hydro1, hydro2)
> ccfvalues

Autocorrelations of series X, by lag


-15
-0.231
-6
0.113
3
0.182
12
0.005

-14
-13
-12
-11
-10
-9
-8
-7
-0.008 0.089 0.177 0.037 0.226 -0.022 0.094 -0.069
-5
-4
-3
-2
-1
0
1
2
-0.106 -0.016 0.069 0.071 0.271 0.341 0.770 0.297
4
5
6
7
8
9
10
11
-0.038 0.046 -0.136 -0.025 -0.058 -0.043 -0.106 0.044
13
14
15
-0.067 0.014 -0.044

When one or more xt+h (hydro1) are predictors yt (hydro2) and h is positive x lags y

Whats next?

If we know 1 variable leads another at a certain lag, can try to predict x using the lagged
observation of y using regression

= +1

2 = 1+1

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
10.90883
11.95623
0.912
0.364
hydroreg$hydro1 0.44684
0.04193 10.657
<2e-16 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 41.04 on 75 degrees of freedom
Multiple R-squared: 0.6023,
Adjusted R-squared: 0.597
F-statistic: 113.6 on 1 and 75 DF, p-value: < 2.2e-16

Just a check
> acf(hreg$resid)

You might also like