This tutorial will help you to calculate the lagged correlation between two time series. It will use calculate_lagged_correlation function of lagci package.
6.1 Prepare your data
You can get started with the example data in lagci package. And the format of time column is POSIXct
Warning in fun(libname, pkgname): mzR has been built against a different Rcpp version (1.0.13)
than is installed on your system (1.1.0). This might lead to errors
when loading mzR. If you encounter such issues, please send a report,
including the output of sessionInfo() to the Bioc support forum at
https://support.bioconductor.org/. For details see also
https://github.com/sneumann/mzR/wiki/mzR-Rcpp-compiler-linker-issue.
Calculate with calculate_lagged_correlation function of lagci package.
6.2.1 x, y
Type: Numeric vectors
Purpose: The two time-series to be compared.
Details: Each vector represents the observed values of one variable. Internally, both vectors are standardized (centered and scaled to unit variance) to make the correlation unbiased by different magnitudes or units.
6.2.2 time1, time2
Type: POSIXct vectors (or convertible to POSIXct)
Purpose: Timestamps corresponding to x and y.
Role: Used to align the two time-series onto a shared, regularly spaced timeline before computing correlations.
6.2.3 time_tol
Type: Numeric (hours)
Default: 1
Meaning: The maximum time lag to evaluate in both positive and negative directions.
Unit: Hours. For example:
time_tol = 1 → ±1 hour
time_tol = 0.5 → ±30 minutes
time_tol = 1/60 → ±1 minute
6.2.4 step
Type: Numeric (hours)
Default: 1/60 (i.e., 1 minute)
Meaning: The resolution of the regular time grid used for alignment.
Unit: Hours. For example:
step = 1 → 1-hour sampling
step = 1/60 → 1-minute sampling
step = 1/3600 → 1-second sampling
6.2.5 min_matched_sample
Type: Integer
Default: 10
Purpose: Minimum number of overlapping (non-missing) samples required after alignment. If fewer matched samples remain, the function returns NULL to avoid unstable correlations.
6.2.6 progressbar
Type: Logical
Default: TRUE
Purpose: Whether to display progress information. Particularly useful when applying the function to many pairs of series in batch analyses.
6.2.7 align_method
Type: Character string
Options: “linear” or “constant”
Default: “linear”
Purpose: Method used for interpolation when mapping the irregular time-series onto the regular grid.
“linear”: Interpolates linearly between neighboring observations.
“constant”: Uses the nearest neighbor (step-wise constant).
6.2.8 cor_method
Type: Character string
Options: “spearman” or “pearson”
Default: “spearman”
Purpose: The statistical correlation measure.
“pearson”: Measures linear correlation between the raw values.
“spearman”: Measures rank correlation, robust to monotonic but non-linear relationships and less sensitive to outliers.
6.2.9 Calculation
result_1<-lagci::calculate_lagged_correlation( x =heart_data$heart, y =step_data$step, time1 =heart_data$time, time2 =step_data$time, time_tol =0.5, step =1/60, align_method ="linear", cor_method ="spearman")print(result_1)
--------------------
Brief information
Shift time is x - y
If shift time > 0:
means that x is changing after y
If shift time < 0:
means that x is changing before y
--------------------
length of x: 91948
length of y: 12960
length of correlations: 0
length of correlations: 0
Max correlation index: 32
Global correlation index: 30
--------------------
Parameters
--------------------
pacakge_name: lagci
function_name: calculate_lagged_correlation
time: 2025-09-22 00:16:24.710992
parameters:
time_tol : 0.5
step : 0.0166666666666667
min_matched_sample : 10
progressbar : TRUE
threads : 10
cor_method : spearman
result_2<-lagci::calculate_lagged_correlation( y =heart_data$heart, x =step_data$step, time2 =heart_data$time, time1 =step_data$time, time_tol =0.5, # step =1/60, align_method ="linear", cor_method ="spearman")print(result_2)
--------------------
Brief information
Shift time is x - y
If shift time > 0:
means that x is changing after y
If shift time < 0:
means that x is changing before y
--------------------
length of x: 12960
length of y: 91948
length of correlations: 0
length of correlations: 0
Max correlation index: 28
Global correlation index: 30
--------------------
Parameters
--------------------
pacakge_name: lagci
function_name: calculate_lagged_correlation
time: 2025-09-22 00:16:24.89312
parameters:
time_tol : 0.5
step : 0.0166666666666667
min_matched_sample : 10
progressbar : TRUE
threads : 10
cor_method : spearman