6  Calculation

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

library(lagci)
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.
── Attaching packages ────────────────────────────────────────── lagci 0.99.5 ──
✔ dplyr   1.1.4     ✔ ggplot2 3.5.2
── Conflicts ────────────────────────────────────────────── lagci_conflicts() ──
✖ methods::body<-()    masks base::body<-()
✖ dplyr::filter()      masks stats::filter()
✖ methods::kronecker() masks base::kronecker()
✖ dplyr::lag()         masks stats::lag()
data("heart_data")
data("step_data")

head(heart_data)
                 time heart
1 2019-04-29 00:00:08    64
2 2019-04-29 00:00:13    65
3 2019-04-29 00:00:18    66
4 2019-04-29 00:00:23    65
5 2019-04-29 00:00:33    64
6 2019-04-29 00:00:38    61
head(step_data)
                 time step
1 2019-04-29 00:00:00    0
2 2019-04-29 00:01:00    0
3 2019-04-29 00:02:00    0
4 2019-04-29 00:03:00    0
5 2019-04-29 00:04:00    0
6 2019-04-29 00:05:00    0
class(heart_data$time)
[1] "POSIXct" "POSIXt" 
class(step_data$time)
[1] "POSIXct" "POSIXt" 

6.2 Calculation

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