Skip to contents

Calculate the lagged correlation between numeric vectors x and y. Vectors x and y are assumed to be captured at the same resolution and, similarly, successive values in x and y are assumed to be equi-distant. Missing values are allowed in each vector, correlations are calculated based on the complete cases.

Usage

get_ccf(x, y, min.overlap = round(0.1 * max(length(x), length(y))))

Arguments

x

vector, assumption is that x is longer than y

y

vector

min.overlap

integer value: what is the minimal number of values between x and y that should be considered? Caution: the default minimal overlap assumes that both sequences have approximately the same length.

Value

list with ccf values and lags and the number of observations the ccf is based on

Details

This version of the cross correlation function is different from the stats implementation of ccf in two ways:

  1. We consider the full region of correlations between vectors x and y as specified by min.overlap rather than just the overlap. The two vectors can be of very different length (e.g. when y is just a snippet recovered from a crime scene and x is from the full length object in the lab).

  2. We do not use a Fourier transformation to calculate cross-correlation. This makes the evaluation slower, but prevents any edge effects.

Examples

library(dplyr)
x <- runif(20)
get_ccf(x, lead(x, 5))
#> $lag
#>  [1] -18 -17 -16 -15 -14 -13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0
#> [20]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
#> 
#> $ccf
#>  [1]          NA          NA          NA          NA          NA  1.00000000
#>  [7] -0.57895159 -0.82605771 -0.43674045 -0.23688851  0.67046440 -0.08064013
#> [13] -0.63361874 -0.01504534  0.29846545  0.02015801 -0.36945589 -0.27375509
#> [19]  0.45588836  0.19685956 -0.06203548 -0.18421718  0.09024513  1.00000000
#> [25]  0.08361471 -0.22756578 -0.15054923  0.22611468  0.63877310 -0.27843807
#> [31]  0.02336284 -0.09750601  0.37518206  0.43861768 -0.86599543 -0.79572257
#> [37] -1.00000000
#> 
#> $ns
#>  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 15 15 15 15 15 14 13 12 11 10  9
#> [26]  8  7  6  5  4  3  2
#> 
get_ccf(x, lag(x, 5), min.overlap = 3)
#> $lag
#>  [1] -17 -16 -15 -14 -13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1
#> [20]   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
#> 
#> $ccf
#>  [1] -0.55827587 -0.80956775 -0.46332938  0.09649268  0.04148700 -0.71828574
#>  [7] -0.45633029  0.21734211  0.22003254 -0.10663862 -0.30447758  0.18882317
#> [13]  1.00000000 -0.02333867 -0.30269409 -0.03722500  0.37035464  0.45588836
#> [19] -0.27375509 -0.36945589  0.02015801  0.29846545 -0.01504534 -0.63361874
#> [25] -0.08064013  0.67046440 -0.23688851 -0.43674045 -0.82605771 -0.57895159
#> [31]          NA          NA          NA          NA          NA
#> 
#> $ns
#>  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 15 15 15 15 15 14 13 12 11 10  9  8
#> [26]  7  6  5  4  3
#> 
x <- runif(100)
get_ccf(x[45:50], x, min.overlap = 6)
#> $lag
#>  [1] -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76
#> [20] -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57
#> [39] -56 -55 -54 -53 -52 -51 -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38
#> [58] -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19
#> [77] -18 -17 -16 -15 -14 -13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0
#> 
#> $ccf
#>  [1]  0.4085956268 -0.5116815677 -0.0111446825 -0.3918138796 -0.4270710572
#>  [6]  0.4303473588  0.1222390123  0.0677575677 -0.0552173583  0.5788843824
#> [11] -0.4233869071 -0.3562032675  0.3736698836  0.5680125274  0.1533341652
#> [16]  0.4395298466 -0.2286684624 -0.9122152297 -0.3840816615  0.8543319144
#> [21]  0.4916933974 -0.6714719796 -0.2938584374 -0.5626190069  0.3316861594
#> [26]  0.7203805631  0.0337253178 -0.8249679894  0.1903672534  0.1896229554
#> [31] -0.5040911869  0.2626342229  0.2227702790  0.1457268733  0.0551651219
#> [36]  0.6231005110 -0.2166187225 -0.8841530188 -0.3536732128  0.6991547532
#> [41]  0.4035944560  0.3308723257 -0.1485991886 -0.8581906259  0.0226078240
#> [46]  0.1945619971  0.1947488938 -0.3174923148 -0.3145916586  0.1107536706
#> [51]  1.0000000000  0.2149876585 -0.8040139520 -0.5286928687  0.7743110245
#> [56]  0.3404370976 -0.6782379651 -0.4160623575  0.3741716620  0.8310446852
#> [61] -0.2549369260 -0.5786877932  0.0472662155 -0.2638764012 -0.4127305801
#> [66]  0.6804550118  0.5181535294  0.0236315016 -0.8770551860  0.2130314850
#> [71]  0.5933438463 -0.1159211450  0.0824253057  0.1706721959 -0.4652050136
#> [76] -0.0004700292 -0.5392268127 -0.0264124508  0.7661357209  0.5596174556
#> [81] -0.3765467356 -0.7198102852 -0.1136001693 -0.3207293063  0.0569257730
#> [86]  0.9006575087  0.1044893444 -0.6497565766 -0.3234263853  0.7421817222
#> [91]  0.0146901834 -0.1190534288  0.3879125911 -0.3356829767 -0.5245482684
#> 
#> $ns
#>  [1] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
#> [39] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
#> [77] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
#>