mql4beginner
mql4beginner

Reputation: 2233

How to get correlations between two variables with lags

I would like to check if there is a correlation between "birds" & "wolfs" in different lags.Getting the correlation value is easy but how can I address the lag issue ( I need to check the correleation value for 1:4 lags )? The output that I look for is a data table that contains the lag value and the related correlation value.

df <- read.table(text = " day birds    wolfs     
                           0   2       21           
                           1   8         4  
                           2   2         5 
                           3   2         4 
                           4   3         6
                           5   1         12  
                           6   7         10 
                           7   1         9
                           8   2         12 header = TRUE)

Output(not real results): Lag CorValue

0     0.9
1     0.8
2     0.7
3     0.9

Upvotes: 2

Views: 4246

Answers (1)

etienne
etienne

Reputation: 3678

If you do this :

corLag<-ccf(df$birds,df$wolfs,lag.max=max(df$day))

it will return this :

Autocorrelations of series ‘X’, by lag

-8     -7     -6     -5     -4     -3     -2     -1      0      1      2      3      4      5      6     7      8  
-0.028  0.123 -0.045 -0.019  0.145 -0.176 -0.082 -0.126 -0.296  0.757 -0.134 -0.180  0.070 -0.272  0.549 -0.170 -0.117 

the first row is the lag, the second is the correlation value. you can check that cor(df$birds,df$wolfs)is indeed equal to -0.296

Upvotes: 4

Related Questions