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.
Details
This version of the cross correlation function is different from the stats
implementation of ccf
in two ways:
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).
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.71278879 0.73747884 -0.81553757 -0.40332017 0.40602963 0.01703219
#> [13] 0.37985375 -0.41284958 -0.32528179 -0.55644623 0.15730063 0.18121184
#> [19] 0.30569062 -0.22447315 -0.58790382 -0.15320563 0.08106094 1.00000000
#> [25] 0.10985504 -0.04989051 -0.47084961 -0.03283750 0.62469144 0.27733019
#> [31] 0.17847443 -0.60628277 -0.07197082 0.08886414 0.50477083 0.17928673
#> [37] -1.00000000
#>
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.14545732 0.70441246 -0.52446047 -0.40087762 -0.41598936 0.12376067
#> [7] 0.41565620 0.17353735 -0.18985838 -0.69158655 -0.02530303 0.10073960
#> [13] 1.00000000 0.20480769 -0.13516059 -0.50532498 -0.36700355 0.30569062
#> [19] 0.18121184 0.15730063 -0.55644623 -0.32528179 -0.41284958 0.37985375
#> [25] 0.01703219 0.40602963 -0.40332017 -0.81553757 0.73747884 -0.71278879
#> [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.748550154 -0.047674378 -0.460038519 -0.185925184 0.754964035
#> [6] 0.011531413 -0.635501372 0.056527757 0.561974218 -0.743626060
#> [11] 0.001720930 0.190317998 0.661270855 -0.674425046 -0.196502859
#> [16] 0.568160334 0.013621767 -0.487902334 0.492458264 -0.380229213
#> [21] 0.347067625 0.127015886 -0.389416721 -0.206698028 0.423753339
#> [26] -0.051284106 -0.682006660 0.333530816 0.263136113 -0.559043800
#> [31] 0.362198540 0.645897368 -0.118024020 -0.794772736 0.762121993
#> [36] 0.282202458 -0.604902891 0.276824457 0.475978060 -0.896691534
#> [41] 0.055865717 0.622206059 -0.235890127 -0.166695421 -0.408108917
#> [46] 0.426900838 0.411359843 -0.122889597 -0.538335140 0.100507223
#> [51] 1.000000000 -0.244365027 -0.314534171 0.069220830 0.447154686
#> [56] -0.063037672 0.130674716 -0.792678080 0.284160279 0.228151131
#> [61] 0.005378847 -0.925613719 0.823572455 -0.097171428 -0.337984181
#> [66] 0.024846466 0.028884465 0.177797394 -0.555305064 0.553760641
#> [71] -0.330372674 0.177578362 0.203187637 -0.267371096 -0.417195876
#> [76] 0.709702298 -0.527422343 -0.421044196 0.083233734 0.811268195
#> [81] -0.282221953 -0.047584501 0.266370006 0.504874921 -0.602352088
#> [86] -0.202356472 0.479220970 -0.693687782 0.100292971 -0.149798452
#> [91] -0.069943172 0.332179268 -0.215646113 0.121370279 -0.096661556
#>