calculate_lagged_correlation: Calculate Lagged Correlation Between Two Time Series

View source: R/8-calculate_lagged_correlation.R

calculate_lagged_correlationR Documentation

Calculate Lagged Correlation Between Two Time Series

Description

This function calculates the lagged correlation between two time series data sets over specified time windows, with the capability to adjust for time tolerance, step size, and the minimum number of matched samples. The function also allows for the use of different correlation methods and parallel processing.

Usage

calculate_lagged_correlation(
  x,
  y,
  time1,
  time2,
  time_tol = 1,
  step = 1/60,
  min_matched_sample = 10,
  progressbar = TRUE,
  all_idx = NULL,
  threads = 10,
  cor_method = c("spearman", "pearson")
)

Arguments

x

A numeric vector for the first time series.

y

A numeric vector for the second time series.

time1

A numeric vector of timestamps corresponding to x. (POSIXct).

time2

A numeric vector of timestamps corresponding to y. (POSIXct).

time_tol

Tolerance for the lag time, in hours (default is 1 hour).

step

The step size for the lag window, in hours (default is 1/60 hour, namely 1 min).

min_matched_sample

The minimum number of matched samples to consider a valid correlation (default is 10).

progressbar

Logical indicating whether to show a progress bar (default is TRUE).

all_idx

An optional precomputed index list to speed up calculations.

threads

The number of threads to use for parallel processing (default is 10).

cor_method

The method for computing correlation: "spearman" or "pearson" (default is "spearman").

Details

The function scales the input time series and computes correlations over a range of time lags, handling mismatches in time series lengths by a tolerance window. Parallel processing is implemented to improve performance for large datasets.

Value

An object of class "lagged_cor_result" containing the lagged correlation results, indices, and other relevant data.

Author(s)

Xiaotao Shen shenxt1990@stanford.edu

Examples

## Not run: 
data("heart_data", package = "laggedcor")
data("step_data", package = "laggedcor")

dim(heart_data)
dim(step_data)

x = step_data$step
time1 = step_data$time

y = heart_data$heart
time2 = heart_data$time

object =
  calculate_lagged_correlation(
    x = x,
    y = y,
    time1 = time1,
    time2 = time2,
    time_tol = 0.1,
    step = 1 / 60,
    min_matched_sample = 10,
    progressbar = TRUE,
    threads = 5,
    cor_method = "spearman"
  )
object

## End(Not run)

jaspershen/laggedcor documentation built on Nov. 9, 2023, 3:44 p.m.