Cross correlation function between two vectors

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?

## Value

list with ccf values and lags

## Examples

library(dplyr)
x <- runif(20)
#> $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
#>  [6] -1.000000000  0.876681200 -0.756982306  0.156337831 -0.370634803
#> [11]  0.687490532 -0.441984745  0.735260367 -0.268909840  0.397837628
#> [16] -0.158654091 -0.406766036  0.071683057  0.006662668  0.081704788
#> [21] -0.431397440  0.040457336 -0.192325164  1.000000000 -0.193504985
#> [26]  0.021491247 -0.466294049  0.020352711  0.257541654 -0.045155379
#> [31] -0.395752552 -0.419741251  0.307386826 -0.159961486  0.911271068
#> [36] -0.964393695  1.000000000
#>
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.298830421  0.587809357 -0.647632346  0.486289789  0.282601039
#>  [6] -0.155118331  0.019782148 -0.092231897 -0.321562915 -0.142989815
#> [11]  0.161658920 -0.322460274  1.000000000 -0.302965872  0.168070998
#> [16] -0.294148285 -0.016224507  0.006662668  0.071683057 -0.406766036
#> [21] -0.158654091  0.397837628 -0.268909840  0.735260367 -0.441984745
#> [26]  0.687490532 -0.370634803  0.156337831 -0.756982306  0.876681200
#> [31]           NA           NA           NA           NA           NA
#>
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.149280195 -0.672295015  0.545969733 -0.869896656  0.574551503
#>  [6] -0.224694840 -0.176806094  0.651413041 -0.700884772  0.726604474
#> [11] -0.525552440  0.666203665 -0.665393791  0.569461821 -0.503678388
#> [16]  0.570943095 -0.265383203  0.340456572 -0.532748020  0.131911807
#> [21]  0.478111814 -0.700151231  0.787728704 -0.792085095  0.915761107
#> [26] -0.878830811  0.519191377 -0.226834075  0.340178888 -0.435759252
#> [31] -0.089830134 -0.046496891  0.316197172 -0.136019910 -0.090849729
#> [36] -0.060690740 -0.422386250  0.378291317  0.132623624  0.478632376
#> [41] -0.068191043  0.001001766  0.084475282  0.094413025 -0.543837146
#> [46]  0.070984348 -0.299058940 -0.156665624  0.385073577 -0.665512666
#> [51]  1.000000000 -0.849967087  0.801679961 -0.308004931  0.545435280
#> [56] -0.162432778 -0.375554618 -0.091490933 -0.449990912 -0.140675538
#> [61]  0.197977079 -0.319573815  0.486927282  0.278368152 -0.122501687
#> [66]  0.613905008 -0.314858042  0.524915998 -0.714414027  0.172255723
#> [71] -0.636844637  0.717948167 -0.091960225  0.642781364 -0.274501806
#> [76]  0.233004707 -0.329975005  0.267257265  0.232944621 -0.086856595
#> [81]  0.003676208 -0.670343098  0.425783331 -0.770510517  0.216180636
#> [86] -0.375854053  0.304289840  0.458502250 -0.232325473  0.640175505
#> [91] -0.320000304  0.330838750 -0.604871575  0.042853228 -0.051028510
#>